当 bash globbing 太多时,列出匹配模式的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17382190/
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
List files matching pattern when too many for bash globbing
提问by DavidR
I'd like to run the following:
我想运行以下命令:
ls /path/to/files/pattern*
and get
并得到
/path/to/files/pattern1
/path/to/files/pattern2
/path/to/files/pattern3
However, there are too many files matching the pattern in that directory, and I get
但是,该目录中匹配模式的文件太多,我得到
bash: /bin/ls: Argument list too long
What's a better way to do this? Maybe using the find command? I need to print out the full paths to the files.
有什么更好的方法来做到这一点?也许使用 find 命令?我需要打印出文件的完整路径。
回答by jaypal singh
This is where find
in combination with xargs
will help.
这是find
结合使用xargs
将有所帮助的地方。
find /path/to/files -name "pattern*" -print0 | xargs -0 ls
Note from comments:xargs
will help if you wish to do with the list once you have obtained it from find
. If you only intend to list the files, then find
should suffice. However, if you wish to copy
, delete
or perform any action on the list, then using xargs
instead of -exec
will help.
评论中的注意事项:xargs
如果您希望从find
. 如果您只想列出文件,那么find
应该足够了。但是,如果您希望copy
,delete
或对列表执行任何操作,则使用xargs
而不是-exec
会有所帮助。