LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to find directories that do not have a certain file (https://www.linuxquestions.org/questions/programming-9/how-to-find-directories-that-do-not-have-a-certain-file-810772/)

subsonix 05-28-2010 01:48 PM

How to find directories that do not have a certain file
 
I'm having problems figuring out the process to find directories that DO NOT contain a certain file. I have a mp3 collection that all the album art is name "folder.jpg". Not all the albums have images. I need a way to find the albums/directories that do not contain "folder.jpg". I can find the ones that do contain "folder.jpg" with

Code:

find . -iname 'folder.jpg' -print0 | xargs -0 ls >> album_art
but that is as far as my bash-fu can take me. I'm not really sure what my next step is.

Any suggestions as to how to go about this would be appreciated.

My directory structure is like such:
a-z/artist/album/folder.jpg

tuxdev 05-28-2010 01:52 PM

Code:

find . '!' -iname 'folder.jpg'
What is it that you're actually trying to do?

Robhogg 05-28-2010 02:07 PM

I don't think that you can do this with a single "find" command (find . ! -iname "folder.jpg" will find all files (and folders) not called folder.jpg).

To find all directories that do not contain this file, you could use a simple loop like:

Code:

for dir in `find . -type d`; do
if ! ls $dir/folder.jpg > /dev/null 2>&1; then
echo $dir
fi
done

The find command here just finds any directory, and the if statement then tests that ls cannot list a file called folder.jpg in that directory. (the "> /dev/null 2>&1" after the ls statement discards all normal output, as well as any "No such file..." errors).

tuxdev 05-28-2010 03:01 PM

hmm.. reading comprehension fail
Code:

find -type d '!' -exec test -e "{}/folder.jpg" ";" -print

subsonix 05-28-2010 04:27 PM

Thanks Robhogg, that gets me a lot closer.

Now I just need to list only the album directory and not its parent directory. I'm off to research and play with this a little more.

Robhogg 05-29-2010 12:56 PM

Quote:

Originally Posted by tuxdev (Post 3984616)
hmm.. reading comprehension fail
Code:

find -type d '!' -exec test -e "{}/folder.jpg" ";" -print

I stand corrected.


All times are GMT -5. The time now is 06:06 PM.