...Because someone's gotta tell the story...


To return to the main Blog List, click Full Blog Listing.


Site Updates

Linux Tricks: Echoing into multiple files

Thursday, January 03, 2019 in Linux (Views: 648)
So, in previous blogs, we talked about creating files using touch, even using touch to create multiple files by wildcard.

But if you want to add text to multiple files with a single command?

For this, we use the tee command...

Let's use those wildcarding principles:
First, create files...
[breakglass@learn a]$ touch {a..c}{1..3}
[breakglass@learn a]$ ls
a1 a2 a3 b1 b2 b3 c1 c2 c3

Now, to only add text to the files 1 and 2, but leave 3 alone...

Use this command:
echo "I have written to this file" | tee {a..c}{1..2}
You can use tee -a to append to these files as well.

Now, for the output, looking at a1 to a3
Notice a1 and a2 were touched, but not a3:
[breakglass@learn a]$ cat a1
I have written to this file
[breakglass@learn a]$ cat a2
I have written to this file
[breakglass@learn a]$ cat a3
[breakglass@learn a]$

Now, let's look at the c1, c2, and c3 files for the same behavior.
[breakglass@learn a]$ cat c1
I have written to this file
[breakglass@learn a]$ cat c2
I have written to this file
[breakglass@learn a]$ cat c3
[breakglass@learn a]$

Quickly, with a single command, you have written to multiple files.


Related Blogs You May Be Interested In:

To leave a comment, please log in and/or register.