Title: The head and tail Commands
Course: CIS 121: Linux/UNIX I: Fundamentals ~ For IGNITE Review > Section: Learning Modules

Head Command

The head command will give you the first ten lines of a file or the first ten lines of the output of a command.

- Syntax: head filename

You can specify the number of lines that you want by using a hyphen followed by a number.

- The command: head -12 num_file will display the first 12 lines of num_file.

If there are not enough lines, you will get the whole output. No error is generated.

Tail Command

The tail command will display the last ten lines of a file or the last ten output lines of a command.

- Syntax: tail filename

You can use a hyphen and a number to request a specific number of lines.

- Example: tail -7 filex will return the last seven lines of filex.

If there are not enough lines in the file, you will get the entire file. If you use a plus sign and a number, you will get all lines starting at the line number entered to the end of the file.

- Example: tail +2 filez will give you the contents of filez starting at the second line of the file.

NOTE: The plus sign cannot be used with the head command, only with the tail command.