Linux write something across multiple files

I have a file "atest.txt" which has text.

I want to print this text in files "asdasd.txt asgfaya.txt asdjfusfdgh.txt asyeiuyhavujh.txt"

These files do not exist on my server.

I am running Debian .. What can I do?

0


source to share


4 answers


Use the tee (1) command , which duplicates your standard input to standard output and any files specified on the command line. For example.

printf "Hello \ nthis is a test \ nthank you \ n"
  | tee test1.txt test2.txt $ OTHER_FILES> / dev / null


Using your example:

cat atest.txt
  | tee asdasd.txt asgfaya.txt asdjfusfdgh.txt asyeiuyhavujh.txt> / dev / null
+6


source


In bash, you can write

#!/bin/bash
$TEXT="hello\nthis is a test\nthank you"
for i in `seq 1 $1`; do echo -e $TEXT >text$i.txt; done

      

EDIT (in response to changing the question)

If you are unable to programmatically determine the names of the target files, you can use this script it:



#!/bin/bash
ORIGIN=$1;
shift
for i in `seq $#`; do cp "$ORIGIN" "$1"; shift; done

      

you can use it like this:
script_name origin_file dest_file1 second_dest_file 'third file' ...

If you are wondering why there are double quotes in the cp command, this is for reference with a filename containing spaces

+1


source


From a bash prompt:

for f in test1.txt test2.txt test3.txt; do echo -e "hello\nworld" >> $f; done

      

If the text lives in atest.txt then do the following:

for f in test1.txt test2.txt test3.txt; do cat atest.txt >> $f; done

      

+1


source


Isn't it easy:

cp atest.txt asdasd.txt 
cp atest.txt asgfaya.txt
cp atest.txt asdjfusfdgh.txt
cp atest.txt asyeiuyhavujh.txt

      

?

+1


source







All Articles