13

How can get I get the contents of a text file, except the last (for instance) 2 lines, with standard or GNU shell tools? (bash, tail, cat and so on.)

3 Answers3

22

Use the head command from coreutils:

head -n -2

See info head for more.

Thor
  • 6,763
  • 1
  • 40
  • 44
5

You simply want.

head -n-2 file

Read man head for more information.

iiSeymour
  • 675
0

This command gives the whole $file except the last $except number of lines:

length=$(wc -l $file); head -n $((${length%% *}-except)) $file

(No checking whether $except is larger than the total number of lines of $file...)

slm
  • 10,859
PePa
  • 7