如何将当前目录和子目录中的所有文件附加为命令参数?

发布于 2024-08-13 21:10:27 字数 434 浏览 5 评论 0原文

我有一个像这样的目录:

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 like
kate 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亚希 2024-08-20 21:10:27

为此有几种可能的选择。这些答案基于您的场景,您知道 kate 可以打开所有文件,并且您想要打开具有任何扩展名的文件。

find dir -type f -exec kate {} +  

kate $(find dir -type f)

kate `find dir -type f`

第二种和第三种形式几乎是等价的。主要区别[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.

find dir -type f -exec kate {} +  

kate $(find dir -type f)

kate `find dir -type f`

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.

陪你搞怪i 2024-08-20 21:10:27

尝试

kate `find . -name \*.txt -type f`

使用 -type f 阻止您访问目录。

下面是一个使用 ls -1 而不是 kate 的示例:

edd@ron:~/src/debian/R$ ls -1 `find . -type f -name \*.txt`
./R-2.10.0/src/extra/graphapp/readme.txt
./R-2.10.0/src/extra/xdr/copyrght.txt
./R-2.10.0/src/extra/xdr/README.txt
./R-2.10.0/src/gnuwin32/fixed/etc/rgb.txt
./R-2.10.0/src/gnuwin32/installer/CustomMsg.txt
./R-2.10.0/src/library/grid/inst/doc/changes.txt
./R-2.10.0/src/unix/system.txt
./R-2.9.2-ra-1.2.8/src/extra/graphapp/readme.txt
./R-2.9.2-ra-1.2.8/src/extra/xdr/copyrght.txt
./R-2.9.2-ra-1.2.8/src/extra/xdr/README.txt
./R-2.9.2-ra-1.2.8/src/gnuwin32/fixed/etc/rgb.txt
./R-2.9.2-ra-1.2.8/src/gnuwin32/installer/CustomMsg.txt
./R-2.9.2-ra-1.2.8/src/library/grid/inst/doc/changes.txt
./R-2.9.2-ra-1.2.8/src/unix/system.txt
edd@ron:~/src/debian/R$

如果您确实想要子目录中的所有文件,则调用会简化为

kate `find . -type f`

如果您位于 dir/ 否则

kate `find dir -type f`

Try

kate `find . -name \*.txt -type f`

where the -type f prevents you from hitting directories.

Here is an example using ls -1 instead of kate:

edd@ron:~/src/debian/R$ ls -1 `find . -type f -name \*.txt`
./R-2.10.0/src/extra/graphapp/readme.txt
./R-2.10.0/src/extra/xdr/copyrght.txt
./R-2.10.0/src/extra/xdr/README.txt
./R-2.10.0/src/gnuwin32/fixed/etc/rgb.txt
./R-2.10.0/src/gnuwin32/installer/CustomMsg.txt
./R-2.10.0/src/library/grid/inst/doc/changes.txt
./R-2.10.0/src/unix/system.txt
./R-2.9.2-ra-1.2.8/src/extra/graphapp/readme.txt
./R-2.9.2-ra-1.2.8/src/extra/xdr/copyrght.txt
./R-2.9.2-ra-1.2.8/src/extra/xdr/README.txt
./R-2.9.2-ra-1.2.8/src/gnuwin32/fixed/etc/rgb.txt
./R-2.9.2-ra-1.2.8/src/gnuwin32/installer/CustomMsg.txt
./R-2.9.2-ra-1.2.8/src/library/grid/inst/doc/changes.txt
./R-2.9.2-ra-1.2.8/src/unix/system.txt
edd@ron:~/src/debian/R$

and if you really want all files in subdirectories the call simplifies to

kate `find . -type f`

if you are in dir/ or else

kate `find dir -type f`
不必你懂 2024-08-20 21:10:27

凯特 $(查找目录 -type f)

kate $(find dir -type f)

别靠近我心 2024-08-20 21:10:27

您要么不太熟悉 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 the find command and voila, it behaves like xargs.

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). Both find ... + and xargs get around these limitations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文