Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo to all files matching pattern

I have some files in some directory. For example:

/path/file1.out
/path/file2.out
/path/file3.out
/path/file4.out
/path/file5.out

Is it possible to write something to all of these files using echo with a single command? I would expect something like

echo "asd" > /path/file*.out

but it is not recognizing the glob.

like image 536
J. Doe Avatar asked Mar 07 '26 08:03

J. Doe


1 Answers

It is not possible to redirect to more files, you need to use a loop:

shopt -s nullglob
for file in /path/file*.out; do
   echo "foo" > "$file"
done

Or you can use tee that will output to stdout at the same time:

shopt -s nullglob
echo "foo" | tee /path/file*.out

Note that shopt -s nullglob is important so that you don't create an undesired file if there are no files matching your pattern.

like image 199
PesaThe Avatar answered Mar 09 '26 02:03

PesaThe