I was recently looking for a way to remove the last line of a file foo.txt
and came across the following solution
head -n -1 foo.txt temp.txt; mv temp.txt foo.txt
which works fine. However, I also tried simply
head -n -1 foo.txt > foo.txt
which to my surprise left foo.txt
an empty text file, and I would like to know why.
I'm just getting started with unix, so it's possible my question is rather silly.
Because the redirect >
happens before the rest of the command.
If you want to do an inplace edit, you'll need a suitably aware utility. e.g. perl
or sed
. (Or just do the mv
like the original snippet suggested)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments