Remove the last line from a file in Bash
问题 I have a file, foo.txt , containing the following lines: a b c I want a simple command that results in the contents of foo.txt being: a b 回答1: Using GNU sed: sed -i '$ d' foo.txt The -i option does not exist in GNU sed versions older than 3.95, so you have to use it as a filter with a temporary file: cp foo.txt foo.txt.tmp sed '$ d' foo.txt.tmp > foo.txt rm -f foo.txt.tmp Of course, in that case you could also use head -n -1 instead of sed . MacOS: On Mac OS X (as of 10.7.4), the equivalent