当 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:54:17  来源:igfitidea点击:

List files matching pattern when too many for bash globbing

bashfindls

提问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 findin combination with xargswill help.

这是find结合使用xargs将有所帮助的地方。

find /path/to/files -name "pattern*" -print0 | xargs -0 ls

Note from comments:xargswill 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 findshould suffice. However, if you wish to copy, deleteor perform any action on the list, then using xargsinstead of -execwill help.

评论中的注意事项:xargs如果您希望从find. 如果您只想列出文件,那么find应该足够了。但是,如果您希望copydelete或对列表执行任何操作,则使用xargs而不是-exec会有所帮助。