Finding executable files using ls and grep

微笑、不失礼 提交于 2019-11-29 22:15:14
Dan

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}"
rmmh

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

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.

Alptekin Keskin
file * |grep "ELF 32-bit LSB executable"|awk '{print $1}'
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!