find -exec 具有多个命令
我尝试将 find -exec 与多个命令一起使用,但没有成功。有谁知道以下命令是否可行?
find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;
基本上,我试图打印当前目录中每个 txt 文件的最后一行,并在行末尾打印一个逗号,后跟文件名。
I am trying to use find -exec with multiple commands without any success. Does anybody know if commands such as the following are possible?
find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;
Basically, I am trying to print the last line of each txt file in the current directory and print at the end of the line, a comma followed by the filename.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
find
接受命令的多个-exec
部分。例如:请注意,在这种情况下,只有第一个命令成功返回时,第二个命令才会运行,正如 @Caleb 提到的。如果您希望这两个命令无论成功或失败都运行,您可以使用以下构造:
find
accepts multiple-exec
portions to the command. For example:Note that in this case the second command will only run if the first one returns successfully, as mentioned by @Caleb. If you want both commands to run regardless of their success or failure, you could use this construct:
以下之一:
One of the following:
另一种方法是这样的:
在一行中
multiple_cmd()
” - 是一个函数export -f multiple_cmd
” - 将导出它,以便任何其他子 shell 都可以看到它find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;
" - find 将在您的示例中执行该函数这样, multiple_cmd 可以像这样一样长和复杂,你需要。
Another way is like this:
in one line
multiple_cmd()
" - is a functionexport -f multiple_cmd
" - will export it so any other subshell can see itfind *.txt -exec bash -c 'multiple_cmd "$0"' {} \;
" - find that will execute the function on your exampleIn this way multiple_cmd can be as long and as complex, as you need.
有一个更简单的方法:
或者:
There's an easier way:
Alternatively:
扩展@Tinker的答案,
就我而言,我需要创建一个命令 |命令 |
-exec
中的命令 打印文件名以及在包含特定文本的文件中找到的文本。我能够做到这一点:
结果是:
Extending @Tinker's answer,
In my case, I needed to make a
command | command | command
inside the-exec
to print both the filename and the found text in files containing a certain text.I was able to do it with:
the result is:
我不知道是否可以使用 find 来完成此操作,但替代解决方案是创建一个 shell 脚本并使用 find 来运行它。
lastline.sh:
使脚本可执行
使用
find
:I don't know if you can do this with find, but an alternate solution would be to create a shell script and to run this with find.
lastline.sh:
Make the script executable
Use
find
:感谢卡米洛·马丁(Camilo Martin),我能够回答一个相关问题:
我想做的是
不起作用。不过,
确实有效,所以谢谢!
Thanks to Camilo Martin, I was able to answer a related question:
What I wanted to do was
which didn't work. However,
does work, so thank you!
丹尼斯的第一个回答是解决问题的答案。但事实上,它不再是像标题所暗示的那样,在一个
-exec
中使用多个命令进行find
。要回答一个带有多个命令的-exec
问题,我们必须寻找其他东西来解决。以下是一个示例:使用多个
{}
引用使用一个-exec
命令保留过去 7 天内修改的最后 10000 行 .log 文件See该命令将对哪些文件执行什么操作:
执行(注意不再有
\>
,但这次只有>
):The first answer of Denis is the answer to resolve the trouble. But in fact it is no more a
find
with several commands in only one-exec
like the title suggests. To answer the one-exec
with several commands thing we will have to look for something else to resolve. Here is a example:Keep last 10000 lines of .log files which has been modified in the last 7 days using one
-exec
command using several{}
referencesSee what the command will do on which files:
Do it (note no more
\>
but only>
this time):我通常将
find
嵌入到一个小的for
循环中,其中 find 在带有$()
的子命令中执行。那么您的命令将如下所示:
好处是您只需使用
$f
而不是{}
而不是-exec …
您将所有命令写在do
和之间;完成
。不确定你真正想做什么,但也许是这样的?
I usually embed the
find
in a smallfor
loop one liner, where the find is executed in a subcommand with$()
.Your command would look like this then:
The good thing is that instead of
{}
you just use$f
and instead of the-exec …
you write all your commands betweendo
and; done
.Not sure what you actually want to do, but maybe something like this?
我找到了这个解决方案(也许它已经在评论中说过,但我找不到任何答案)
您可以使用“bash -c”连续执行多个命令,
在您的情况下
我用测试文件测试了它:
I found this solution (maybe it is already said in a comment, but I could not find any answer with this)
you can execute MULTIPLE COMMANDS in a row using "bash -c"
in your case
i tested it with a test file:
find+xargs
答案。下面的示例查找所有
.html
文件并创建一个附加了.BAK
扩展名的副本(例如1.html
>1 .html.BAK
)。具有多个占位符的单个命令 具有
多个占位符的多个命令
由于参数引用和
--,此命令还可以处理以连字符开头或包含空格(例如
在-my file.html
)的文件cp
之后,向cp
发出参数结束和实际文件名开始的信号。A
find+xargs
answer.The example below finds all
.html
files and creates a copy with the.BAK
extension appended (e.g.1.html
>1.html.BAK
).Single command with multiple placeholders
Multiple commands with multiple placeholders
This command will also work with files that start with a hyphen or contain spaces such as
-my file.html
thanks to parameter quoting and the--
aftercp
which signals tocp
the end of parameters and the beginning of the actual file names.应该使用 xargs :)
另一个(在 osx 上工作)
should use xargs :)
another one (working on osx)
这是我的 bash 脚本,您可以使用它来查找多个文件,然后使用命令处理所有文件。
使用示例。此命令将
file
linux 命令应用于每个找到的文件:Finder 脚本:
Here is my bash script that you can use to find multiple files and then process them all using a command.
Example of usage. This command applies a
file
linux command to each found file:Finder script: