Linux prepend content to a file

Prepend content to a file

Use sed with -i option. You can also specify the line to which the content will prepend to.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed -i '' '1s/^/package home;/" Home.java
sed -i '' '1s/^/package home;/" Home.java
sed -i '' '1s/^/package home;/" Home.java

 

Prepend content to multiple files

Here I need to add a package to 200 java files. To do this manual would be tedios. 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
for f in *.java
do
sed -i "" "1s/^/package home;/" $f
done
for f in *.java do sed -i "" "1s/^/package home;/" $f done
for f in *.java
do
  sed -i "" "1s/^/package home;/" $f 
done

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *