0

I wish to find all files and folders except/excluding folders starting with 'tmp' and 'logs' under '/app/test' directory using prune

Thus I tried the below command.

find /app/test -type d \( ! -name logs* \)  -o \( ! -name log \)  -o \( ! -name tmp* \)  -o
\( ! -name tmp \) -prune | tee /tmp/findall.log

But I see top folder included in the find result like below:

/app/test/Admin/tmp/data/pm.trp
/app/test/Admin/tmp/wond/iqc.log
etc ...

Can you please suggest?

4
  • Wildcards used as patterns with find must me quoted to stop the shell from expanding them. Do you have names matching tmp* and logs* in the current directory when you run this? Also, you seem to apply -prune to everything not matching the names.
    – Kusalananda
    May 16, 2020 at 8:46
  • i put single quotes find /app/test -type d \( ! -name 'logs*' \) -o \( ! -name 'log' \) -o \( ! -name 'tmp*' \) -o \( ! -name 'tmp' \) -prune | tee /tmp/findall.log but still /tmp/ is showing in the results. Not sure what you said about prune.
    – Ashar
    May 16, 2020 at 9:00
  • You want to -prune all names matching tmp etc., right? But you have ! -name tmp -prune, i.e. "prune names that are not tmp".
    – Kusalananda
    May 16, 2020 at 9:17
  • if I remove the ! from the command then find does not get all the files but only displays folders with the names logs & tmp. My requirement is simple... i want find to show all files except files which are in folder names starting with tmp or logs.
    – Ashar
    May 16, 2020 at 9:25

2 Answers 2

1

Apart from not quoting words that contain filename globbing characters, you also have inverted the sense of all -name tests, resulting in pruning the names that do not match these names and patterns from the search tree.

Instead:

find /app/test -type d \
    \( -name 'logs*' -o -name log -o -name 'tmp*' -o -name tmp \) -prune -o \
    -print | tee /tmp/findall.log

The -prune test would, without -print, still print out pathnames of things matching up to that point.

3
  • This works .... one query I still have is what if I want find to get files under a folder called config (exact match) but exclude all folders which have names starting with config****
    – Ashar
    May 16, 2020 at 9:49
  • @Ashar You would use something like find . -type d -name config -print -o -name 'config*' -prune
    – Kusalananda
    May 16, 2020 at 10:10
  • @Ashar Sorry, you said all files under. Then find . -type f -path '*/config/*' -print -o \( ! -name config -name 'config*' \) -prune
    – Kusalananda
    May 16, 2020 at 10:23
1

Another way to do it:

find /app/test -type d \( -path "*/log*" -o -path "*/tmp" \) -prune -o -print | tee /tmp/findall.log

Note that when using -path the full directory name is being matched; hence the use of the front * wildcard in the glob patterns.

Note also that "*/tmp" matches only paths with directories named exactly tmp, and not directories named tmp2 or tmporary for example; while "*/log*" will match any path including a directory name starting with log.

-prune makes find skip all directories matched within the parenthesis.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .