Reputation: 23
I have the directory structure as shown in the image below. Can there be a script written in linux which basically traverses everything under the sub directories and adds a line of text in the beginning of the file having extension as .txt
there are multiple folders but all have a file having extension .txt
Upvotes: 2
Views: 793
Reputation: 838
Just in case sed -i
switch is not there.
for file in `find Root -type f -name "*.txt"`; do sed '1i\
this is the line
' $file > ${file}_new; done
Upvotes: 2
Reputation: 782148
find Root -type f -name '*.txt' -exec sed -i '1i\
line to insert
' {} +
The find
command will recurse from the Root
directory, looking for filenames that match *.txt
. It will then execute the sed
command, which inserts a line at the beginning of the file.
Upvotes: 3