bash,查找文件名中包含数字的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2155673/
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
bash, find files which contain numbers on filename
提问by asdf
In bash, I would like to use the command findto find files which contain the numbers from 40 to 70 in a certain position like c43_data.txt. How is it possible to implement this filter in find?
在 bash 中,我想使用该命令find来查找在某个位置(如c43_data.txt )中包含从 40 到 70 的数字的文件。如何在 中实现此过滤器find?
I tried file . -name "c**_data.txt" | grep 4, but this is not very nice.
我试过了file . -name "c**_data.txt" | grep 4,但这不是很好。
Thanks
谢谢
采纳答案by danben
ls -R | grep -e 'c[4-7][0-9]_data.txt'
ls -R | grep -e 'c[4-7][0-9]_data.txt'
findcan be used in place of ls, obviously.
findls显然可以代替 使用。
回答by Nick Presta
Perhaps something like:
也许是这样的:
find . -regextype posix-egrep -regex "./c(([4-6][0-9])|70)_data.txt"
This matches 40 - 69, and 70.
这匹配 40 - 69 和 70。
You may also use the iregexoption for case-insensitive matching.
您还可以使用iregex不区分大小写匹配的选项。
回答by ghostdog74
$ ls
c40_data.txt c42_data.txt c44_data.txt c70_data.txt c72_data.txt c74_data.txt
c41_data.txt c43_data.txt c45_data.txt c71_data.txt c73_data.txt c75_data.txt
$ find . -type f \( -name "c[4-6][0-9]_*txt" -o -name "c70_*txt" -o -name "c[1-2][3-4]_*.txt" \) -print
./c43_data.txt
./c41_data.txt
./c45_data.txt
./c70_data.txt
./c40_data.txt
./c44_data.txt
./c42_data.txt
回答by Dancrumb
Try something like:
尝试类似:
find . -regextype posix-egrep -regex '.\*c([3-6][0-9]|70).\*'
with the appropriate refinements to limit this to the files you want
进行适当的改进以将其限制为您想要的文件

