The tail command stands for “tail of the report,” and because the identify suggests, it’s essentially used to view the final a part of recordsdata. Whether or not you’re tracking log recordsdata or monitoring real-time information adjustments, tail is your go-to application.

The command is incessantly used at the side of different Linux instructions like grep for looking, awk for textual content processing, and | (pipe) for chaining more than one instructions in combination. For instance, chances are you’ll use tail -f /var/log/syslog | grep "error" to observe machine logs for mistakes in real-time.

Normal syntax for tail command:

$ tail [OPTION...] [FILE...]
1. Show the precise selection of traces
tail -n [number] [file]

The use of this selection means that you can get the tail command to provide output that can show a definite selection of traces in a report.

Instance:

Think you’ve gotten a textual content report named instance.txt with the next content material:

Line 1: That is the primary line.
Line 2: That is the second one line.
Line 3: That is the 3rd line.
Line 4: That is the fourth line.
Line 5: That is the 5th line.
Line 6: That is the 6th line.
Line 7: That is the 7th line.
Line 8: That is the 8th line.
Line 9: That is the 9th line.
Line 10: That is the 10th line.

If you wish to show the final 3 traces of this report, you could possibly use the next command:

tail -n 3 instance.txt

This may display you the final 3 traces of the instance.txt report.

Line 8: That is the 8th line.
Line 9: That is the 9th line.
Line 10: That is the 10th line.
2. Show traces ranging from a selected line quantity
tail +[number] [file]

The command with the + signal outputs information ranging from the desired line quantity.

Instance:

Let’s say you’ve gotten a textual content report named instance.txt with the next content material:

Line 1: That is the primary line.
Line 2: That is the second one line.
Line 3: That is the 3rd line.
Line 4: That is the fourth line.
Line 5: That is the 5th line.
Line 6: That is the 6th line.
Line 7: That is the 7th line.
Line 8: That is the 8th line.
Line 9: That is the 9th line.
Line 10: That is the 10th line.

If you wish to show the content material ranging from line 5 to the top of the report, you’ll use the next command:

tail +5 instance.txt

The output might be:

Line 5: That is the 5th line.
Line 6: That is the 6th line.
Line 7: That is the 7th line.
Line 8: That is the 8th line.
Line 9: That is the 9th line.
Line 10: That is the 10th line.
3. Show more than one recordsdata
tail [file1] [file2]

Use this command to show the information of more than one recordsdata on the similar time.

Instance 1: Viewing the Remaining 10 Strains of a Unmarried Report

Let’s say you’ve gotten a report named instance.txt with the next content material:

Line 1: Hi
Line 2: Global
Line 3: This
Line 4: is
Line 5: a
Line 6: pattern
Line 7: textual content
Line 8: report
Line 9: for
Line 10: demonstration
Line 11: functions
Line 12: best

Working the command:

tail instance.txt

Will output:

Line 3: This
Line 4: is
Line 5: a
Line 6: pattern
Line 7: textual content
Line 8: report
Line 9: for
Line 10: demonstration
Line 11: functions
Line 12: best

Instance 2: Viewing the Remaining 10 Strains of More than one Recordsdata

Think you’ve gotten some other report named example2.txt with the next content material:

Line 1: Every other
Line 2: Instance
Line 3: Report

You’ll be able to view the final 10 traces of each instance.txt and example2.txt through working:

tail instance.txt example2.txt

This may output:

==> instance.txt  example2.txt <==
Line 1: Every other
Line 2: Instance
Line 3: Report

The ==> <== notation is used to split the output from other recordsdata.

4. Output a definite selection of bytes
tail -c [bytes] [file]

To show a selected selection of bytes in a textual content report, use the -c possibility.

Instance:

Shall we say we've a report named pattern.txt with the next content material:

That is the primary line.
That is the second one line.
That is the 3rd line.
That is the fourth line.
That is the 5th line.

If we need to show the final 20 bytes of this report, we might use the next command:

tail -c 20 pattern.txt
5. Use more than one instructions directly
tail [file] | [other_command]

Use the tail command with pipes | to make use of it at the side of some other command.

Instance: The use of tail with grep

Shall we say you've gotten a log report known as server.log and you need to test the final 10 traces for any occurrences of the phrase "error".

tail server.log | grep 'error'

Here is what occurs:

  • tail server.log reads the final 10 traces of the server.log report.
  • The output is then piped (|) to the grep 'error' command.
  • grep 'error' filters the traces to just display those who comprise the phrase "error".
6. Observe recordsdata in real-time
tail -f [file]

The -f possibility is used to trace report adjustments. When new log entries are added to the log report, it updates the show within the terminal window.

Instance:

Shall we say you've gotten a log report named utility.log this is being written to whilst an utility is working. You wish to have to observe this log report for any new entries.

Open a Terminal Window, navigate to the listing the place utility.log is positioned, i.e cd /trail/to/listing.

Run the tail -f command:

tail -f utility.log

After working this command, you can see the final 10 traces of utility.log displayed within the terminal. The terminal will keep open, and any new traces added to utility.log might be displayed in real-time.

Extra Linux instructions:
Listing Operations rmdir · cd · pwd · exa · ls
Report Operations cat · cp · dd · much less · contact · ln · rename · extra · head
Report Device Operations chown · mkfs · find
Networking ping · curl · wget · iptables · mtr
Seek and Textual content Processing in finding · grep · sed · whatis · ripgrep · fd · tldr
Device Data and Control env · historical past · most sensible · who · htop · glances · lsof
Consumer and Consultation Control display · su · sudo · open

The put up Tips on how to Use the Tail Command in Linux gave the impression first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/linux-command-tail/

[ continue ]