I have this project that has a images folder with images in them and I'm pretty sure some of them are not used. So I'd like to list all images that are not referenced in any other file. I'm not very experienced using the Terminal but this is what I know so far and works:

Being at the root folder of the project.

To list all images (I know they are all png).

find . -name "*.png" -exec basename {} \;

To search in all files that are not in .svn

grep -Iri --exclude-dir=".svn" 'searchTerm' .

Now I'm stuck. My next step was to try and pipe the first command as searchTerm to the second but wasn't succesfull with that. And anyway that would be almost like the opposite as what I try to do. But if that worked the result could possibly be used to check manually. But what I really would like is just list the images that are not referenced.

1 Answer 1


You could do:

find . -name '*.png' |
  awk -F/ '{print tolower($NF)}' |
  sort -u > ~/tmp/png-files &&
grep -IhFriof ~/tmp/png-files --exclude-dir=".svn" . | 
  awk '{print tolower($0)}' | sort -u | comm -23 ~/tmp/png-files -

That would give you the lower-case names of the png files that are never referenced.

The first pipeline builds a sorted list of lowercase file names.

We pass that with -f to grep with -F for fixed-string search and -o to output the matched text only.

The output of that is sorted and converted to lowercase as well and compared with our earlier list. Then we report the entries that are only found in our first list.

  • The ouput I get from this is -bash: /Users/ibeckers/tmp/png-files: No such file or directory find: -printf: unknown primary or operator
    – Ivo
    Jun 12, 2015 at 16:20
  • Maybe has to do with this GNU tools since I am not familiar with that.
    – Ivo
    Jun 12, 2015 at 16:21
  • @IvoBeckers mkdir ~/tmp to have your tmp folder, or store that file elsewhere. Sorry, I had missed the osx tag. Should be better now. Jun 12, 2015 at 16:22
  • I am using the Terminal in OS X
    – Ivo
    Jun 12, 2015 at 16:23
  • Now it does execute but I'm afraid it doesn't work. it seems to list every image. I'm trying to understand the commands and I believe the cause might be that the grep doesn't just return the name that matches but also the path of the file where it found the match
    – Ivo
    Jun 12, 2015 at 17:05

You must log in to answer this question.

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