Search⌘ K

Reading the Beginning and End of a File

Explore how to use head and tail commands to read the first and last lines of files, including following file changes in real-time. Understand file permission issues when creating or modifying files in restricted directories and how to use sudo effectively.

Sometimes, the most interesting information in a file is in the first few lines or the last few lines. That’s where the head and tail commands come in handy.

The head command

The head command reads the first ten lines from a file and displays them to the screen:

$ head /var/log/bootstrap.log

Use the terminal below to practice this command.

Terminal 1
Terminal
Loading...

If we want a different number of lines from the file, we use the -n argument to specify the number of lines we want to see. To grab the first line in the /var/log/bootstrap.log file, we use the following command:

$ head -n 1
...