Linux 列出所有不以数字开头的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9515263/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:55:53  来源:igfitidea点击:

List all files not starting with a number

regexlinuxshellls

提问by Pavan Manjunath

I want to examine the all the key files present in my /proc. But /prochas innumerable directories corresponding to the running processes. I don't want these directories to be listed. All these directories' names contain only numbers. As I am poor in regular expressions, can anyone tell me whats the regexthat I need to send to lsto make it NOTto search files/directories which have numbers in their name?

我想检查我的/proc. 但是/proc有无数的目录对应于正在运行的进程。我不想列出这些目录。所有这些目录的名称都只包含数字。由于我在正则表达式方面很差,有人能告诉我regex我需要发送什么ls来使它搜索名称中包含数字的文件/目录吗?

UPDATE: Thanks to all the replies! But I would love to have a lsalone solution instead of ls+grepsolution. The lsalone solutions offered till now doesn't seem to be working!

更新:感谢所有回复!但我希望有一个ls单独的解决方案而不是ls+grep解决方案。ls到目前为止提供的单独解决方案似乎不起作用!

采纳答案by l0b0

All files and directories in /procwhich do not contain numbers (in other words, excluding process directories):

所有/proc不包含数字的文件和目录(换句话说,不包括进程目录):

ls -d /proc/[^0-9]*

All files recursively under /procwhich do not start with a number:

所有/proc不以数字开头的递归文件:

find /proc -regex '.*/[0-9].*' -prune -o -print

Butthis will also exclude numeric files in subdirectories(for example /proc/foo/bar/123). If you want to exclude only the top-level files with a number:

但这也将排除子目录中的数字文件(例如/proc/foo/bar/123)。如果您只想排除带有数字的顶级文件:

find /proc -regex '/proc/[0-9].*' -prune -o -print

Hold on again! Doesn't this mean that any regular filescreated by touch /proc/123or the like will be excluded? Theoretically yes, but I don't think you can do that. Try creating a file for a PID which does not exist:

再坚持一下!这是否意味着将排除由之类创建的任何常规文件touch /proc/123?理论上是的,但我认为你做不到。尝试为不存在的 PID 创建文件:

$ sudo touch /proc/123
touch: cannot touch `/proc/123': No such file or directory

回答by Kimvais

Use grep with -vwhich tells it to print all lines not matchingthe pattern.

使用 grep-v告诉它打印所有与模式不匹配的行。

 ls /proc | grep -v '[0-9+]'

回答by Shekhar

Following regex matches all the characters except numbers

以下正则表达式匹配除数字以外的所有字符

^[\D]+?$

Hope it helps !

希望能帮助到你 !

回答by rkhayrov

ls /proc | grep -v -E '[0-9]+'

ls /proc | grep -v -E '[0-9]+'

回答by Mithrandir

You don't need grep, just ls:

您不需要 grep,只需ls

ls -ad /proc/[^0-9]*

if you want to search the whole subdirectory structure use find:

如果要搜索整个子目录结构,请使用 find:

find /proc/ -type f -regex "[^0-9]*" -print

回答by Jayan

For the sake of of completion. You may apply Mithandir's answer with find.

为了完成。您可以通过 find 应用 Mithandir 的答案。

  find . -name "[^0-9]*" -type f