bash Linux:如何仅使用 ls 列出所有文件/目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15605697/
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
Linux: How to list all files/directories, using only ls
提问by m.spyratos
This is a question given at university.
这是大学里的一道题。
The question is: List all files/directories inside testfilesfolder, that have no extension.
The right answer given is this:
问题是:列出文件testfiles夹中没有扩展名的所有文件/目录。
给出的正确答案是这样的:
ls testfiles | grep -v "\."
Now, just to understand how lsregex works, could someone please explainto me how this would be by using only ls? Moreover, I would appreciate any example that also uses the dollar sign $, to specifically state that the name ends with .[a-z].
现在,只是为了了解ls正则表达式的工作原理,有人可以向我解释仅使用regex 会如何ls吗?此外,我将感谢任何也使用美元符号的示例$,以明确说明名称以.[a-z].
Any help, really appreciated.
任何帮助,真的很感激。
One more thing! Another answer to this question is using:
还有一件事!这个问题的另一个答案是使用:
ls testfiles | grep "^[^.]*$"
How is that read? I read ^[^.]*$like this:
那是怎么读的?我^[^.]*$是这样读的:
^ -> does not contain
[^.] -> starts with a dot
* -> repeated zero or more times
$ -> till the end
I would like someone to correct me on this... Thanks!
我希望有人在这方面纠正我...谢谢!
采纳答案by ktm5124
Unix utilities were meant to be used as filters, so the answer given makes sense since it best approximates a real-world application.
Unix 实用程序旨在用作过滤器,因此给出的答案很有意义,因为它最接近真实世界的应用程序。
You do understand, though, that grep "\."matches everything with a "period" (hence, extension), and grep -v "\."matches everything else (i.e., the complement).
但是,您确实理解,这grep "\."与带有“句点”的所有内容(因此,扩展名)grep -v "\."相匹配,并与其他所有内容(即补语)相匹配。
It is hard to make the command any more precise than what it is already, since who can say what's intended to be an extension, and what's not?
很难使命令比现有命令更精确,因为谁能说出什么是扩展,什么不是?
Part 2: ls testfiles | grep "^[^.]*$"
第2部分: ls testfiles | grep "^[^.]*$"
A bracket expression is a list of characters enclosed by [ and ]. It matches any single character in that list; if the first character of the list is the caret ^ then it matches any character not in the list. For example, the regular expression [0123456789] matches any single digit.
括号表达式是由 [ 和 ] 括起来的字符列表。它匹配该列表中的任何单个字符;如果列表的第一个字符是插入符号 ^ 则它匹配不在列表中的任何字符。例如,正则表达式 [0123456789] 匹配任何单个数字。
http://unixhelp.ed.ac.uk/CGI/man-cgi?grep
http://unixhelp.ed.ac.uk/CGI/man-cgi?grep
So ^[^.]*$actually means:
所以^[^.]*$实际上意味着:
Anything that begins and ends with a string of characters, each of which is not a period.
任何以字符串开头和结尾的内容,每个字符串都不是句点。
The first caret is not a negation, it means begins with. The second caret is the "not" signifier.
第一个脱字符不是否定,它意味着开始。第二个脱字符是“非”能指。
回答by Charles Duffy
Correction:
更正:
^ -> pattern starts at the beginning of the string
[^.] -> matches something that is not a dot
* -> repeated zero or more times
$ -> and must continue to match (only non-dot items) until the end
Thus, it must have only non-dot things from the beginning to the end.
因此,它从头到尾必须只有非点的东西。
回答by chue x
Now, just to understand how ls regex works, could someone please explain to me how this would be [done] by using only ls?
现在,只是为了了解 ls regex 是如何工作的,有人可以向我解释如何仅使用 ls 来[完成]吗?
You could do it with the ignore flag:
您可以使用忽略标志来做到这一点:
ls -I '*.*'
Note - works on CentOS 6, not sure about other Linux distributions.
注意 - 适用于 CentOS 6,不确定其他 Linux 发行版。
回答by glenn Hymanman
Since you tagged the question with bash, you don't need grep:
由于您使用bash标记了问题,因此您不需要 grep:
shopt -s extglob
(cd testfiles && ls -d !(*.*))
see http://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching
见http://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching

