Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix find directory with missing file

Tags:

find

unix

How can I use find to identify those directories that do not contain a file with a specified name? I want to create a file that contains a list of all directories missing a certain file.

like image 304
user1486488 Avatar asked Dec 06 '25 15:12

user1486488


2 Answers

Find directories:

find -type d > DIRS

Find directories with the file:

find -type f -name 'SpecificName' | sed 's!/[^/]*$!!' > FILEDIRS

Find the difference:

grep DIRS -vf FILEDIRS
like image 119
perreal Avatar answered Dec 08 '25 16:12

perreal


There are quite a few different ways you could go about this - here's the approach I would take (bash assumed):

while read d
do
  [[ -r ${d}/SpecificFile.txt ]] || echo ${d}
done < <(find . -type d -print)

If your target directories only exist at a certain depth, there are other options you could add to find to limit the number of directories to check...

like image 44
twalberg Avatar answered Dec 08 '25 16:12

twalberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!