Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude directories in find that don't contain a specific filename?

Tags:

find

unix

Let's say I have a directory containing a number of subdirectories, each with a number of files in them. I wish to check which of these directories do not contain a specific file. For example, if these are my directories:

dir/A:
    foo
    bar
dir/B:
    bar
dir/C:
    foo
dir/D:
dir/E:
    foo
    bar

If I wanted to list all directories not containing foo, I would get:

dir/B
dir/D

Is it possible to do this with unix find, or do I need to use some alternate tool?

like image 302
srstinson Avatar asked Apr 25 '13 21:04

srstinson


People also ask

How do I exclude a directory in find command?

To exclude multiple directories, OR them between parentheses. And, to exclude directories with a specific name at any level, use the -name primary instead of -path .

How do I exclude a file path?

To exclude specific files, in the Files text box, type the file names and paths to exclude, separated by commas. For example, C:\windows\system32\filename. dll. You can include question mark and asterisk wildcards.

How do you exclude a directory in ls?

Adding -ls or -exec ls -l {} \; would make it like ls -l without directories.

How do you use prune in find command?

Prune usage:$ find . -prune . The find command works like this: It starts finding files from the path provided in the command which in this case is the current directory(.). From here, it traverses through all the files in the entire tree and prints those files matching the criteria specified.


1 Answers

I found a good solution after a bit more research:

find . -type d \! -exec test -e '{}/foo' \; -print

Where you replace foo with whatever file you're looking for.

This will print out a list of all directories that don't contain whatever file you're looking for.

like image 179
srstinson Avatar answered Dec 04 '22 22:12

srstinson