Unix: How can I prepend output to a file? Unix: How can I prepend output to a file? unix unix

Unix: How can I prepend output to a file?


sed will happily do that for you, using -i to edit in place, eg.

sed -i -e "1i `date "+%Y-%m-%d at %H:%M"`" some_file


This works by creating an output file:

Let's say we have the initial contents on file.txt

echo "first line" > file.txt          echo "second line" >> file.txt

So, file.txt is our 'bottom' text file. Now prepend into a new 'output' file

echo "add new first line" | cat - file.txt > output.txt # <--- Just this command

Now, output has the contents the way we want. If you need your old name:

mv output.txt file.txtcat file.txt


The only simple and safe way to modify an input file using bash tools, is to use a temp file, eg. sed -i uses a temp file behind the scenes (but to be robust sed needs more).

Some of the methods used have a subtle "can break things" trap, when, rather than running your command on the real data file, you run it on a symbolic link (to the file you intend to modify). Unless catered for correctly, this can break the link and convert it into a real file which receives the mods and leaves the original real file without the intended mods and without the symlink (no error exit-code results)

To avoid this with sed, you need to use the --follow-symlinks option.
For other methods, just be aware that it needs to follow symlinks (when you act on such a link)
Using a temp file, then rm temp file works only if "file" is not a symlink.

One safe way is to use sponge from package moreutils

Unlike a shell redirect, sponge soaks up all its input before opening the output file. This allows for constructing pipelines that read from and write to the same file.

sponge is a good general way to handle this type of situation.

Here is an example, using sponge

hbu=~/'Documents/Homebrew Updates.txt'{ date "+%Y-%m-%d at %H:%M"; cat "$hbu"; } | sponge "$hbu"