I am in need of deleting all executable files from a directory. My directory contains some configuration files and executable files, and I need to delete all executable files that I don't want.

For that I have wrote a command like this:

ls -lp | grep -v / | awk 'match($0,"-**x*x*x",a);{print  a[1]}'| \
awk '{print $9}' | xargs rm -f

Is there aother way to do this?

I tried with find. It will list all other sub directory files. I used grep -v /, to avoid sub-directories in the current folder.

  • FWIW: your awk approach breaks for filenames with spaces. Use the find solution below. – Satō Katsura Jul 4 '16 at 9:13
  • With zsh, this would be as simple as rm *(*). – marcelm Jul 4 '16 at 12:09

You should use find for this task:

find . -type f -executable

will show you the files that are executable by current user, recursively.

To limit the search to current directory only:

find . -maxdepth 1 -type f -executable

To remove:

find . -maxdepth 1 -type f -executable -exec rm {} +

Or with GNU find:

find . -maxdepth 1 -type f -executable -delete

As a side note, if you want to find files executable by any user, not just the current user (set as regular execute permission bits):

find . -type f -perm /111
  • 1
    Got it!! great, I tried with find after some time, I stopped that one and started digging into awk!! Thanks! – saravanakumar Jul 4 '16 at 9:16
  • Could you please explain rm {} + meaning in remove command? – saravanakumar Jul 4 '16 at 9:27
  • 1
    @saravanakumar exec rm {} + is find's exec` predicate within which rm is being used to remove the file. {} + will be expanded to the files found by find . -maxdepth 1 -type f -executable – heemayl Jul 4 '16 at 9:28
  • and if you wanted to remove those files executables for everyone (instead of being executable only by the current user (eg 750 mode)) you'll use -perm -111 – Carlos Campderrós Jul 4 '16 at 9:48
  • 1
    difference between AND and OR. /111 requires that it is executable by someone (owner OR group OR other can execute it), -111 requires that it is executable by everyone (owner, group, AND other). – cas Jul 4 '16 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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