List all files not starting with a number

≯℡__Kan透↙ 提交于 2019-12-21 12:13:07

问题


I want to examine the all the key files present in my /proc. But /proc has 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 regex that I need to send to ls to make it NOT to search files/directories which have numbers in their name?

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


回答1:


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

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

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

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

But this 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:

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

Hold on again! Doesn't this mean that any regular files created by touch /proc/123 or 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:

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



回答2:


You don't need grep, just ls:

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

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

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



回答3:


Use grep with -v which tells it to print all lines not matching the pattern.

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



回答4:


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




回答5:


Following regex matches all the characters except numbers

^[\D]+?$

Hope it helps !




回答6:


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

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


来源:https://stackoverflow.com/questions/9515263/list-all-files-not-starting-with-a-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!