Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert copyright message into multiple files

How would you insert a copyright message at the very top of every file?

like image 351
Nick Vanderbilt Avatar asked Mar 15 '10 20:03

Nick Vanderbilt


1 Answers

#!/bin/bash
for file in *; do
  echo "Copyright" > tempfile;
  cat $file >> tempfile;
  mv tempfile $file;
done

Recursive solution (finds all .txt files in all subdirectories):

#!/bin/bash
for file in $(find . -type f -name \*.txt); do
  echo "Copyright" > copyright-file.txt;
  echo "" >> copyright-file.txt;
  cat $file >> copyright-file.txt;
  mv copyright-file.txt $file;
done

Use caution; if spaces exist in file names you might get unexpected behaviour.

like image 81
Paul Creasey Avatar answered Oct 11 '22 13:10

Paul Creasey