exec with multiple commands

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.


find accepts multiple -exec portions to the command. For example:

find . -name "*.txt" -exec echo {} ; -exec grep banana {} ;

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 . -type d -exec sh -c "echo -n {}; echo -n ' x '; echo {}" ;

以下之一:

find *.txt -exec awk 'END {print $0 "," FILENAME}' {} ;

find *.txt -exec sh -c 'echo "$(tail -n 1 "$1"),$1"' _ {} ;

find *.txt -exec sh -c 'echo "$(sed -n "$p" "$1"),$1"' _ {} ;
链接地址: http://www.djcxy.com/p/13720.html

上一篇: 递归计数Linux目录中的文件

下一篇: exec使用多个命令