In our scenario we have a file called file1
with a following content:
$ cat file1 line 1 line 2 line 3
Next, we can use a sed
command to append a line “This is my first line” to the beginning to this file:
$ sed '1 s/^/This is my first line\n/' file1 This is my first line line 1 line 2 line 3
Use STDOUT redirection to save this file or include -i
sed option to save this file in place:
$ sed '1 s/^/This is my first line\n/' file1 > file2 $ cat file2 This is my first line line 1 line 2 line 3
Use for loop to insert a first line into every file within your current directory:
for i in $( ls * ); do sed -i '1 s/^/This is my first line\n/' $i; done