Raja G
Raja G

Reputation: 6633

how to use echo with find in bash?

I have 10 files. I can list them with find . -type f and what I am trying to achieve is sending a message to all 10 files after finding them with find command.

What I have tried, find . -type f -exec echo "This file found" >> {} \;

May be logically I am right but its not working. Is there any way I can achieve with using find and echo only ?

Thank you

Upvotes: 6

Views: 15521

Answers (1)

heemayl
heemayl

Reputation: 42007

The shell redirection, >> is being done at first, a file named {} is being created before even the find starts and the strings (the number of files are in there) are being written to the file {}.

You need:

find . -type f -exec bash -c 'echo "This file found" >>"$1"' _ {} \;

Upvotes: 12

Related Questions