I use sed to quickly delete lines with specific position as

sed '1d'
sed '5d'

But, what if I want to delete the last line of the file and I don't know the count of lines (I know I can get that using wc and several other tricks).

Currently, using a workaround with head and tailcombined with wc to do so. Any quick twists here?

  • Which os? Some flavours have interesting options for head and tail...
    – Nils
    Oct 24 '12 at 19:24
  • @Nils I am on 'Red Hat Enterprise Linux Server release 5.5 (Tikanga)'. Can you point for the options for head and tail you know of on different flavours?
    – mtk
    Nov 19 '12 at 6:39
  • I did. See below.
    – Nils
    Nov 23 '12 at 20:51

in sed $ is the last line so to delete the last line:

sed '$d' <file>
  • 31
    Use sed -i '$d' <file> to edit file in place. May 22 '17 at 18:59
  • 2
    What would be for deleting the last n lines, where n is any integer number? Feb 18 '19 at 20:26
  • 2
    @JoshuaSalazar for i in {1..N}; do sed -i '$d' <file>; done dont forget to replace N
    – ghilesZ
    Oct 21 '20 at 13:23
  • somehow your answer didn't work without the top comment's suggestion i.e. without the -i. Why is it? i.e. sed -i '$d' <file> is needed for me. Apr 24 at 0:31

$ for the last line:

sed '$d' file
  • 13
    beat you by 30 seconds :P
    – h3rrmiller
    Oct 24 '12 at 14:45
  • 1
    Sadly though one has to use more complex techniques to delete, say, the last two lines.
    – dubiousjim
    Oct 24 '12 at 15:13
  • 1
    What do you mean? sed '$d' file; sed '$d' file
    – Rob
    Oct 24 '12 at 15:35
  • 4
    @Rob: sed '$d' file doesn't actually modify the file; it just prints out the contents of the file, minus the last line. So sed '$d' file; sed '$d' file will print out the contents of the file twice, minus the last line each time. The delete-the-last-two-lines equivalent of sed '$d' file is sed '$d' file | sed '$d'.
    – ruakh
    Oct 24 '12 at 18:48
  • 2
    Yeah, that's inelegant but of course it works. I would do it in one pass with sed -n '1{h;n;}; $q; x; p;'. I was pouting that we can't just do sed '$-1,$d'.
    – dubiousjim
    Oct 24 '12 at 20:24

cat file.txt | head -n -1 > new_file.txt

Beware, it seems, depending on the last line of file.txt (if it ends with EOF, or \n and then EOF), the number of lines in new_file.txt may be the same (as file.txt) after this command (this happens when there is no \n) - in any case, the contents of the last line is deleted.

Also, note that you must use a second (intermediate) file. If you cat and redirect to the same file, you'll erase it.

  • 7
    Where this works, it could also be simply head -n -1 file.txt > new_file.txt. Note though that negative counts on head -n is only available for some implementations of head. For example, it doesn't work for FreeBSD's or for the BusyBox version of head.
    – dubiousjim
    Oct 25 '12 at 9:50
  • 1
    @dubiousjim: Agreed, head -n -1 file.txt > new_file.txt is better. It works the same way with respect to my two comments (same number of lines, and, erase on same file). As for the negative argument, such differences occur from time to time between Unixes, and they are often frustrating because you expect it to be the same command entirely (and why wouldn't you - same name and purpose!) -- anyhow, good point. (I use Debian.) Oct 25 '12 at 17:38

head --lines=-1. I first stumbled across this possibility in the man-page for head on a SLES11SP2-system (coreutils-8.12-6.23.1)

tail and head are part of the coreutils-rpm (at least for rpm-based-systems).

According to the changelog of coreutils, this syntax is supported since coreutils-version 5.0.1

Bad news: According to the RH5-man-page this option is not described

Good news: It works with RH5 (so in your case: it works - at least with a current version of RH5).

rpm -q coreutils shows me (on CentOS 5.8): coreutils-5.97-34.el5_8.1

I am not sure if RH5.5. already has the coreutils-version that supports it. But 5.5 has EoLed anyway.

  • +1. it work's perfectly on RH5.5. Thanks.
    – mtk
    Nov 28 '12 at 6:23


$ is the last line, d for delete:

sed '$d' ~/path/to/your/file/name


Equivalent of the sed -i

sed -i '' -e '$ d' ~/path/to/your/file/name

On Mac (BSD head/tail) you can use:

cat file.txt | tail -r | tail -n +2 | tail -r

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.