Shell – Delete all files with executable permission in a directory

filesshell

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.

Best Answer

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