【技巧】批量向多个文件追加相同信息
一. 问题背景
我们经常会有一个想要创建多个文件并且每个文件要有相同的内容。
二. 解决方案
方案1. tee方法(推荐)
tee用法:把标准输入的内容(例如:从管道前面过来的)传递到标准输出(屏幕)和写入到文件中
准备环境
[root@oldboyedu-show01 /oldboy]# echo 'oldboyedu.com QQ group:598972270' >oldboy.txt
[root@oldboyedu-show01 /oldboy]# cat oldboy.txt
oldboyedu.com QQ group:598972270
进行测试
显示出oldboy.txt内容通过tee显示到屏幕和写入到文件中(可以是1个文件或多个文件都行)
[root@oldboyedu-show01 /oldboy]# cat oldboy.txt|tee oldboy01.txt oldboy02.txt
oldboyedu.com QQ group:598972270
[root@oldboyedu-show01 /oldboy]# head oldboy01.txt oldboy02.txt
==> oldboy01.txt <==
oldboyedu.com QQ group:598972270
==> oldboy02.txt <==
oldboyedu.com QQ group:598972270
批量写入到多个文件中(同时创建文件)
[root@oldboyedu-show01 /oldboy]# cat oldboy.txt|tee oldboy{03..10}.txt
oldboyedu.com QQ group:598972270
[root@oldboyedu-show01 /oldboy]# ls -l
total 44
-rw-r--r-- 1 root root 33 Apr 10 21:22 oldboy01.txt
-rw-r--r-- 1 root root 33 Apr 10 21:22 oldboy02.txt
-rw-r--r-- 1 root root 33 Apr 10 21:23 oldboy03.txt
-rw-r--r-- 1 root root 33 Apr 10 21:23 oldboy04.txt
-rw-r--r-- 1 root root 33 Apr 10 21:23 oldboy05.txt
-rw-r--r-- 1 root root 33 Apr 10 21:23 oldboy06.txt
-rw-r--r-- 1 root root 33 Apr 10 21:23 oldboy07.txt
-rw-r--r-- 1 root root 33 Apr 10 21:23 oldboy08.txt
-rw-r--r-- 1 root root 33 Apr 10 21:23 oldboy09.txt
-rw-r--r-- 1 root root 33 Apr 10 21:23 oldboy10.txt
-rw-r--r-- 1 root root 33 Apr 10 21:22 oldboy.txt
[root@oldboyedu-show01 /oldboy]# head *.txt
==> oldboy01.txt <==
oldboyedu.com QQ group:598972270
==> oldboy02.txt <==
oldboyedu.com QQ group:598972270
==> oldboy03.txt <==
oldboyedu.com QQ group:598972270
==> oldboy04.txt <==
oldboyedu.com QQ group:598972270
==> oldboy05.txt <==
oldboyedu.com QQ group:598972270
==> oldboy06.txt <==
oldboyedu.com QQ group:598972270
==> oldboy07.txt <==
oldboyedu.com QQ group:598972270
==> oldboy08.txt <==
oldboyedu.com QQ group:598972270
==> oldboy09.txt <==
oldboyedu.com QQ group:598972270
==> oldboy10.txt <==
oldboyedu.com QQ group:598972270
==> oldboy.txt <==
oldboyedu.com QQ group:598972270
温馨提示:
tee默认写入到文件是重定向到文件(类似“>”),会清空源文件内容
如果想通过tee进行追加 加上-a(--append)参数即可
方案2. for循环方法
此方法方便 容易理解
用法:
for 变量名字 in 列表
do
执行的命令
done
for n in {01..6}
do
echo "周$n带李导去大保健"
done
[root@oldboyedu-show01 /oldboy]# for n in {01..6}
> do
> echo "周$n带李导去大保健"
> done
周01带李导去大保健
周02带李导去大保健
周03带李导去大保健
周04带李导去大保健
周05带李导去大保健
周06带李导去大保健
for n in {01..10}
do
echo oldboyedu.com $n >lidao$n.txt
done
[root@oldboyedu-show01 /oldboy]# for n in {01..10}
> do
> echo oldboyedu.com $n >lidao$n.txt
> done
[root@oldboyedu-show01 /oldboy]# head lidao*.txt
==> lidao01.txt <==
oldboyedu.com 01
==> lidao02.txt <==
oldboyedu.com 02
==> lidao03.txt <==
oldboyedu.com 03
==> lidao04.txt <==
oldboyedu.com 04
==> lidao05.txt <==
oldboyedu.com 05
==> lidao06.txt <==
oldboyedu.com 06
==> lidao07.txt <==
oldboyedu.com 07
==> lidao08.txt <==
oldboyedu.com 08
==> lidao09.txt <==
oldboyedu.com 09
==> lidao10.txt <==
oldboyedu.com 10
方案3. find实现
注意:此方法需要文件已经存在
-exec的常用格式为 -exec 命令 {} ;
-exec | 命令 | {} | ; |
---|---|---|---|
-exec | 命令 | {}表示前面find命令找到的文件 | ;结束标记 |
这里使用bash -c 是创建一个shell通道用来运行echo oldboyedu 追加到 {}(find找出的文件中)
算是特殊用法了了解下即可
如果不用find . -type f -name "lidao*.txt" -exec echo oldboyedu >> {} \;
这样会创建出一个叫“{}”的文件 oldboyedu会被追加到这个文件中
[root@oldboyedu-show01 /oldboy]# find . -type f -name "lidao*.txt" -exec bash -c "echo oldboyedu >> {}" \;
[root@oldboyedu-show01 /oldboy]# cat lidao01.txt
oldboyedu.com 01
oldboyedu
[root@oldboyedu-show01 /oldboy]# head lidao*.txt
==> lidao01.txt <==
oldboyedu.com 01
oldboyedu
==> lidao02.txt <==
oldboyedu.com 02
oldboyedu
==> lidao03.txt <==
oldboyedu.com 03
oldboyedu
==> lidao04.txt <==
oldboyedu.com 04
oldboyedu
==> lidao05.txt <==
oldboyedu.com 05
oldboyedu
==> lidao06.txt <==
oldboyedu.com 06
oldboyedu
==> lidao07.txt <==
oldboyedu.com 07
oldboyedu
==> lidao08.txt <==
oldboyedu.com 08
oldboyedu
==> lidao09.txt <==
oldboyedu.com 09
oldboyedu
==> lidao10.txt <==
oldboyedu.com 10
oldboyedu
三. 总结
-
方法1和2是必须会的
-
方法3是特殊技巧了解即可
-
其他没有提到的方法欢迎补充。