Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 executables 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.

share|improve this question
    
FWIW: your awk approach breaks for filenames with spaces. Use the find solution below. – Sato Katsura yesterday
    
With zsh, this would be as simple as rm *(*). – marcelm yesterday
up vote 13 down vote accepted

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
share|improve this answer
1  
Got it!! great, I tried with find after some time, I stopped that one and started digging into awk!! Thanks! – saravanakumar yesterday
    
Could you please explain rm {} + meaning in remove command? – saravanakumar yesterday
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 yesterday
    
Thanks, for your explanation!! – saravanakumar yesterday
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 yesterday

Your Answer

 
discard

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.