05. Reading files less, more, head, tail

To view a short file, we can simply use cat, as we saw before. For viewing larger files with search and easy navigation options, we instead use less or more.

The main reason that one would want to use less and more instead of a text editor like Vim is that these file readers don't have to load the entire file contents into memory.

Reading with more

The more command is a very basic file read. We are only able to read files forward, so no backward movement is available.

To open a file with more, simply enter the filename as the argument.

$ more file1.txt

To see a list of controls when activating more, simply press h.

Less is more

Unix users like the old adage, "less is more," so they named an improved version of more as less.

less is an extension of more. It provides backward movement in the file, along with forward movement. x

$ less file.txt

Basic commands for less

Here are some basic commands for less. Remember that you can simply press the h key to display a complete command summary.

Forward one line.
Backward one line.
Forward one window.
Backward one window.
Move to line n.

Searching in files

To search forward or backward for the word "foo" in a file, you can type /foo and ?foo, respectively. You can then press n to move forward in the search.

Viewing just part of a file with head and tail

In case you want to read just the beggining or ends of a file, you may use the head and tail commands, which output the first and last 10 lines by default. To specify a certain number of lines, use the -n option.

The tail command is especially helpful when reading out log files, in which the most recent text is appended onto the end of the files.

$ tail -n 3 /var/log/wifi.log
Wed Jul 29 05:17:52.328 ***Starting Up*** Fri Aug 14 22:09:10.179 ***Starting Up*** Tue Sep 15 12:25:43.535 ***Starting Up***

You can even live-track the file with the -f option (f for follow). With this option on, you may see any changes that are added to the file instantaneously.

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources