Linux find exclude path

Find command Exclude or Ignore Files (e.g. Ignore All Hidden .dot Files )

Find command exclude or ignore files syntax

The syntax is as follows:

Examples: find command and logical operators

Find any file whose name ends with either ‘c’ or ‘asm’, enter:
$ find . -type f \( -iname «*.c» -or -iname «*.asm» \)
In this example, find all *.conf and (.txt) text files in the /etc/ directory:
$ find . -type f \( -name «*.conf» -or -name «*.txt» \) -print

Fig.01: Linux find command exclude files command

Understanding find command operators

Operators build a complex expression from tests and actions. The operators are, in order of decreasing precedence:

( expr ) Force precedence. True if expr is true
expr -not expr
! expr
True if expr is false. In some shells, it is necessary to protect the ‘!’ from shell interpretation by quoting it.
expr1 -and expr2 expr2 is not evaluated if expr1 is false.
expr1 -or expr2 expr2 is not evaluated if expr1 is true.

How do I ignore hidden .dot files while searching for files?

Find *.txt file but ignore hidden .txt file such as .vimrc or .data.txt file:
$ find . -type f \( -iname «*.txt» ! -iname «.*» \)
Find all .dot files but ignore .htaccess file:
$ find . -type f \( -iname «.*» ! -iname «.htaccess» \)

Читайте также:  Не подходят драйвера под windows

Say hello to -path option

This option return true if the pathname being examined matches pattern. For example, find all *.txt files in the current directory but exclude ./Movies/, ./Downloads/, and ./Music/ folders:

Источник

Exclude a directory or multiple directories while using find command

Table of Contents

Is it possible to exclude a directory with find command? Exclude directories while doing running find command?

Yep, the command FIND has wide range of options to search what you actually looking for. I have already listed different switches and its usages with examples. Here we go for excluding some directories from our find job.

In some cases, we have to exclude some directories from our search pattern to improve the search speed or efficiency. If the server has a lot of directories and we are sure about that the file / directory that we are searching is not in some directories, we can directly exclude those to improve the performance. The result will be faster as compared to the full search.

There are different ways to exclude a directory or multiple directories in FIND command. Here I’m listing some methods!

To explain this, I created the following directories and files:

  • cry“, “bit” and “com” directories.
  • findme “: The test file in all directories.

Lets see the output:

Method 1 : Using the option “-prune -o”

We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command.

See the example:

The directory “bit” will be excluded from the find search!

Читайте также:  Как остановить начатое обновление windows 10

Method 2 : Using “! -path”

This is not much complicated compared to first method. See the example pasted below:

Method 3 : Simple 🙂

Yes, it’s very simple. We can ignore the location by using inverse grep “grep -v” option.

See the example:

Excluding multiples directories

Similar way we can exclude multiple directories also. See the sample outputs:

Источник

Оцените статью