How to Use the head Command to Preview a File – 7 Examples

head-command

The head command displays the first 10 lines of a given file. You can use other alternative commands, but if you aim to display a specific part of your file, using the head or the tail command is the best approach.

For instance, if you want to check the first lines of a log file, you can use the head command and limit the displayed output. Doing so ensures you quickly work with the required section instead of the entire file. This tutorial discusses the head command and gives examples of using it.

Understanding the head Command

To run the head command, specify the option and the file. Follow the syntax below.

The head command can be used with the few options in the below table.

OptionDescription
-n or –linesIt limits the output to a specified number of lines.
-c or –bytesIt shows the output based on specified bytes.
-v or –verboseIt includes the file name in the output.
-q or –quietIt removes the file name tag from the output.

Examples of Using the head Command

For anyone looking to view the contents of a file, using the head command is an easy way out. Hopefully, the examples below will clear the air on using the command.

Example 1: Basic Usage

The quick way of using the head command is to display the first 10 lines of any file. Add the head command and specify the file you wish to view its contents.

First, let’s display the contents of our target file using the cat command.

We’ve added the -n option to include the line numbers.

cat-command-display-file-contents

Instead of showing all the 14 lines in our output, we can focus on the first 10 lines using the head command.

head-command

Example 2: head Command Set Number of Lines

Even with the head command, you can specify how many lines to include in your output. Add the -n option, and then specify the number.

We’ve specified five lines for our case, and that’s what we get in our output.

head-command-set-line-limit

Example 3: Using the head Command with Bytes

When you add the -c option, the command will use bytes instead of lines in the output. For instance, using the -c 15 option, the output will show the first 15 bytes of our file.

We would have our command as follows:

head-command-with-bytes

Example 4: Include File Name Tag in Output

The verbose mode will show the file name tag when displaying the output of the command. In previous examples, you’ve noticed that the file name tag doesn’t appear in the output. However, our output will differ if we run our command with the -v option.

show-file-name-tag

Example 5: Working with Multiple Files

Suppose you want to view the first lines of multiple files. The head command allows you to specify the files and include any option.

To display the first five lines, we would use the below command.

Notice how the file name tag separates each section. Add the -q option to suppress the file name tag if you want a combined output.

head-command-on-multiple-files

Example 6: save Output to a File

You can redirect the output of your command for later. We would run the command below to rerun the previous example and save it as a file.

We’ve then used the cat command to view the contents of the saved output file. Its contents match what would have been displayed in the terminal after running the command.

head-command-redirect-output

Example 7: Using the head Command with the ls Command

For this example, we demonstrate how to use the head command with other commands. The ls command lists a directory’s contents. However, you can combine it with the command to limit the listed output to 10.

head-command-with-ls

You can even include other options for better results. For instance, limiting the number of displayed outputs requires adding the -n option, like in the example below.

head-command-with-ls-command

Conclusion

This post sheds light on the head command. We’ve discussed how to use it to view a file’s first lines and given examples for a better understanding. With that, you can now view specific sections of a file. Still, you can combine it with the tail command to focus on the last lines of the file.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top