Shell commands - head, tail for file and content processing

Keywords: Linux ssh network DBus

File and content processing - head, tail

1. head: Display the file content header

Functional description of head command

The head command is used to display the contents of the file header. By default, executing the head command will output 10 lines at the beginning of the file.

The grammatical format of the head command

head [OPTION]... [FILE]...
head [option] [file]

Options for the head command

The head parameter is rarely used. Table 1 shows the parameters and descriptions of the head command:

Table 1: Parameters and description of the head command

Parameter options interpretative statement
-n< rows > Number of rows indicated
-c< bytes > Indicate the number of bytes displayed
-q Does not display a header containing a given filename
-v Always display a header containing a given filename

Practical operation of head command

Example 1: Display the first 10 lines of a file

[root@oldboyedu ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

Example 2: Display the first five lines of a file

[root@oldboyedu ~]# head -n5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Example 3: The first n bytes of the display file

[root@oldboyedu ~]# head -c 10 /etc/passwd
root:x:0:0[root@oldboyedu ~]# 

Example 4: Print the file apart from the last 15 lines

[root@oldboyedu /test]# seq -w 20 > oldboy.txt
[root@oldboyedu /test]# cat oldboy.txt
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
[root@oldboyedu /test]# head -n -15 oldboy.txt
01
02
03
04
05

2. tail: Display the end of the file content

Functional description of tail command

The tail command is used to display the content at the end of the file. By default, executing the tail command will output the last 10 lines of the file.

Syntax Format of tail Command

tail [OPTION]... [FILE]...
tail [options] [files]

Option description of tail command

Tail parameters are rarely used. Table 1 shows the parameters and explanations of tail command:

Table 1: Parameters and description of tail command

Parameter options interpretative statement
-f Real-time output of additional data after file changes
-q Do not display processing information
-v Display detailed processing information
-c< number > Number of bytes displayed
-n< rows > Display n lines at the end of the file
--pid=PID Used in conjunction with - f to denote the end of process ID and PID after death
-q, --quiet, --silent Never output the first part of a file name
-s, --sleep-interval=S In conjunction with - f, it means sleeping for S seconds at intervals of each iteration.

Practical operation of tail command

Example 1: The last 10 lines of the display file

[oldboy@oldboyedu ~]$ tail /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
oldboy:x:1000:1000::/home/oldboy:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin

Example 2: The last five lines of the display file

[oldboy@oldboyedu ~]$ tail -n5 /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
oldboy:x:1000:1000::/home/oldboy:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin

Example 3: Display file content from line 15

[oldboy@oldboyedu ~]$ tail -n +15 /etc/passwd
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
oldboy:x:1000:1000::/home/oldboy:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin

Example 4: Real-time monitoring of file changes (commonly used)

[root@oldboyedu /test]# tail -f oldboy.txt 

So far as I'm concerned, if you have any questions or mistakes, please feel free to comment on them.

Posted by Robert07 on Wed, 15 May 2019 22:47:48 -0700