Linux tail command: View end-of-file data
Linux tail command Function Description
Use the tail command to view the end-of-file data.
By default, the last 10 lines of the specified file to the standard output are displayed.
If multiple files are specified, tail adds the corresponding file name as a header at the beginning of each output segment.
Linux tail command Syntax
tail [Option] [File]
The meaning of each option in the command is shown in the table.
Option | Description |
---|---|
-n <K> |
Output the last K lines, where K is a number, and use -n+K to output from the Kth line of each file |
-c <K> |
Output the last K bytes, where K is a number, and use -c+K to output from the Kth byte of each file |
-f | Immediate output of data appended after file changes |
Linux tail command Demo
View the last 3 lines of data in the file /etc/passwd
tail -n 3 /etc/passwd
Output:
The data content of the file /etc/passwd is displayed starting at line 20
tail -n +20 /etc/passwd
Output:
[root@rhel ~]# tail -n +20 /etc/passwd
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:”Saslauthd user”:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
View the contents of the 100 bytes of data at the end of the file /etc/passwd
tail -c 100 /etc/passwd
Output:
[root@rhel ~]# tail -c 100 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
Display the data content of the file /etc/passwd starting at the 900th byte
tail -c +900 /etc/passwd
Output:
[root@rhel ~]# tail -c +900 /etc/passwd
ahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:''Saslauthd user'':/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
Dynamically track the growth of the file /var/log/messages
The tail command will check every second to see if new content has been added to the file, and if so, append it to the original output and display it. If you want to terminate the output, press [Ctrl+c
] to interrupt the tail command.
[root@rhel ~]# tail -f /var/log/messages