Skip to main content

less command

less - file perusal filter for crt viewing

The less command is a powerful and efficient tool in Linux for viewing the contents of files, particularly large ones. Unlike some text editors or viewers that load an entire file into memory, less displays content one page at a time, making it fast and resource-friendly.

Usage: less [OPTION]... [FILE]...

  • OPTION: Flags which enhances the less abilities.
  • FILE: The file you want to view.

Examples

  • Opening a File and Navigating

    To view a file, simply pass its name to less.

    $ less logfile.txt
    $ less -N logfile.txt
    • Opens logfile.txt in the less viewer.

    • The first page of the file is displayed, with a : prompt at the bottom.

    • -N option displays logfile.txt with line numbers on the left.

    • less uses keyboard commands to move around. Here are the essentials:

      KeyAction
      Space or fScroll down one page
      bScroll up one page
      Down ArrowMove down one line
      Up ArrowMove up one line
      gGo to the start of the file
      GGo to the end of the file
      qQuit less
      -NShow line numbers
      -iMake searches case-insensitive
      -SChop long lines (don’t wrap)
      -FQuit if the file fits on one screen
      -XDon’t clear the screen on exit
  • Searching in a File

    Search for text using the / key followed by your pattern.

    $ /pattern
    • Type /error and press Enter to find the next occurrence of "error".
    • Press n to jump to the next match, N for the previous match.
  • Jumping to a Specific Line

    Go to a specific line number by typing it and pressing g.

    $ 50g
    • Jumps to line 50 in the file.
  • Viewing Multiple Files

    You can open multiple files and switch between them.

    $ less file1.txt file2.txt
    • Opens file1.txt first.
    • Use :n to switch to the next file (file2.txt).
    • Use :p to go back to the previous file.
  • Real-Time Updates

    Use less +F to watch a file as it grows (like tail -f).

    $ less +F logfile.txt
    • Shows the end of logfile.txt and updates as new lines are added.
    • Press Ctrl+C to stop following and enter normal less mode, then q to quit
  • Large Files: Use less instead of cat for big files to avoid terminal overload.

  • Search Power: Combine / with n/N to navigate logs or code efficiently.

  • Pipe Input: View command output with less (e.g., ls -l | less).

  • Customize: Set options in the LESS environment variable (e.g., export LESS="-N -i" in .bashrc).

  • Using less with Pipes

    You can pipe command outputs to less for paginated viewing. For instance, to browse a long directory listing:

    $ ls -l | less

    This lets you scroll through the output one page at a time instead of it flooding your terminal.