使用多个命令查找 -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 {} ;
这篇关于使用多个命令查找 -exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!