...

/

Reading the Beginning and End of a File

Reading the Beginning and End of a File

Learn how to read the beginning and the end of a file, and learn how to create a file anywhere in the filesystem by specifying the path to the directory.

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
...