Skip to main content
Engineering LibreTexts

05-C.6.2: Working with Files and Directories - less/more Command

  • Page ID
    32180
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The less Command

    The less command is Linux utility which can be used to read contents of text file one page (one screen) per time. It has faster access because if the file is large, it doesn’t access the complete file, but accesses it page by page.

    For example, if it’s a large file and you are reading it using any text editor, then the complete file will be loaded to main memory, but less command doesn’t load the entire file, instead it loads it part by part, which makes it faster.

    This video covers the basics of several commands, a lot of good information.

    Syntax:

    less [options] [input file]
    

    Command Options:

    Options Option Meaning
    -E Causes less to automatically exit the first time it reaches end of file.
    -f Forces non-regular file to open.
    -F Causes less to exit if entire file can be displayed on first screen.
    -g Highlights the string which was found by last search command.
    -G Suppresses all highlighting of strings found by search commands.
    -i Cause searches to ignore case.
    -n Suppresses line numbers.
    -p pattern Tells less to start at the first occurrence of pattern in the file.
    -s Causes consecutive blank lines to be squeezed into a single blank line.

    The less command can display either directly from a file, or from the output of another command.

    pbmac@ubuntu $ less dmesg
    ...the contents of the file are displayed...
    pbmac@ubuntu $ cat dmesg | less
    
    

    In this example, we see the less command with a single file name as the input for the less command. In the second line we see a very common use of the output of one command being "piped" to the input of the second command.

    When the less command is used to display data, it will show a screenful, then pause with the cursor at the bottom of the screen next to a colon prompt. At this point the user has several options: 1) move on to the next screen by simply hitting the space bar, the PageDown or the f key (forward) 2) move down one line by hitting the Enter key or the down arrow 3) move to the previous screen by hitting the PageUp button, or the b key (back) 4) move up one line by using the up arrow key 5) enter a / followed by a text string, then use the n key to search forward, or the N key to search backwards in the file or 6) enter q to quit.

    There are powerful search capabilities built into less, but that is beyond what we would like to cover here. Numerous resources are available on the web to understand how this capability works.

     The more Command

    The more command is actually the original command, with less being developed at a later time. They both are known as "pagers," in that they display a page at a time. Both commands have similar capabilities and operate in very much the same manner. The biggest difference is that less is a bit faster because it does not load the entire file into memory before it displays the output.

    Syntax:

    more [ OPTIONS ] [-num] [+/pattern] [+linenum] [file_name]
    

    Command Options:

    Options Option Meaning
    -f Causes more to count logical, rather than screen lines (i.e., long lines are not folded).
    -p Do not scroll. Instead, clear the whole screen and then display the text.
    -c Do not scroll. Instead, paint each screen from the top, clearing the remainder of each line as it is displayed.
    -l more usually treats ^L (form feed) as a special character, and will pause after any line that contains a form feed. The -l option will prevent this behavior.
    -d more will prompt the user with the message "[Press space to continue, 'q' to quit.]" and will display "[Press 'h' for instructions.]" instead of ringing the bell when an illegal key is pressed.
    +num Start at line number num .
    -u Suppress underlining.
    +/ The +/ option specifies a string that will be searched for before each file is displayed.
    -s Squeeze multiple blank lines into one.

    To help remember the difference between less and more, a common joke is to say, "less > more," implying that less has greater functionality than more. A similar saying is that "less is more, more or less."

    Adapted from:
    "less command in Linux with Examples" by Mandeep Singh, Geeks for Geeks is licensed under CC BY-SA 4.0


    05-C.6.2: Working with Files and Directories - less/more Command is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.