The tail
command stands for “tail of the file,” and as the establish suggests, it’s necessarily used to view the general part of files. Whether or not or now not you’re monitoring log files or tracking real-time knowledge changes, tail
is your go-to software.
The command is frequently used along side other Linux directions like grep
for taking a look, awk
for text processing, and |
(pipe) for chaining a couple of directions together. As an example, likelihood is that you’ll use tail -f /var/log/syslog | grep "error"
to watch device logs for errors in real-time.
Customary syntax for tail
command:
$ tail [OPTION...] [FILE...]
1. Display the fitting collection of lines
tail -n [number] [file]
Using this option signifies that you’ll be able to get the tail
command to supply output that may display a certain collection of lines in a file.
Example:
Suppose you’ve gotten a text file named example.txt
with the following content material subject material:
Line 1: That's the number one line. Line 2: This is the second line. Line 3: That's the third line. Line 4: That's the fourth line. Line 5: That's the fifth line. Line 6: That's the sixth line. Line 7: That's the seventh line. Line 8: That's the eighth line. Line 9: That's the ninth line. Line 10: That's the tenth line.
If you want to display the general 3 lines of this file, you might be able to use the following command:
tail -n 3 example.txt
This will show you the general 3 lines of the example.txt
file.
Line 8: That's the eighth line. Line 9: That's the ninth line. Line 10: That's the tenth line.
2. Display lines starting from a decided on line amount
tail +[number] [file]
The command with the +
sign outputs knowledge starting from the required line amount.
Example:
Let’s say you’ve gotten a text file named example.txt
with the following content material subject material:
Line 1: That's the number one line. Line 2: This is the second line. Line 3: That's the third line. Line 4: That's the fourth line. Line 5: That's the fifth line. Line 6: That's the sixth line. Line 7: That's the seventh line. Line 8: That's the eighth line. Line 9: That's the ninth line. Line 10: That's the tenth line.
If you want to display the content material subject material starting from line 5 to the highest of the file, you’ll use the following command:
tail +5 example.txt
The output may well be:
Line 5: That's the fifth line. Line 6: That's the sixth line. Line 7: That's the seventh line. Line 8: That's the eighth line. Line 9: That's the ninth line. Line 10: That's the tenth line.
3. Display a couple of files
tail [file1] [file2]
Use this command to turn the ideas of a couple of files at the identical time.
Example 1: Viewing the Ultimate 10 Traces of a Single Record
Let’s say you’ve gotten a file named example.txt
with the following content material subject material:
Line 1: Hello Line 2: World Line 3: This Line 4: is Line 5: a Line 6: development Line 7: text Line 8: file Line 9: for Line 10: demonstration Line 11: purposes Line 12: very best
Running the command:
tail example.txt
Will output:
Line 3: This Line 4: is Line 5: a Line 6: development Line 7: text Line 8: file Line 9: for Line 10: demonstration Line 11: purposes Line 12: very best
Example 2: Viewing the Ultimate 10 Traces of Multiple Information
Suppose you’ve gotten every other file named example2.txt
with the following content material subject material:
Line 1: Each and every different Line 2: Example Line 3: Record
You’ll have the ability to view the general 10 lines of every example.txt
and example2.txt
via running:
tail example.txt example2.txt
This will output:
==> example.txt example2.txt <== Line 1: Each and every different Line 2: Example Line 3: Record
The ==>
<==
notation is used to separate the output from different files.
4. Output a certain collection of bytes
tail -c [bytes] [file]
To turn a decided on collection of bytes in a text file, use the -c
risk.
Example:
Let’s consider now we have a file named development.txt
with the following content material subject material:
That's the number one line. This is the second line. That's the third line. That's the fourth line. That's the fifth line.
If we wish to display the general 20 bytes of this file, we may use the following command:
tail -c 20 development.txt
5. Use a couple of directions at once
tail [file] | [other_command]
Use the tail
command with pipes |
to use it along side every other command.
Example: Using tail
with grep
Let’s consider you’ve gotten gotten a log file referred to as server.log
and you wish to have to check the general 10 lines for any occurrences of the word “error
“.
tail server.log | grep 'error'
Here’s what happens:
tail server.log
reads the general 10 lines of the server.log file.- The output is then piped (
|
) to thegrep 'error'
command. grep 'error'
filters the lines to only show those that contain the word “error
“.
6. Follow files in real-time
tail -f [file]
The -f
risk is used to track file changes. When new log entries are added to the log file, it updates the display inside the terminal window.
Example:
Let’s consider you’ve gotten gotten a log file named application.log
that is being written to while an application is operating. You need to watch this log file for any new entries.
Open a Terminal Window, navigate to the checklist where application.log
is situated, i.e cd /path/to/checklist
.
Run the tail -f
command:
tail -f application.log
After running this command, you’ll be able to see the general 10 lines of application.log
displayed inside the terminal. The terminal will stay open, and any new lines added to application.log
may well be displayed in real-time.
Additional Linux directions:
List Operations | rmdir · cd · pwd · exa · ls |
Record Operations | cat · cp · dd · much less · contact · ln · rename · extra · head |
Record Instrument Operations | chown · mkfs · find |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | in finding · grep · sed · whatis · ripgrep · fd · tldr |
Instrument Information and Keep an eye on | env · historical past · most sensible · who · htop · glances · lsof |
Client and Session Keep an eye on | display · su · sudo · open |
The post Tips on how to Use the Tail Command in Linux appeared first on Hongkiat.
Supply: https://www.hongkiat.com/blog/linux-command-tail/
Contents
- 0.0.0.1 1. Display the fitting collection of lines
- 0.0.0.2 2. Display lines starting from a decided on line amount
- 0.0.0.3 3. Display a couple of files
- 0.0.0.4 4. Output a certain collection of bytes
- 0.0.0.5 5. Use a couple of directions at once
- 0.0.0.6 6. Follow files in real-time
- 0.0.0.7 Additional Linux directions:
- 0.1 Related posts:
- 1 The Execs and Cons of Trunk-Based totally vs. Characteristic-Based totally Building
- 2 Edit your WordPress web site’s header with out plugins
- 3 Construction a Cast Investor Pitch Deck: Key Parts and Highest Practices
0 Comments