Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Before I recursively tar a directory i want to be able to delete all of the executables. In windows I would have simply deleted all of the files with .exe extensions. And I cannot simply delete all files with the executable mode since that would delete shell scripts as well. So is there any way to delete only non-shell script script files in a directory automatically?

share|improve this question
It might help us answer if you provided more context describing why you need to do this. –  Zoredache Jul 1 '09 at 6:11
Why don't you want to delete shell scripts? What about perl / python scripts? Do you only want to delete 'compiled' files? What about .pyc files (compiled python files)? In short this sounds like a very difficuly spec and I can't see why you'd want to do this. –  Rory Jul 28 '09 at 8:34

7 Answers 7

I assume you mean binaries? If the number of executables is very large, this command may fail due to the command line for "file" and "echo" being too long. A quick example:

find /bin -type f | xargs file | grep "ELF.*executable" | awk -F: '{print $1}' | xargs echo

If you replace "echo" with "rm" in the above example, those files will be deleted. The "grep" command should prevent libraries from being deleted (you can test on /lib).

(Note, I only have access to FreeBSD right now, so the output of linux's "file" command may differ, thus changing what you need to do.)

This should give you an easy template for how to do it. You'll just need to be sure to know what to look for in the output given by "file" and grep accordingly.

Be careful. You could totally bork your system if you run this as root on the wrong directory.

share|improve this answer
find . -perm /111 -type f -exec echo rm -v {} \;

The magic here is that the -perm flag (for permissions) can take a / preceding the permission argument, which causes it to search for a logical OR on each of the bits. From the man page:

   -perm /mode
          Any  of the permission bits mode are set for the file.  Symbolic
          modes are accepted in this form.  You must specify 'u',  'g'  or
          'o'  if  you  use a symbolic mode.  See the EXAMPLES section for
          some illustrative examples.  If no permission bits in  mode  are
          set,  this  test  currently  matches no files.  However, it will
          soon be changed to match any file (the idea is to be  more  con-
          sistent with the behaviour of perm -000).

In case that wasn't clear, / specifies 111 specifies x in the user OR x in the group OR x in the others. And those are not XOR, so we're looking for at least one but up to 3.

Since the unix file permissions are


And we care about the x bits, we get a mask of


or 111

It should be noted that in the command listed above, there's an echo to prevent you from shooting yourself in the foot. Feel free to take of the safety once you'd nailed the files correctly.


OK, this blows away all shell scripts, too. There is no nice, neat way to do it with find that I've found, due to the relatively primitive regular expression matching. I can find all of the .sh files you want using regular expressions:

   -regex ".*\(.*sh\$\)"

But I can't invert that at all. So I give up. I'm still leaving this up here, in case it's useful to someone.

Write a shell script, or use someone else's suggestion, or if you really want to, just temporarily remove executable permissions to the shell scripts, remove all the executable files, then add the +x back. Good luck.

share|improve this answer
The OP stated he didn't wish to nuke shell scripts, which your command would do. –  Geoff Fritz Jul 1 '09 at 3:03
argh. ok, one sec –  Matt Simmons Jul 1 '09 at 3:10
by one sec, I mean holy christ there's no way in hell using find. In order to negate a regular expression, you need to use a tool that has negative lookahead assertion, which gnu find doesn't do. –  Matt Simmons Jul 1 '09 at 3:39
file * | grep executable | grep -v 'shell script' | cut -d: -f 1 | xargs rm

Or perhaps:

make clean


share|improve this answer

Before I recursively tar a directory i want to be able to delete all of the executables.

IMHO the short answer is that there is no 100% foolproof way to do this. Depending on permissions alone may miss or include things you don't want. Depending on filenames won't work. Even depending on the output of the file command probably isn't a good idea, I have seen it miss-identify things on several occasions.

share|improve this answer

You need find to iterate over the files in a tree, the file command-line utility to determine their file type, followed by perl to filter the executables:

find . -type f -print0 | xargs -0 file -N0 -- \
| perl -nlwe '/(.*\0).*ELF.*executable/ and printf $1' | xargs -0 ls -l --

You really want to delete the files replace the final ls -l -- with rm --.

Note: all the -print0 and -0 switches are needed to make sure that spaces in the filenames are handled correctly. This is also the reason why perl is used instead of awk, which cannot really swallow null characters in the input. The -- switches are meant to protect against filenames starting a dash.

share|improve this answer

You probably have to write a shell script to do this. Off the top of my head, run file first, get the executables and exclude all the scripts. Then you can operate on the remainder which should be what you are looking for.

share|improve this answer

Using bits from Matt's answer I came up with this:

find . -perm /111 -type f -print0 | xargs -0 file | grep -v 'shell script' | cut -d: -f1 | xargs echo rm
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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