What if you have been working on a large software project and now it is made mandatory to have File Banners on all files? What will you do? Isn't it a tedious process to go to all files and paste the banner, what if you have 98 files?
One method is...$cat banner.txt > tmp.txt
$echo "" >> tmp.txt
$cat file1.c >> tmp.txt
$cp tmp.txt file1.c
You will have to do this for all files one by one. No, you don't have to... xargs come to the rescue...
Here is a small shell script I made to make life much easier.{ cat banner ;
echo -e "" ;
cat $1
} > tmp.txt
mv tmp.txt $1
Save this as banner.sh.
Now what?$find -iname *.c | xargs -i bash banner.sh {}
This will search the directory tree for all .c files and add banner to those files.
- find : searches for all .c files
- xargs : sends the text it gets from left one line at a time to the command given as argument to it. Here it takes one file at a time and gives to bash banner.sh {}. The {} is replaced by the input when -i is used. So, if the file is file1.c, then the command executed is bash banner.sh file1.c.
If you need to add footer also, just modify the script file.
Any doubts, feel free to ask.
Tags: unix shell tips CLI


2 comments:
Isn't cat supposed to be used to conCATtonate files, like so:
cat banner.txt file1.c > tmp.txt
mv tmp.txt file1.c
True, you have to put that extra line in your banner.txt, but this way you're only running 2 programs (cat then mv) instead of 4 (cat, echo, cat, mv). I'd imagine it would improve the efficiency of the whole process when you're dealing with a large number of files.
Just thought you'd like to know.
Hi nova,
I haven't thought about that, thank you...
Post a Comment