[英]Linux: How to list all files/directories, using only ls
This is a question given at university. 这是大学提出的问题。
The question is: List all files/directories inside testfiles
folder, that have no extension. 问题是:列出
testfiles
文件夹中没有扩展名的所有文件/目录。
The right answer given is this: 给出的正确答案是:
ls testfiles | grep -v "\."
Now, just to understand how ls
regex works, could someone please explain to me how this would be by using only ls
? 现在,只是为了理解
ls
正则表达式如何工作,有人可以通过仅使用ls
向我解释这是怎么回事吗? Moreover, I would appreciate any example that also uses the dollar sign $
, to specifically state that the name ends with .[az]
. 此外,我会感谢任何也使用美元符号
$
例子,具体说明该名称以.[az]
结尾。
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! 我希望有人在此纠正我...谢谢!
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 "\\."
但你确实理解
grep "\\."
matches everything with a "period" (hence, extension), and grep -v "\\."
用“句号”(因此,扩展名)和
grep -v "\\."
匹配所有内容grep -v "\\."
matches everything else (ie, the complement). 匹配其他一切(即补充)。
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 "^[^.]*$"
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.
第二个插入符号是“非”符号。
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. 因此,它必须从头到尾只有非点的东西。
Now, just to understand how ls regex works, could someone please explain to me how this would be [done] by using only ls?
现在,只是为了理解ls正则表达式是如何工作的,有人可以通过仅使用ls向我解释这将是如何[完成]的吗?
You could do it with the ignore flag: 您可以使用ignore标志执行此操作:
ls -I '*.*'
Note - works on CentOS 6, not sure about other Linux distributions. 注 - 适用于CentOS 6,不确定其他Linux发行版。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.