简体   繁体   中英

Linux Write Something on multiple files

I have a file "atest.txt" that have some text..

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

This files is not exist on my server..

I'm running Debian.. What can i do?

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

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

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 of question change)

If you can't determine programmatically the names of the target files then 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 this way:
script_name origin_file dest_file1 second_dest_file 'third file' ...

If you are wondering why there are the double quotes into the cp command, it is for cope with filename containing spaces

From your 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:

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

Isn't it simply:

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

?

如果有人想将相同的内容写入 dir 中的所有文件:

printf 'your_text' | tee *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM