Linux Commands

How to Exclude a Directory When Using Find

The find command in Linux is an extremely useful command that helps you out in looking for a file or a directory depending upon the specified search criteria. However, at times, you wish to find all the directories present at a particular path except for a specific directory. Therefore, today, we will talk about the method of excluding a directory when using the find command on a Linux Mint 21 system.

How to Exclude a Directory When Using the Find Command in Linux Mint 21?

To exclude a directory from the search results of the find command on a Linux Mint 21 system, carry out the following steps:

Step 1: Take a Look at the Directories to be Displayed and Excluded

First, try to list down the contents of a parent directory to identify the sub-directory that you wish to exclude in the find search results. To demonstrate this method, list down the contents of the “Folder” directory (residing within the Home directory of the system) which contains three sub-directories – Folder1, Folder2, and Folder3. To list them down, use the following command:

$ ls –h /home/system/Folder

The contents of the “Folder” directory are shown in the following image. Here, we also highlighted the sub-directory that we want to be excluded in the find search results.

Step 2: Excluding a Directory While Running the Find Command

Now, to exclude the highlighted sub-directory from the find search results, use the following command:

$ find . –type d –name Folder1 –prune –o –name ‘Folder*’ –print

Here, “Folder1” represents the name of the directory to be excluded whereas “Folder” represents the name of the directory in which the find command is supposed to run.

You can clearly witness from the image that follows that the specified directory is successfully excluded from the find search results:

Conclusion

With the help of the method described in the provided discussion, you can easily exclude a directory from the search results of the find command on a Linux Mint 21 system. In this way, you can make the specified directory disappear from the output of the find command as per your requirements.

Similar Posts