Here is a solution using sponge (from the moreutils package):
head -n -1 foo.txt | sponge foo.txt
Summary of solutions:
If you want a fast solution for large files, use the efficient tail or dd approach.
If you want something easy to extend/tweak and portable, use the redirect and move approach.
If you want something easy to extend/tweak, the file is not too large, portability (i.e., depending on moreutils
package) is not an issue, and you are a fan of square pants, consider the sponge approach.
A nice benefit of the sponge approach, compared to "redirect and move" approaches, is that sponge preserves file permissions.
Sponge uses considerably more RAM compared to the "redirect and move" approach. This gains a bit of speed (only about 20%), but if you're interested in speed the "efficient tail" and dd approaches are the way to go.