1

I was trying using head command, in macOS using zsh, code below,

a.txt:

1
2
3
4
5
6
7
8
9
10

tail -n +5 a.txt   // line 5 to line end

tail -n -5 a.txt   // last line 5 to line end

head -n +5 a.txt // line 1 to line 5

head -n -5 a.txt  // # What did this do?

The last command shows an error.

head: illegal line count -- -5

What did head -n -5 actually do?

imbr
  • 6,226
  • 4
  • 53
  • 65
user956609
  • 906
  • 7
  • 22

2 Answers2

2

Some implementations of head like GNU head support negative arguments for -n

But that's not standard! Your case is clearly not supported.

When supported The negative argument should remove the last 5 lines before doing the head

imbr
  • 6,226
  • 4
  • 53
  • 65
0

It becomes more clear, if using 3 instead of 5. Note the signs!

# print 10 lines:
seq 10

1
2
3
4
5
6
7
8
9
10

#-------------------------    
# get the last 3 lines:
seq 10 | tail -n 3

8
9
10

#--------------------------------------
# start at line 3 (skip first 2 lines)
seq 10 | tail -n +3

3
4
5
6
7
8
9
10

#-------------------------    
# get the first 3 lines:
seq 10 | head -n 3

1
2
3

#-------------------------    
# skip the last 3 lines:
seq 10 | head -n -3

1
2
3
4
5
6
7

btw, man tail and man head explain this behavior.

Wiimm
  • 2,971
  • 1
  • 15
  • 25