Ubuntu – Add a string to a text file from terminal

bashcommand line

I would like to modify a bunch of text files from the terminal, more precisey:

add the string '50' to the first line of every text file in /mydat/ ?

Alternatively, if you know of a link to a page on the web that lists commands to manipulate text files from the shell…

Best Answer

find and sed are your weapons of choice:

find /mydat/ -exec sed '1i 50' {} \;

That will stick 50 followed by a new line on the beginning of the file.

Alternatively if you don't need recursion or complex selectors for find you can drop find completely:

sed '1i 50' *
Related Question