Gnu find: Difference between revisions

From wikinotes
 
Line 38: Line 38:
== Exclude Directories while searching for files ==
== Exclude Directories while searching for files ==
<blockquote>
<blockquote>
<syntaxhighlight lang="bash">
find . \
  -path ./exclude_me -prune -o  `# exclude ./exclude_me and subdirs` \
  -type f -name '*.txt'        `# rest of your find expression`
</syntaxhighlight>
<source lang="bash">
<source lang="bash">
# search for *.txt files, not recursing into ~/.cache
# search for *.txt files, not recursing into ~/.cache

Latest revision as of 03:09, 17 November 2021

Search files and directories.
The man page is very good and has several examples.
This page is essentially a cookbook.

Also see bsd find.

Documentation

man find https://man.archlinux.org/man/core/findutils/find.1.en

Recipes

Find files by bitmask

Unfortunately, different between gfind and bsd find.

# files with the executable bit set for user AND group AND other
find . -type f -111

# files with the executable bit set for user OR group OR other
# ('+111' on bsd-find)
find . -type f /111

Suppress stderr in subshell

results=($({ find . -name .git; } 2>/dev/null))

Exclude Directories while searching for files

find . \
  -path ./exclude_me -prune -o  `# exclude ./exclude_me and subdirs` \
  -type f -name '*.txt'         `# rest of your find expression`
# search for *.txt files, not recursing into ~/.cache
find . \
    -not \( -path $HOME/.cache -prune \) \
    -name '*.txt'

Another less efficient alternative, that must glob match all files

find . \
    -not -path $HOME/.cache/* \
    -name '*.txt'