Bash: How to add a line on top of text files with sed

Sometimes you might need to add one or more lines of text to the top of an existing text file. Let ‘sed’ rescue you:

sed -i '1iSTUFF TO ADD' file.txt

You can add multiple lines (separated by \n) to multiple files at once:

sed -i '1iSTUFF TO ADD\nMORE STUFF' *.txt

Comments are closed.