tail command in Linux with examples

Let’s learn about tail command in Linux with examples and their options, the tail command is mainly used to check the last lines of the files or log files.

Also, a continuous log file can be viewed by tail command. It is mainly used by System Admin to check the logs.

Q.How to view the last few lines using a tail command in Linux?

So, when we have to view the last few lines then use a tail command, which shows us the last 10 lines

So, the file tail_example contains 20 lines and if we have to see the last 10 lines.

Command:
# tail tail_example

Also, we can use pipe(|) with the cat command like cat [file_name]|tail

Command:
# cat tail_example|tail

Q.How to view the last few lines by N number of lines using a tail command?

Now we will use -n option to view the N number of lines in a file.

Command:
# tail -n 5 tail_example

So, we have used -n 5 to get the output of the last five lines. If you have to see the last 12 lines use the option like -n 12 with a tail.

Q.How to view Multiple files using a tail command?

Command:
# tail check_the_number_of_line tail_example

We are now getting the output of the two files with there filename in the place of header.

If you do not want that file name in the place of Header, use -q option to never print the file name.

Command:
# tail -q check_the_number_of_line tail_example

Q.How to view filename using a tail command in Linux?

So, we have seen that by using the -q option, will not print the file name, but what if we have to print the file name for the single files?

Command:
# tail -v tail_example

So, by using -v option we can print the file name in the place of Header place.

Q.How to view content by the size using a tail command in Linux?

So, to view the content by the size can use a -c option. By default, the size is in bytes

So, the tail_example has the size of 402 bytes, and have to see what content does the last 125 bytes contains.

Command:
# tail -c 125 tail_example

What if we want to see the content after 125 bytes?

So for that, we have used +125 with -c option and by using this option we will not able to see the starting content of 125 bytes.

Command:
# tail -c +125 tail_example

Q.How to check the log files using a tail command in Linux?

This is the most used term for the System Admin and Linux Admin in Industries.

If any things happen on the server-side, we have to check with the tail -f option command.

Command:
# tail -f /var/log/nginx/access.log

So, -f option will append the data in output and we can see the file grows if any new logs generated.

Let’s have a look how we can see the two log files at the same time?

Command:
# tail -f /var/log/nginx/access.log /var/log/nginx/error.log

So, we have the two files in the /var/log/nginx directories access.log and error.log

Cheatsheet of the tail command
Q.How to view the last few lines using a tail command in Linux?

Syntax:
# tail [filename]

Example:
# tail tail_example
Q.How we can use pipe with tail command

Syntax:
# cat [filename] | tail
# ls -l |tail

Example:
# cat tail_example|tail
# ls -l | tail
Q.How to view the last few lines by N number of lines using a tail command?

Syntax:
# tail -n Number [file_name]

Example:
# tail -n 5 tail_example
Q.How to view Multiple files using a tail command?

Syntax:
# tail [file_name] [file_name2] [file_name3]

Example:
# tail first_file second_file third_file
Q.Not print the filename in the Place of header by tail command?

Syntax:
# tail -q [file_name] [file_name2] [file_name3]

Example:
# tail -q first_file second_file third_file
Q.How to view filename using a tail command in Linux?

Syntax:
# tail -v [file_name]
Example:
# tail -v tail_example
Q.How to view content by the size using a tail command in Linux?

Syntax:
# tail -c [Number] [file_name]

Example:
# tail -c 125 first_file
** By using -c 125 you can see the last 125 bytes content **

# tail -c +125 first_file
** By using -c +125 not able to see the starting content of 125 bytes. **  
Q.How to check the log files using tail command in Linux?

Syntax:
# tail -f [file_name]

Example:
# tail -f /var/log/nginx/access.log
Q.How to check multiple log files using a tail command in Linux?

Syntax:
# tail -f [file_name] [file_name2] [file_nameN]

Example:
# tail -f /var/log/nginx/access.log /var/log/nginx/error.log

Let’s Play QUIZ for the tail command

[wp_quiz id=”2976″]

Want to read about head command follow the link. Thanks to READ the POST.

One Reply to “tail command in Linux with examples”

Give your valuable time