0

Possible Duplicate:
How can I make iconv replace the input file with the converted output?

I'm writing a script to change the content of my hosts file but I got stuck on the head output redirection.

If I do head -n -1 hosts >hosts my hosts file will result empty and yes, it has more than 1 line.

Esolitos
  • 103

1 Answers1

2

That's because your shell truncates the file when you redirect to it. This happens before head gets a chance to read it.

You can either use a temporary file:

head -n1 hosts > hosts.tmp && mv hosts.tmp hosts

Or use sponge from the moreutils packate:

head -n1 hosts | sponge hosts