使用ls和grep查找可执行文件 [英] Finding executable files using ls and grep
问题描述
我必须编写一个脚本来查找目录中的所有可执行文件。所以我尝试了几种方法来实现它,并且他们实际上工作。但我想知道是否有更好的方法来做到这一点。
所以这是我的第一个方法:
ls -Fla | grep \ * $
这可以正常工作,因为-F标志为我工作并添加给每个可执行文件一个星号,但假设我不喜欢星号。
所以这是第二种方法:
ls -la | grep -E ^ - 。{2} x
这也可以正常工作,我想要一个破折号作为第一个字符,那么我对接下来的两个字符不感兴趣,第四个字符必须是x。
但是在要求中有一点不明确,因为我不喜欢不知道我是否必须检查用户,组或其他可执行权限。所以这将工作:
ls -la | grep -E ^ - 。{2} x \ | ^ - 。{5} x \ | ^ - 。{8} x
所以我测试第四,第七和第十个字符是x。
现在我真正的问题是,是否有更好的解决方案,使用ls和grep与正则表达式来表示:
I只想grep这些文件,在由 ls -la
你需要使用ls吗?你可以使用find来做同样的事情:
find。 -maxdepth 1 -perm -111 -type f
将返回当前目录中的所有可执行文件。删除-maxdepth标志以遍历所有子目录。
您可以尝试这种可怕的方式,但它可能会匹配包含类似权限的字符串的文件。
ls -lsa | grep -E[d \ - ](([rw\ - {2})x){1,3}
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.
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.
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.
Now my real question, is there a better solution using ls and grep with regex to say:
I want to grep only those files, having at least one x in the ten first characters of a line produced by ls -la
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}"
这篇关于使用ls和grep查找可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!