使用 ls 和 grep 查找可执行文件 [英] Finding executable files using ls and grep

查看:86
本文介绍了使用 ls 和 grep 查找可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 *$

这很好用,因为 -F 标志对我有用,并为每个可执行文件添加了一个星号,但假设我不喜欢星号.

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.

所以这是第二种方法:

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

这也很好用,我想要一个破折号作为第一个字符,然后我对接下来的两个字符不感兴趣,第四个字符必须是 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.

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

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

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

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

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

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

我只想 grep 那些文件,在 ls -la

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

推荐答案

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

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

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

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

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

这篇关于使用 ls 和 grep 查找可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆