Linux 使用 ls 和 grep 查找可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7812324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:44:55  来源:igfitidea点击:

Finding executable files using ls and grep

linuxbashgrepls

提问by k13n

I have to write a script that finds all executable files in a directory. So I tried several ways to implement it and they actually work. But I wonder if there is a nicer way to do so.

我必须编写一个脚本来查找目录中的所有可执行文件。所以我尝试了几种方法来实现它,它们确实有效。但我想知道是否有更好的方法来做到这一点。

So this was my first approach:

所以这是我的第一种方法:

ls -Fla | grep \*$

This works fine, because the -F flag does the work for me and adds to each executable file an asterisk, but let's say I don't like the asterisk sign.

这工作正常,因为 -F 标志为我完成工作并向每个可执行文件添加一个星号,但假设我不喜欢星号。

So this was the second approach:

所以这是第二种方法:

ls -la | grep -E ^-.{2}x 

This too works fine, I want a dash as first character, then I'm not interested in the next two characters and the fourth character must be a x.

这也很好用,我想要一个破折号作为第一个字符,然后我对接下来的两个字符不感兴趣,第四个字符必须是 x。

But there's a bit of ambiguity in the requirements, because I don't know whether I have to check for user, group or other executable permission. So this would work:

但是要求有点含糊,因为我不知道我是否必须检查用户、组或其他可执行权限。所以这会起作用:

ls -la | grep -E ^-.{2}x\|^-.{5}x\|^-.{8}x

So I'm testing the fourth, seventh and tenth character to be a x.

所以我正在测试第四个、第七个和第十个字符是 x。

Now my real question, is there a better solution using ls and grep with regex to say:

现在我真正的问题是,是否有更好的解决方案使用 ls 和 grep 与正则表达式说:

I want to grep only those files, having at least one x in the ten first characters of a line produced by ls -la

我只想 grep 那些文件,在产生的行的前十个字符中至少有一个 x ls -la

采纳答案by Dan

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

你需要使用ls吗?您可以使用 find 来做同样的事情:

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.

将返回当前目录中的所有可执行文件。删除 -maxdepth 标志以遍历所有子目录。

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

回答by rmmh

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

如果您绝对必须使用 ls 和 grep,则此方法有效:

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

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

它匹配第一个单词(非空格)包含至少一个“x”的行。

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

Find 是一个完美的工具。这将查找所有可执行文件(-type f):

find . -type f -executable

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

如果您不希望它递归列出所有可执行文件,请使用 maxdepth:

find . -maxdepth 1 -type f -executable

回答by David Poole

Perhaps with test -x?

也许用test -x?

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

The \ on ls will bypass shell aliases.

ls 上的 \ 将绕过 shell 别名。

回答by user842679

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

This gives absolute paths to the executables.

这给出了可执行文件的绝对路径。

回答by Alptekin Keskin

file * |grep "ELF 32-bit LSB executable"|awk '{print }'