Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I am trying to work on the find -exec with multiple commands but I am not succeeding in it. Is the following command are possible?

find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;

I wanted to print the last line of each of my txt file in this current directory and print it at the end of the line, a comma followed by my filename.

1 Answer

0 votes
by (36.8k points)
edited by

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

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

Note that in the above case the 2nd command will only run if the 1st one, returns successfully, as mentioned by @Caleb. If you want both your  commands to run mindless of their success or failure, you could use this construct:

find . -name "*.txt" \( -exec echo {} \; -o -exec true \; \) -exec grep banana {} \;

Come join the Linux course and gain knowledge in Linux.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 30, 2020 in Linux by dev_sk2311 (45k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...