How to Exclude a Directory When Using 'Find'

How do I exclude a directory when using `find`?

Use the -prune primary. For example, if you want to exclude ./misc:

find . -path ./misc -prune -o -name '*.txt' -print

To exclude multiple directories, OR them between parentheses.

find . -type d \( -path ./dir1 -o -path ./dir2 -o -path ./dir3 \) -prune -o -name '*.txt' -print

And, to exclude directories with a specific name at any level, use the -name primary instead of -path.

find . -type d -name node_modules -prune -o -name '*.json' -print

How to exclude subdirectories of a specific directory from find command?

Just with find:

find /var/www/html -type f -not -path '/var/www/html/folder/*/*'

Original answer:

One hack could be grep -v on the output of find:

find /var/www/html/ -type f | grep -v "/var/www/html/folder/.*/" | wc -l

Exclude folders when using find

You can use the -path option to exclude certain directory paths in your search:

find / -iregex ".*\.py" ! -path "/your/django/directory"

And you can chain this multiple times if you want to exclude multiple directories:

find / -iregex ".*\.py" ! -path "/your/django/directory" ! -path "/another/dir"

How do I exclude a directory when using `find`?

Use the -prune primary. For example, if you want to exclude ./misc:

find . -path ./misc -prune -o -name '*.txt' -print

To exclude multiple directories, OR them between parentheses.

find . -type d \( -path ./dir1 -o -path ./dir2 -o -path ./dir3 \) -prune -o -name '*.txt' -print

And, to exclude directories with a specific name at any level, use the -name primary instead of -path.

find . -type d -name node_modules -prune -o -name '*.json' -print

How to exclude this / current / dot folder from find type d

POSIX 7 solution:

find . ! -path . -type d

For this particular case (.), golfs better than the mindepth solution (24 vs 26 chars), although this is probably slightly harder to type because of the !.

To exclude other directories, this will golf less well and requires a variable for DRYness:

D="long_name"
find "$D" ! -path "$D" -type d

My decision tree between ! and -mindepth:

  • script? Use ! for portability.
  • interactive session on GNU?

    • exclude .? Throw a coin.
    • exclude long_name? Use -mindepth.

Exclude a sub-directory using find

This works:

find /home/feeds/data -type f -not -path "*def/incoming*" -not -path "*456/incoming*"

Explanation:

  • find /home/feeds/data: start finding recursively from specified path
  • -type f: find files only
  • -not -path "*def/incoming*": don't include anything with def/incoming as part of its path
  • -not -path "*456/incoming*": don't include anything with 456/incoming as part of its path

Use find command but exclude files in two directories

Here's how you can specify that with find:

find . -type f -name "*_peaks.bed" ! -path "./tmp/*" ! -path "./scripts/*"

Explanation:

  • find . - Start find from current working directory (recursively by default)
  • -type f - Specify to find that you only want files in the results
  • -name "*_peaks.bed" - Look for files with the name ending in _peaks.bed
  • ! -path "./tmp/*" - Exclude all results whose path starts with ./tmp/
  • ! -path "./scripts/*" - Also exclude all results whose path starts with ./scripts/

Testing the Solution:

$ mkdir a b c d e
$ touch a/1 b/2 c/3 d/4 e/5 e/a e/b
$ find . -type f ! -path "./a/*" ! -path "./b/*"

./d/4
./c/3
./e/a
./e/b
./e/5

You were pretty close, the -name option only considers the basename, where as -path considers the entire path =)

find: Exclude files with the same name based on their depth in a directory

find . -type f -not -regex '\./[^/]+/file\.ext'

It will discard anything that match the regex. Here a direct subdirectory containing the file file.ext.

The regex is run to the whole path.

  • \. means starts with a .
  • [^/]+ means a bunch of characters not containing a /
  • file\.ext means file.ext

How to exclude folders in DU and TAR commands

With GNU tar and du from GNU coreutils with absolute paths:

#!/bin/bash

mainLocation='/data/media/0/!Temp/!Test'
ex1='/data/media/0/!Temp/!Test/bellota'
ex2='/data/media/0/!Temp/!Test/Xiaomi'
tarArchiveLocationWithName='/tmp/big-files.tar'

# get size in bytes without excludes
size=$(du -sb --exclude="$ex1" --exclude="$ex2" "$mainLocation" | awk '$0=$1')

# create tar without excludes
tar -C / --exclude="$ex1" --exclude="$ex2" -c -P "$mainLocation" | pv -s "$size" > "$tarArchiveLocationWithName"


Related Topics



Leave a reply



Submit