Wrestling Inodes

The origin of this article was a problem I was encountering in mangling files.

Mangling files is what Unix is all about. So the faster that you can wrestle them inodes, the more useful work you can accomplish. There's a whole toolkit full of useful tools to do file mangling in Unix: echo, cat, tac, sort, col, grep, sed, awk, perl, and a whole bunch more. They each have their own niche, and part of the zen of Unix is learning the feel of each tool so you can determine when each on is appropriate.

Appending stuff to a list of files

This is the example of an obvious niche. If you have a directory full of files, appending stuff to end of all (or some) of them is easy. The only tools required are echo and shell file globbing.

For example, you could for i in *.txt; do echo "test" >> $i; done to append the phrase "The end." to each file ending in .txt in the current directory.

Prepending stuff to a list of files

This is the example of a more obtuse niche. I had trouble figuring out the best way to handle this one initially. It seemed to me that this should be a fairly common problem, and therefore there should be a tool optimized for performing this function. It seems like sed wouldn't be the right tool for this as it has a reputation of being line orientated rather than file orientated. Perl would be needlessly complex. Shell provides an append redirector (>>), but no prepend operator (there are historical efficiency reasons for why Unix doesn't do this).

It turns out that the sed line editor is the solution. Commands in sed can specify a line number (such as line 1 for prepending), a feature that I wasn't aware of.

You're probably already fairly proficient with ed if you use vi. The "colon" interface for commands is essentially ed. So to prepend something to a list of files, a two-line shell command like:

for i in *.txt; do cat $i | sed -e '1iPrepended Line Test' > $i; done

would be appropriate.