一次编辑linux中子目录中的所有类似文件

时间:2015-10-15 02:54:06

标签: linux bash shell unix

我有目录结构,如下图所示。 可以有一个用linux编写的脚本,基本上遍历子目录下的所有内容,并在文件开头添加一行文本,扩展名为.txt

有多个文件夹,但都有一个扩展名为.txt的文件

enter image description here

2 个答案:

答案 0 :(得分:2)

find Root -type f -name '*.txt' -exec sed -i '1i\
line to insert
' {} +

find命令将从Root目录递归,查找与*.txt匹配的文件名。然后它将执行sed命令,该命令在文件的开头插入一行。

答案 1 :(得分:1)

以防sed -i切换不存在。

for file in `find Root -type f -name "*.txt"`; do sed '1i\
this is the line
' $file > ${file}_new; done