find -exec 与多个命令 [英] find -exec with multiple commands
问题描述
我试图将 find -exec 与多个命令一起使用,但没有成功.有人知道以下命令是否可行?
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 '{}')" ;
基本上,我试图打印当前目录中每个 txt 文件的最后一行,并在行尾打印一个逗号,后跟文件名.
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.
推荐答案
find
接受命令的多个 -exec
部分.例如:
find
accepts multiple -exec
portions to the command. For example:
find . -name "*.txt" -exec echo {} ; -exec grep banana {} ;
请注意,在这种情况下,只有在第一个命令成功返回时才会运行第二个命令,如@Caleb 所述.如果您希望两个命令无论成功或失败都运行,您可以使用以下结构:
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:
find . -name "*.txt" ( -exec echo {} ; -o -exec true ; ) -exec grep banana {} ;
这篇关于find -exec 与多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!