Recursively delete all binary files in folder

Viewed 5052

I want to recursively delete all binary files in a folder under linux using the command-line or a bash script. I found

grep -r -m 1 "^"  path/to/folder | grep "^Binary file"

to list all binary files in path/to/folder at How to list all binary file extensions within a directory tree?. I would now like to delete all these files. I could do

grep -r -m 1 "^"  path/to/folder | grep "^Binary file" | xargs rm

but that is rather fishy as it also tries to delete the files 'Binary', 'file', and 'matches' as in

rm: cannot remove ‘Binary’: No such file or directory
rm: cannot remove ‘file’: No such file or directory
rm: cannot remove ‘matches’: No such file or directory

The question is thus how do I delete those files correctly ?

3 Answers

I coded a tool, called blobs, that lists runable binaries.

Its readme mentions how to pipe to any other command.

This should do the job, if you are deleting a lot of binrary files in a folder.

 find . -type f -executable | xargs rm
Related