Gé Custers
Gé Custers

Reputation: 31

find a .txt file and write in it

I was wondering if it is possible to find multiple .txt files in a directory and its sub-directories and write something in it. something like

find . -depth -type d -path "/path/to/files/*.txt" -exec echo > *.txt;

I managed to create the files like this but now I would like to write in all of them.

thanks in advance.

Upvotes: 2

Views: 774

Answers (1)

MacMartin
MacMartin

Reputation: 2866

I want to answer with an example:

To write 'blablubb' to each file (and subdirectories) in:

me@my:/tmp$ ls /tmp/*txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/0txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/1txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/2txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/3txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/4txt

You can write wtih find ... -exec ... or xargs:

me@my:/tmp$ find . -maxdepth 2 -regex '.*txt' | xargs -I{} sh -c "echo 'blablubb' >> {}" 

me@my:/tmp$ find . -maxdepth 1 -regex '.*txt' -exec cat {} \;
blablubb
blablubb
blablubb
blablubb
blablubb

more can be find here: https://unix.stackexchange.com/questions/22229/xargs-with-stdin-stdout-redirection

Upvotes: 2

Related Questions