Monday, October 24, 2011

Of cats and redirection...

Let's say you want to cat a file and redirect the output to itself, something like:

cat file > file;

Or perhaps more usefully:

cat file | tail -n 100 > file;

You will end up with an empty file. This is likely because of how the shell handles inodes and redirection (just a guess). A quick and way to get around this is:

cat file | tail -n 100 | dd of=file;

And viola! file points at the correct inode.