简体   繁体   English

Linux:如何仅使用ls列出所有文件/目录

[英]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 , you don't need grep: 由于您使用标记了问题,因此您不需要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.

相关问题 在zsh中使用`ls`隐藏所有以大写字母开头的目录 - Hiding all directories that begin with a capitalized letter using `ls` in zsh 如何仅查找 ls 名称中大写的文件? - How to find ls only files with uppercases in name? Unix - 使用find列出所有.html文件。 (不要使用shell通配符或ls命令) - Unix - Using find to List all .html files. (Do not use shell wildcards or the ls command) 列出在C#中使用正则表达式仅包含txt文件的目录 - list directories which has only txt files using regex in C# 如何使用ls列出以数字结尾的文件 - How to use ls to list out files that end in numbers 如何在多个不同目录中移动许多文件(在Linux上) - How to move many files in multiple different directories (on Linux) 在Unix / Linux目录/子目录中的任何位置获取与文件名中的模式匹配的所有文件 - Get all the files that match pattern in file name anywhere in unix/linux directories/subdirectories 如何修改此正则表达式,以便它只匹配目录并排除文件? - How to modify this regex so that it only matches directories and excludes files? 如何在find命令的额外-exec选项中表示所有文件/目录? - How to express all files/directories at the extra -exec option of `find` command? 根据Linux中的文件数重命名目录 - Rename directories based on number of files within in Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM