简体   繁体   English

列出具有数字名称的文件

[英]Listing files with numeric names

How do I list files that have only numeric names eg 如何列出仅具有数字名称的文件,例如

1111 
2342 
763
71

I have tried ls -l [0-9]* but this seems to bring all file names that start with a digit and can have anything in their name after a digit. 我已经尝试过ls -l [0-9]*但是这似乎带来了所有以数字开头的文件名,并且文件名后面可以有任何东西。

First, turn on Bash's extglob option: 首先,打开Bash的extglob选项:

shopt -s extglob

Then use this pattern: 然后使用以下模式:

ls -l +([0-9])

Read more about extended pattern matching in the Bash Reference Manual . Bash参考手册中了解有关扩展模式匹配的更多信息。

Using GNU find : 使用GNU find

find . -type f -regex './[0-9]*$'

This uses a regular expression rather than a filename globbing pattern. 这使用正则表达式而不是文件名遍历模式。 The expression ./[0-9]*$ matches ./ (the current directory) followed by only digits until the end ( $ ). 表达式./[0-9]*$匹配./ (当前目录),后跟仅数字,直到末尾( $ )。

If you need to stop find from going into subdirectories, add -maxdepth 1 . 如果您需要停止find子目录,请添加-maxdepth 1

If you want ls -l -like behaviour, add -ls at the very end. 如果您想要ls -l -like行为,请在最后添加-ls

With opposite operation : ignoring filenames with non-digits [^0-9] : 使用相反的操作:忽略非数字 [^0-9]文件名:

ls -1 --ignore=*[^0-9]*

--ignore=PATTERN --ignore =模式
do not list implied entries matching shell PATTERN 不列出与shell PATTERN匹配的隐式条目

This command will be safest option always! 此命令将永远是最安全的选择!

ls | grep -E '^[0-9]+$'

Adding few sample test cases for audiences to make it clear. 增加一些样本测试用例,以使读者清楚。

$ls
1       12222e      2       B.class     Test.java   file.sh     y.csv
1.txt       1a      A.class     Test.class  db_2017-08-15   file.txt
$ ls  | grep -E '^[0-9]+$'
1
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM