“tail” Command Usage Examples in Linux

«
»

This tutorial explains Linux “tail” command, options and its usage with examples.

tail – output the last part of files

DESCRIPTION

advertisement

Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input.

SYNOPSIS

tail [+ number] [-l] [-b] [-c] [-r] [-f] [-c number | -n number] [file]

OPTIONS :

advertisement

+number
-number

This option is only recognized if it is specified first. COUNT is a decimal number optionally followed by a size letter (`b’, `k’, `m’) as in `-c’, or `l’ to mean count by lines, or other option letters (`cfqv’).
-l
Units of lines.
-b
Units of blocks.
-c
Units of bytes.
-r
Reverse. Copies lines from the specified starting point in the file in reverse order. The default for r is to print the entire file in reverse order.
-f
Follow. If the input-file is not a pipe, the program will not terminate after the line of the input-file has been copied, but will enter an endless loop, wherein it sleeps for a second and then attempts to read and copy further records from the input-file. Thus it may be used to monitor the growth of a file that is being written by some other process.
-c number
The number option-argument must be a decimal integer whose sign affects the location in the file, measured in bytes, to begin the copying:
+
Copying starts relative to the beginning of the file.

Copying starts relative to the end of the file.
none
Copying starts relative to the end of the file.
The origin for counting is 1; that is, -c+1 represents the first byte of the file, -c-1 the last.
-n number
Equivalent to -c number, except the starting location in the file is measured in lines instead of bytes. The origin for counting is 1; that is, -n+1 represents the first line of the file, -n-1 the last.
file
Name of the file you wish to display

EXAMPLES

1. Print the last N lines

$ tail -n 5 flavours.txt 
Debian
Redhat
Gentoo
Fedora core

Prints the lines of file1.txt in reverse, from last line to first.
Note: When you simply pass the filename, it prints out the last 10 lines of the file.

advertisement

2. To view the final n bytes of each of multiple files

$tail -c 5 abc def

When tail counts by bytes, it also includes the newline character, which is a non-printing (i.e, invisible) character that is designated by a backward slash and the letter n (i.e., \n). Thus, for example, if there are three new, blank lines at the end of a file, they will be counted as three characters, along with the printing characters (i.e., characters that are visible on the monitor screen or paper).

3. Print from nth line to the end

$tail +7 abc

It prints from seventh line till the end of file.

advertisement

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn