Find (and remove files) with find

terminal

find is a great tool to find things, but not only to find them, but to work on them. I have a small collection of music, which I presume at some point will be removed from my computer hard drive (maybe if I can get a new one it won’t be there at all) because I mostly listen to music from internet and I have a copy of the music in a external hard drive.

Today I imported the folder where the music is to iTunes again, and it created playlists that I don’t remember to have them before. That is because there were several m3u files there (winamp playlists), there are around 5k files in that directory tree and I don’t want to spend my time looking for m3u files, so, let the computer do the job.

With find is easy to find files, not by their name but by their type the f is for file.

find . -type f

Ok, but that will include all the “mp3” which are files, so if I make use of -iname to find by name,  how can I do the opposite?, well, like in most programming languages, you can negate by using the exclamation symbol !.

find . -type f ! -iname "*mp3"

And add some more extensions.

find . -type f ! -iname "*mp3" ! -iname "*ogg" ! -iname "*m4a" ! -iname "*wma"

That will give me all files that are not mp3, ogg, m4a or wma. Then just use the -delete parameter to as find to delete them.

find . -type f ! -iname "*mp3" ! -iname "*ogg" ! -iname "*m4a" ! -iname "*wma" -delete

And… they are gone.

How would you do it?. Share it in the comments.

 2,016 total views,  2 views today