Select Page

Precise Reading with less and more command in Linux

Previously you learned about the cat command to read a file from the terminal. Did you notice one thing that, it prints the entire file at once? In this way, you need to scroll up and down to inspect the entire file. What if you’re viewing a very large file? The cat command becomes tough to inspect the file. Here comes the Linux less command and a very similar more command. The less and more commands show the file contents one screen at a time, i.e. only the amount that fits your screen. You can then scroll the screen or find specific information. Even you can use regex to find content that matches your pattern! You’ll mostly use the less command over the more command. In this article, the following questions have been answered.

  • What are the differences between the Linux less and more commands?
  • Why is the less command better than the more command?
  • A glance at the Linux more command
  • Deep dive into the Linux less command

less command vs more command

more‘ came before ‘less‘ in Linux. So you can guess, ‘less‘ is an updated and upgraded version of ‘more‘. It has more functionality and flexibility than the ‘more‘ command. The main differences are:

  • more‘ loads the entire file in the memory and then shows it on the terminal. That’s why you can see the percentage showing at the bottom telling how much you’ve scrolled of the entire file.
  • less‘ loads one screen at a time in the memory instead of loading at once.
  • When you reach the end of the result in more command, the command ends there. So you can’t perform any operation on it without running the command again.
  • In less, the command doesn’t end until you hit ‘Q‘. So it’s helpful for inspection.

As you can see, the less command is better than the more command in memory efficiency, speed, usage, and inspection. That’s why you’ll mostly use ‘less‘. However, I’ll describe the basic operations of the more command so that you can play around.

Before you start

Download this dictionary from the internet and save it with the name dictionary.txt. You’ll use this file to play with the more and less commands.

$ mkdir MLwiki; cd MLwiki
~/MLwiki$ curl -L -o dictionary.txt 'https://tinyurl.com/zeyq9vc'

If the first line seems unfamiliar to you, try reading this blog on Organizing Files with mkdir, mv,cp.

The output should look like this:

Downloading a list of vocabulary with curl command
Downloading a list of vocabulary with curl command

Now try to read this file with the cat command you learned earlier.

$ cat dictionary.txt
Reading the dictionary file with cat
Reading the dictionary file with cat command

Did you notice that within a fraction of a second the entire list has been printed? This is a very long list, and using the cat command, you can’t manipulate it that much. That’s why the Linux more and less commands come in handy.

more command in Linux

Now read the dictionary.txt file again but with the more command. Here’s the basic syntax for it:

$ more path/to/file

The syntax is the same as the cat command. But now without displaying the entire file at once, the more command is displaying one screen at a time. At the bottom line, you can see the percentage of the file that has been shown already.

reading the dictionary file with the more command
Reading a file with more command

Navigating with the more command

Use these commands to navigate through the file.

  • [Enter key] – Scrolls down 1 line at a time
  • [Space key] – Scrolls down 1 screen at a time
  • [b] – Skips backward 1 screen at a time i.e. scrolls up 1 screen
  • [f] – Skips 1 screen in forward. Equals to 2 space keys
  • [=] – Shows current line number
  • /pattern – Returns all occurrences that match this pattern. This is a regex
  • dot (.) – Repeats the previous command
  • [q] – Interrupt or exit

If you use the Linux more command, these are the shortcuts you’ll be using. There are lots of other options and shortcuts available, which you can see using the following commands. Note that both of the commands display different lists. You’ll also notice that the manual telling you to use the Linux less command.

$ more --help # a quick help
$ man more    # more command manual

less command in Linux

As you’ve already known, the Linux less command is more useful than the more command. It sounds funny, doesn’t it? The less command is faster, memory efficient as it does not load the entire file while reading. So it’s your ultimate choice when reading and manipulating a large file. The less command has more flexibility, options, commands than more. Let’s dive deep into it!

Reading files with less

The basic syntax is:

$ less [option] path/to/file

To see the available options and commands of less, try both of these commands one by one.

$ man less
$ less --help

See how huge it is! That’s why you’re always being told to use the less command over the more command.

Now read the dictionary.txt file with less and manipulate it with the commands and options described below.

$ less MLwiki/dictionary.txt    # From home directory
$ less dictionary.txt           # If you're already in the MLwiki directory
the less command is displaying the dictionary file one screen at  a time. This is so flexible!
less command in action!

Moving around with the less command

Use up, down, right, left arrow keys to move around one row or column at a time. To see the movement in columns, open another file where each line has multiple words. Use enter to scroll down one line, space to one screen at a time. These are just the basics, let’s see more in a table.

CommandAlternativesDescription
ej, ^E, ^N, CRForward one line
5eForward 5 lines
yk, ^Y, ^K, ^PBackward one line
5yBackward 5 lines
fSPACE, ^F, ^VForward one window
5fForward 5 windows
b^B, ESC-vBackward one window
5bBackward 5 windows
d^DForward one half-window
u^UBackward one half-window
RightArrowESC-)Right one half screen width
5RightArrowRight 5 positions
LeftArrowESC-(Left one half screen width
5LeftArrowLeft 5 positions
^RightArrowESC-}Right to the last column
^LeftArrowESC-{Left to the first column
UpArrowBackward one line
5UpArrowBackward 5 lines
DownArrowForward one line
5DownArrowForward 5 lines

Key points to note here:

  • The actual list is huge, so I’ve highlighted the commands that you’ll most likely use.
  • The caret (^) indicates the Ctrl key. So ^E represents Ctrl+E, where E is just the key, not the uppercase letter.
  • I used 5 before some commands just to show that you can use any number before these commands to scroll that amount of lines or screens.
  • The default one-window is the terminal screen size you’re working on.

Searching with less command

You can either search for your desired word or use regex to find all the occurrences that match the pattern you’re looking for. The best part about searching with less command is that it highlights the matching part! To search for words that match boo, try this:

$ less dictionary.txt
/boo

Remember 2 things here:

  1. The pattern matches anywhere in a word. For example, this will also match Facebook.
  2. By default, this command will show only the matches on the displayed screen. To see the other matches, hit ENTER or SPACE.

Here’s a list of the searching commands:

CommandDescription
/patternSearch forward for matching line
?patternSearch backward for matching line
&patternDisplay only matching lines, skip others
nrepeat the previous search
Nrepeat previous search in the reverse direction
=filename, current line number, total lines,
displayed size of the file, total size,
percentage of the file displayed

All of these commands are very important and very common. The last one seems the most important to me.

A search pattern may proceed by one or more of:

  • ! or ^N – Search for NON-matching lines
  • * or ^E – Search multiple files
  • @ or ^F – Start search at first file (for ‘/’) or last file (for ‘?’)

Jumping with the less command

To jump over different positions in the file, use the commands listed below. But remember, you can use a number before any of these commands to multiply the command. For example, g is used to go to the first line of the file. So you can use 3g to go to the 3rd first line of the file.

CommandAlternativesDescription
g<, ESC+<Go to first line in file (or line N)
G>, ESC+>Go to last line in file (or line N)
p%Go to beginning of file (or N percent into file)
tGo to (N-th) next tag, if any
TGo to (N-th) previous tag, if any
(, {, [Find close bracket ), }, ]
), }, ]Find open bracket (, {, [
m<letter>Mark current position with <letter>
‘<letter>Go to a previously marked position
” (2 single quotes)Go to the previous position
^Beginning of the file (Predefined)
$End of the file (Predefined)

2 things to notice here:

  1. The ‘find close bracket’ option finds a close bracket that matches the N-th open bracket in the top line (if any number is given). Otherwise finds a match for the previous open bracket.
  2. The ‘find open bracket’ option finds an open bracket that matches the N-th close bracket in the bottom line (if any number is given). Otherwise finds a match for the next close bracket.

Options in less command

The less command has a huge amount of options to choose from. All of them can be found in the manual. But here’s a few of them you might find useful in daily use.

  • -a or –search-skip-screen
    • Search skips current screen. Although you set this when opening the file with less command, it takes effect when you start searching for something.
  • -A or SEARCH-SKIP-SCREEN
    • The search starts just after the top line being displayed. Works the same as before.
  • -e or -E
    • Quit at end of the file. In less command, you might have noticed that even if you reach the end of the file, the command does not quit like in more command. But if you like to quit the command at the end, then use this option when opening the file.
  • f or –force
    • Force open non-regular file.
  • F or –quit-if-one-screen
    • Quit if the entire file fits on the first screen. If your file is so small that it fits the first screen, then what’s the meaning of trying the functionalities of less command? So then you may choose to use this option.
  • g or –hilite-search
    • Highlight only the first match for searches
  • G or –HILITE-SEARCH
    • Return the matches for searches, but don’t highlight them
  • -m or –long-prompt
    • Set the prompt style. By default, it shows the percentage of the file that has been displayed
  • -n or –line-numbers
    • Don’t use line numbers

Most important options

As the list of available options in the less command is huge, let’s see the most important ones in a different list.

  • -? or –help
    • Displays help from the command line.
  • -i or –ignore-caseIf
    • your search pattern doesn’t contain any uppercase later, then this option will ignore the case while searching for that pattern.
  • I or –IGNORE-CASE
    • Ignore case in all searches, whether it includes any uppercase or not
  • -N
    • Show line numbers
  • -p [pattern]
    • Starts showing the file from the given pattern

Conclusion

This was huge! Tried to cover the important ones that you might need. Still, a lot is remaining! If nothing described above matches your interest, try reading the less command manual. more and less commands in Linux are really fun to work with!

When working with the more and less commands, you might want to create and populate some files by yourself. To do so, try reading these blogs:

Mind Sharing It?
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others