如何将当前目录和子目录中的所有文件附加为命令参数?
我有一个像这样的目录:
dir
dir/somefile.txt
dir/subdir/subsub/somefile2.txt
dir/subdir2/somefile.txt
我想在单个命令实例中打开所有子目录中的所有文件。我尝试使用 -exec 或 xargs 进行查找,但它们使用单独的命令实例打开每个文件。
基本上,我想要的东西最终会像 kate dir/somefile.txt dir/subdir/subsub/somefile2.txt dir/subdir2/somefile.txt
,但适用于任意数量的子目录中的任意数量的文件。我正在使用 bash,但任何脚本建议都可以。
澄清:我指的不仅仅是 .txt 文件,而是任何 ascii 文件(即 .php、.txt、.html 等)
I have a directory like this:
dir
dir/somefile.txt
dir/subdir/subsub/somefile2.txt
dir/subdir2/somefile.txt
and I want to open all the files in all the subdirectories in a single instance of a command. I was trying find with -exec, or xargs, but these open each file with a separate instance of a command.
Basically, I want something that ends up likekate dir/somefile.txt dir/subdir/subsub/somefile2.txt dir/subdir2/somefile.txt
, but for any number of files in any number of subdirectories. I'm using bash, but any script suggestions are fine.
clarification: I didn't just mean .txt files, but any ascii file (ie. .php, .txt, .html, etc..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为此有几种可能的选择。这些答案基于您的场景,您知道 kate 可以打开所有文件,并且您想要打开具有任何扩展名的文件。
第二种和第三种形式几乎是等价的。主要区别[1]是第一个版本将处理名称中包含空格的文件,而第二个和第三个版本则不会。
[1] 感谢您指出 NVRAM,我第一次发布答案时并没有意识到。
There are several possible options for this. These answers are based on your scenario where you know all files can be opened by kate, and you want to open files with any extension.
The second and third forms are almost equivalent. The main difference[1] is that the first version will handle files with whitespace in their name, while the second and third do not.
[1] Thanks for pointing this out NVRAM, I didn't realise when I first posted the answer.
尝试
使用
-type f
阻止您访问目录。下面是一个使用
ls -1
而不是kate
的示例:如果您确实想要子目录中的所有文件,则调用会简化为
如果您位于
dir/ 否则
Try
where the
-type f
prevents you from hitting directories.Here is an example using
ls -1
instead ofkate
:and if you really want all files in subdirectories the call simplifies to
if you are in
dir/
or else凯特 $(查找目录 -type f)
kate $(find dir -type f)
您要么不太熟悉 xargs,要么没有正确使用它,因为您想要做的正是 xargs 旨在解决的问题:给定一个任意长的字符串列表,在尽可能少的执行次数中将它们作为参数传递给程序,同时不超过程序可以采用的参数数量的系统限制。
您的
find ... -exec
是正确的,但这也可以解决。只需在find
命令末尾添加+
即可,瞧,它的行为类似于xargs
。上面使用
kate $(...)
(或带有反引号的等效项)的解决方案一般都不起作用(它们不支持带空格的文件名,如果列表文件很长)。find ... +
和xargs
都绕过了这些限制。You're either not very familiar with
xargs
, or you're not using it correctly, because what you're trying to do is exactly the problem xargs is designed to solve: Given an arbitrarily long list of strings, pass them as arguments to a program in as few executions as possible while not exceeding the system limits on the number of arguments a program can take.You're right for
find ... -exec
, but that can also be fixed. Just add+
at the end of thefind
command and voila, it behaves likexargs
.None of the solutions above that use
kate $(...)
(or the equivalent with backticks) work in general (they don't support file names with spaces, will not run at all if the list of files is very long). Bothfind ... +
andxargs
get around these limitations.