6.1 Directory and File Management Using the Command Line
6.1.4 Displaying file contents with head and tail
System administrators frequently utilize the head and tail commands to quickly check portions of text files that they do not need to view in their entirety.

Using the head Command
The
head command is used to display the first n lines of one or more text files. The first ten lines are displayed by default if the -n option is omitted. The head command is useful when you only want to check the first few lines of a file regardless of its length. Figure uses the head command to display the first five lines of the /usr/dict/words file.

The head command here is used by itself, but is typically used in a command line containing several commands in a pipeline. This will be covered later in the course.

Command Format:
head [ -n ] filename(s)

Using the tail Command 
Use the
tail command to display the last n lines of a text file. The last ten lines are displayed by default if the -n option is omitted. The tail command is useful for checking the most recent entries in large log files. Backup utility programs frequently write their results to a log file showing which files were backed up and when. The final entries in a backup log file are usually the total number of files backed up and messages indicating whether the backup finished successfully. Backup logs can be quite large and the tail command allows you to check the end result of the backup without looking at the whole file.

Like the head command, the tail command can be used by itself but is typically used in a command line containing several commands in a pipeline. This will be covered later in the course.

Command Format:
tail [ -n ] filename(s) or tail [ +n ] filename(s)

The command format above shows two options with the tail command; -n and +n. The -n option displays the last n lines of the file. The +n option allows you to start displaying lines from a specific point in a file. It starts at the nth line in the file and displays all lines from there to the end. The first example in Figure shows the use of the tail command to see the last five lines of the /usr/dict/words file. The second example uses the +23 option to start at line 23 of the file and displays the remaining lines.

Interactive Media Activity (Flash, 90 kB)
  Using head and tail

In this media activity, you are logged in as user2 and your current working directory is: /home/user2. Refer to the Class File Tree Structure by clicking the 'tree' button located on the Menu Bar and type the commands that would accomplish the requested objectives. 
Note: Be sure to press enter after each one. Click on step 1 to begin.