我有一个包含一些文本的文件“atest.txt”..
我想在文件“asdasd.txt asgfaya.txt asdjfusfdgh.txt asyeiuyhavujh.txt”中打印此文本
我的服务器上不存在此文件..
我正在运行 Debian .. 我能做什么?
使用tee (1) 命令,它将其标准输入复制到标准输出和命令行上指定的任何文件。例如
printf "你好\n这是一个测试\n谢谢\n" | tee test1.txt test2.txt $OTHER_FILES >/dev/null
使用您的示例:
猫 atest.txt | tee asdasd.txt asgfaya.txt asdjfusfdgh.txt asyeiuyhavujh.txt >/dev/null
在 bash 你可以写
#!/bin/bash
$TEXT="hello\nthis is a test\nthank you"
for i in `seq 1 $1`; do echo -e $TEXT >text$i.txt; done
编辑(响应问题的变化)
如果您无法以编程方式确定目标文件的名称,则可以使用此脚本:
#!/bin/bash
ORIGIN=$1;
shift
for i in `seq $#`; do cp "$ORIGIN" "$1"; shift; done
你可以这样使用它:
script_name origin_file dest_file1 second_dest_file 'third file' ...
如果您想知道为什么 cp 命令中有双引号,这是为了处理包含空格的文件名
从您的 bash 提示符中:
for f in test1.txt test2.txt test3.txt; do echo -e "hello\nworld" >> $f; done
如果文本位于 atest.txt 中,则执行以下操作:
for f in test1.txt test2.txt test3.txt; do cat atest.txt >> $f; done
是不是很简单:
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 *