4

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 ?

2
  • That's a bad definition of "binary files" to begin with. Regardless - I suggest you to read about Word Splitting. I'm pretty sure you'll quickly understand the source of your problem. Commented Mar 25, 2016 at 21:19
  • That does indeed work, thanks! I do agree that this definition of "binary file" is not very good (see the answer below by vesche and its discussion), but it captures exactly the files I wanted to delete in the given use case.
    – Christian
    Commented Mar 26, 2016 at 7:31

3 Answers 3

7

This command will return all binary executable files recursively within a directory, run this first to ensure proper output.

find . -type f -executable -exec sh -c "file -i '{}' | grep -q 'x-executable; charset=binary'" \; -print

If that works you can pass the output to xargs to delete these files.

find . -type f -executable -exec sh -c "file -i '{}' | grep -q 'x-executable; charset=binary'" \; -print | xargs rm -f

Hope this helped, have an awesome day! :)

15
  • Many thanks for your answer! I also found that command -- the problem is that this command does not properly find the compiled files I want to delete: the grep command finds about 50 files such as ./src/polynom.o while the command you suggest only finds a single file (which is also found using grep).
    – Christian
    Commented Mar 25, 2016 at 14:14
  • Ah, my mistake I assumed you were referring to binary executables, try this: find . -type f -exec sh -c "file -i '{}' | grep -q 'charset=binary'" \; -print
    – vesche
    Commented Mar 25, 2016 at 15:38
  • Hm, that might be a mistake of the tar'ed program I look at, but that command also lists multiple raw c files such as ./pkg/meataxe/src/zgauss.c, and I surely do not want to delete those...
    – Christian
    Commented Mar 25, 2016 at 15:41
  • How odd, I actually found the version of meataxe you are looking at (math.rwth-aachen.de/~MTX/download.html) version 2.1 I believe? Those .c files are appearing as binary files, which is unusual. Looks as if this was fixed in later versions... There's non-ascii characters in the files, looks like on the line where it says " * MeatAxe-2.0, Phase II.^?". You can see it with this grep -P '[\x7f-\xff]' ./pkg/meataxe/src/* this will find non-ascii characters.
    – vesche
    Commented Mar 25, 2016 at 16:02
  • Wow, not bad -- it is indeed version 2.2.3 from Aachen in 1997.
    – Christian
    Commented Mar 25, 2016 at 16:14
1

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

Its readme mentions how to pipe to any other command.

1

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

 find . -type f -executable | xargs rm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.