Linux+: Linux Shell 18 – Head and Tail Commands

J

Jarret W. Buse

Guest
Linux+: Linux Shell 18 – Head and Tail Commands

With large files, it can be helpful to view just the first few lines of each file to determine its contents or verify it is the file you want. With other files, it can be helpful to view the last few lines of the file. By only showing the beginning or ending of the file, it can be quicker than opening a large file in an editor. With log files, the newest logged data can be located at the end or beginning of the file. With the Head and Tail command, you can view just the beginning or end of the file.

Let's begin with the Head command. The Head command displays the beginning of the specified file. The syntax of the Head command is as follows:

head options files

The options are as follows:


  • -c (--bytes=x, --bytes=-x) – displays the first x bytes of the specified file and the -x displays all but the last x bytes of the file

head -c1024 g.txt – prints the first 1024 bytes of the file named g.txt

NOTE: The various size specifications are:

b 512
KB 1000
K 1024
MB 1000*1000
M 1024*1024
GB 1000*1000*1000
G 1024*1024*1024
TB 1000*1000*1000*1000
T 1024*1024*1024*1024
PB 1000*1000*1000*1000*1000
P 1024*1024*1024*1024*1024
EB 1000*1000*1000*1000*1000*1000
E 1024*1024*1024*1024*1024*1024
ZB 1000*1000*1000*1000*1000*1000*1000
Z 1024*1024*1024*1024*1024*1024*1024
YB 1000*1000*1000*1000*1000*1000*1000*1000
Y 1024*1024*1024*1024*1024*1024*1024*1024

So, to show the first 2 kilobytes (1024), the following can be used:

head -c2K g.txt – prints the first 2 KB of the file named g.txt

  • -n (--lines=x, --lines -x) – shows the first x lines instead of the default of 10 lines and -x displays all but the last x line of the specified filename
head -n55 g.txt – prints the first 55 lines of the file named g.txt

It is also possible to drop the 'n' and get the same result with the following:

head -55 g.txt – prints the first 55 lines of the file named g.txt

  • -q (--quiet, --silent) – causes the file name in the header to not be displayed
head -q g.txt h.txt – a header including the file name is not printed before each file (when multiple files are specified, they are printed one after the other and a header is added by default unless the -q option is specified)

  • -v (--verbose) – prints the header for each file even if only onefile is specified
head -v g.txt – prints the header before the first 10 lines of the file g.txt (10 lines is the default number of lines if a specific portion of the file is not specified)

  • --help – prints help information for the Head command
  • --version – prints the current version of the Head command

The Tail commands displays the last lines of a file and uses the following syntax:

tail options files

The options are as follows:

  • -c (--bytes=x, --bytes=+x) – displays the last x bytes of the specified file and the +x displays the data starting at the x byte position to the end

tail -c1024 g.txt – prints the last 1024 bytes of the file named g.txt

NOTE: The various size specifications are listed above in the Head section.

  • -f (--follow=name) – shows the specified portion of the tail of the file and continues to monitor it as it changes. This is a good method to monitor changing logs.

tail -f logfile – monitors last portion of the file (logfile) and continues to display changes made to it

  • -F – same as using the -f option with --retry. The --retry option will retry to open the file when it is inaccessible for some reason
tail -F logfile – monitors the last 10 lines of the file (logfile), continually displaying changes as they are made. Continues to monitor the file and retry when the file is recreated or rotated (for rotating logs)

  • -n (--lines=x, --lines -x) – shows the last x lines instead of the default of 10 lines and -x displays the lines starting at the xth line of the specified filename
tail -n55 g.txt – prints the last 55 lines of the file named g.txt

It is also possible to drop the 'n' and get the same result with the following:

tail -55 g.txt – prints the last 55 lines of the file named g.txt

  • -q (--quiet, --silent) – causes the filename in the header to not be displayed
tail -q g.txt h.txt – a header including the file name is not printed before each file (when multiple files are specified, they are printed one after the other and a header is added by default unless the -q option is specified)

  • --retry – retries to perform the tail command even if the file is inaccessible for some reason
tail --retry logfile – attempts to open logfile to print the last 10 lines (default)continually until the file is found or the command is aborted

  • -s (--sleep=x) – specifies an interval before a new iteration such as with -f. The default is one second

tail -f -s5 logfile – displays last 10 lines of the logfile and continues to monitor it every five seconds for new lines

  • -v (--verbose) – prints the header for each file even if only one file is specified

tail -v g.txt – prints the header before the last 10 lines of the file g.txt (10 lines is the default number of lines if a specific portion of the file is not specified)

  • --help – prints help information for the Tail command
  • --version – prints the current version of the Tail command
These commands can be very useful for checking large files and log files alike. It is always best to open a portion of a file and not the whole file if it is large. Opening larger files takes more time and system resources.
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 7,520

Staff online

Members online


Latest posts

Top