Do Linux commands need to be memorized?Daily Linux Command: manDaily Linux Command: headThe tail(1) command is used to output the last part of a file.It corresponds exactly to the functionality of the head(1) command discussed in the previous chapter.
For system administrators and developers, the tail(1) command is an important tool for real-time log monitoring. This article will introduce several common usages of it.
✦•———-Basic Usage———-•✦By default, tail(1) reads the last 10 lines of a file.
$ tail /etc/services
sgic-cad 17004/tcp # Cluster Admin daemon
binkp 24554/tcp # binkp fidonet protocol
asp 27374/tcp # Address Search Protocol
asp 27374/udp # cluster synchronization tool
dircproxy 57000/tcp # Detachable IRC Proxy
tfido 60177/tcp # fidonet EMSI over telnet
fido 60179/tcp # fidonet EMSI over TCP
# Local services
You can specify the number of lines to display using the -n parameter (this parameter is also common in other similar commands, such as the head(1) command we discussed in the previous chapter, which also uses -n to control the number of lines).
$ tail -n 5 /etc/services
dircproxy 57000/tcp # Detachable IRC Proxy
tfido 60177/tcp # fidonet EMSI over telnet
fido 60179/tcp # fidonet EMSI over TCP
# Local services
In addition to controlling by lines, you can also control the length of bytes read using the -c parameter (the head command also uses the -c parameter to control the length of bytes read):
$ tail -c 5 /etc/services
ices
# Why are there only four characters?
# To clarify, we use the hexdump(1) command to view the hexadecimal output.
# Because there is an invisible newline character (\n) at the end, we can only see 4 characters, but in fact, 5 characters are output.
$ tail -c 5 /etc/services | hexdump -C
00000000 69 63 65 73 0a |ices.|
00000005
Next, let’s look at the most common usage: using the -f parameter to dynamically track log updates.
$ sudo tail -f /var/log/syslog
[sudo] password for user:
Sep 18 03:29:50 ubuntu-server kernel: [69051.399030] br-9cf216f58e65: port 2(vethc107295) entered disabled state
Sep 18 03:29:50 ubuntu-server kernel: [69051.399145] vethb645294: renamed from eth0
Sep 18 03:29:50 ubuntu-server networkd-dispatcher[876]: WARNING:Unknown index 2141 seen, reloading interface list
Sep 18 03:29:50 ubuntu-server systemd-udevd[248806]: Using default interface naming scheme 'v249'.
Sep 18 03:29:50 ubuntu-server systemd-networkd[857]: vethc107295: Link DOWN
Sep 18 03:29:50 ubuntu-server kernel: [69051.532997] br-9cf216f58e65: port 2(vethc107295) entered disabled state
Sep 18 03:29:50 ubuntu-server kernel: [69051.533367] device vethc107295 left promiscuous mode
Sep 18 03:29:50 ubuntu-server kernel: [69051.533370] br-9cf216f58e65: port 2(vethc107295) entered disabled state
Sep 18 03:29:50 ubuntu-server systemd[1]: run-docker-netns-3b18bda2845e.mount: Deactivated successfully.
Sep 18 03:29:50 ubuntu-server systemd[1]: var-lib-docker-overlay2-41ff4436180cb48217203b0b38f74f552d8569b52c98e8b6a12a00e8659f9256-merged.mount: Deactivated successfully.
Sep 18 03:29:55 ubuntu-server edgecore[11569]: I0918 11:29:55.500770 11569 process.go:329] start to sync pod status in edge-store to cloud
Sep 18 03:29:55 ubuntu-server edgecore[11569]: I0918 11:29:55.500937 11569 process.go:336] list pod status, no record, skip sync
Sep 18 03:29:56 ubuntu-server edgecore[11569]: I0918 11:29:56.837470 11569 edgedmetricsconnection.go:119] receive stop signal, so stop metrics scan ...
^C
As long as the log file is updated, tail will continuously output the updated content at the end of the file until you press ctrl+c to interrupt the command execution.
That concludes the content of the tail(1) command.
Congratulations, you have gained useful knowledge again✨