head和tail命令的-n选项都是用于指定显示的行数,但其后面参数值的意义却大相径庭。
一、在/tmp下建一个20行的test.txt文件。
[root@lgh ~]# cd /tmp
[root@lgh tmp]# seq 20 >test.txt
[root@lgh tmp]# cat test.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

二、head的-n选项:“5”等于“+5”,不等于“-5”——正常

显示前5行
[root@lgh tmp]# head -n 5 test.txt
1
2
3
4
5

显示前5行
[root@lgh tmp]# head -n +5 test.txt
1
2
3
4
5

显示前(总行数-5)行
[root@lgh tmp]# head -n -5 test.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

三、tail的-n选项:“5”不等于“+5”,却等于“-5”——任性

显示最后5行
[root@lgh tmp]# tail -n 5 test.txt
16
17
18
19
20

从第5行开始,显示到最后
[root@lgh tmp]# tail -n +5 test.txt
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

显示最后5行
[root@lgh tmp]# tail -n -5 test.txt
16
17
18
19
20