Add a string to a text file from terminal
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' *
To edit a file, you need an editor. ed
and ex
are examples of command based editors, which is useful for editing files from a script. Here's an example inserting a line to every file with .txt extension in /mydata, using ed:
#!/bin/bash
for file in /mydata/*.txt; do
printf '%s\n' 0a 50 . w | ed -s "$file"
done
That'll handle all kinds of odd characters in the filenames too, unlike all the examples using for
-loops with ls
in the answers given so far.
Here's a link describing how to use ed: http://bash-hackers.org/wiki/doku.php?id=howto:edit-ed
For getting to grips with bash, I strongly recommend reading http://mywiki.wooledge.org/BashGuide