Finding executable files using ls and grep

If you absolutely must use ls and grep, this works:

ls -Fla | grep '^\S*x\S*'

It matches lines where the first word (non-whitespace) contains at least one 'x'.

Find is the perfect tool for this. This finds all files (-type f) that are executable:

find . -type f -executable

If you don't want it to recursively list all executables, use maxdepth:

find . -maxdepth 1 -type f -executable

Do you need to use ls? You can use find to do the same:

find . -maxdepth 1 -perm -111 -type f

will return all executable files in the current directory. Remove the -maxdepth flag to traverse all child directories.

You could try this terribleness but it might match files that contain strings that look like permissions.

ls -lsa | grep -E "[d\-](([rw\-]{2})x){1,3}"

Perhaps with test -x?

for f in $(\ls) ; do test -x $f && echo $f ; done

The \ on ls will bypass shell aliases.


for i in `ls -l | awk '{ if ( $1 ~ /x/ ) {print $NF}}'`; do echo `pwd`/$i; done

This gives absolute paths to the executables.

Tags:

Linux

Bash

Grep

Ls