Sign up ×
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?


locked by womble Sep 27 at 7:30

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

closed as unclear what you're asking by kasperd, womble Sep 27 at 7:30

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the 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
You haven't specified what you want to happen if a file with executable mode is neither a shell script nor in an executable format. The simplest solution would certainly be to delete those incorrectly formatted files as well. But it might not be desirable if forgetting the #! line of a shell script would cause it to be deleted. – kasperd Sep 26 at 15:47

8 Answers 8

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.


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.

If the total length of the filenames is over the limit, xargs will do multiple commands as necessary; that's actually it's reason for existing in the first place. – dave_thompson_085 Sep 26 at 17:15
file * | grep executable | grep -v 'shell script' | cut -d: -f 1 | xargs rm

Or perhaps:

make clean


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.

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
You can negate any test in find by preceding it with ! as a separate arg; in most shells usually ! invokes history expansion so to pass it to find you need to backslash or single-quote. find matches are always complete and don't need anchors, and here you don't need regex glob is fine: '!' -name '*.sh' But shell scripts aren't always named – dave_thompson_085 Sep 26 at 17:15

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.


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.


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

I created a bash script to do just this. If you study all the lines in it I'm sure you can figure out how to do everything from the command line and modify it to delete any type of file, i.e. use "find -iname" or whatever.

#this script will grab all executable files in the
#bash shell scripts ending in .sh will be saved
#current directory and delete them 
#it will also destroy itself so keep a copy somewhere

echo "WARNING! This script will self-destruct and take all"
echo "executable files in the current directory when executed"
echo ""
#first create a new file to set up executable files 
touch xdelgo.txt

#save all the bash shell scripts ending in .sh
#this will only work and in the pwd and not child directories
echo "#!/bin/bash" >>
find . -type f | grep .sh | grep -v >> protect.txt
SAVE=$'\n';x=sudo;for l in $(<protect.txt);do echo -e "$x chmod u-x $l";  
((x+1));done >> 

sudo chmod u+x

#throw all executable files from the current directory inside
find -type f -executable >> xdelgo.txt
echo "The following files have been marked for deletion"
echo " "
cat xdelgo.txt
echo " "

#create a new script that will execute deletion
echo "#!/bin/bash" >>
echo "rm" >>
echo "rm xdelgo.txt" >>
echo "rm protect.txt" >>

#append rm to each line and throw into new executable file
DEL=$'\n';x=rm;for l in $(<xdelgo.txt);do echo -e "$x\t$l";((x+1));done   

#go give permission to the file
sudo chmod u+x

echo "bash scripts ending in .sh have been protected"
echo "run ./ to re-enable .sh files"
echo ""
echo "#!/bin/bash" >>
find . | grep .sh | grep -v >> restore.txt  
SAVE=$'\n';x=sudo;for l in $(<restore.txt);do echo -e "$x chmod u+x $l";  
((x+1));done >>

sudo chmod u+x
echo "rm restore.txt" >>
echo "rm" >>
echo "rm" >>

echo "Are you sure you really wanna delete these files?"
echo "Press ENTER to continue, ctrl+c to abort" 
read input
The question explicitly asks for a solution which does not delete shell scripts. Additionally you script will have major issues with special characters in filenames. And the temporary files are not really needed. – kasperd Sep 26 at 15:34

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