codefx codefx - 1 month ago 7
Linux Question

How do I add lines to every file inside a folder?

I am trying to add license declaration to all files inside a folder. Is there any shortcuts / linux commands to do that? If this can be done to files of a specific extension that will be even better.

Edit: This was my final command based on @vimsha's answer:


find . -type f -name "*.js" -exec sh -c 'echo "/* @flow */\n$(cat $0)" > $0' {} \;

Answer

cd into the folder and execute the following command

find *.txt -exec sh -c 'echo "license " >> $0' {} \;

Above command finds all files with 'txt' extension and appends the string "license" to them. You can change it to the extension you want and the text you want to append

To add to the beginning of the file:

find *.txt -exec sh -c 'echo -e "license\n$(cat $0)" > $0' {} \;