3

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.

user1486488
  • 31
  • 1
  • 2

3 Answers3

3

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
perreal
  • 94,503
  • 21
  • 155
  • 181
  • `find -type f -name "SpecificName" -exec dirname {} \; | uniq` use this a much simple way to extract unique dirnames containing the file. Use the same trick with directories. – Jean-Baptiste Yunès Jun 26 '13 at 14:07
  • Use `grep -F` or `fgrep` if the list of directories is long (e.g. >10000) to speed things up. – Heiko Nov 11 '16 at 11:33
1

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...

twalberg
  • 59,951
  • 11
  • 89
  • 84
0

This is an example for a file main.cpp. In the example all directories that do not contain main.cpp in them are found. As you see I first get a list of all folders, then get a list of folders having main.cpp and then run diff to get a difference between the two lists:

diff  \
  <(find . -exec readlink -f {} \; | sed 's/\(.*\)\/.*$/\1/' | sort | uniq) \
  <(find . -name main.cpp  -exec readlink -f {} \; | sed 's/\(.*\)\/.*$/\1/' | sort | uniq) \
  | sed -n 's/< \(.*\)/\1/p'