less

text_processingLinux/Unix/macOS
The less command is one of the most frequently used commands in Linux/Unix-like operating systems. less File pager that allows backward and forward navigation through file contents

Quick Reference

Command Name:

less

Category:

text_processing

Platform:

Linux/Unix/macOS

Basic Usage:

less [options] [arguments]

Common Use Cases

    Syntax

    less [options] file...

    Options

    Option Description
    -? or --help Display help
    -a or --search-skip-screen Search skips current screen
    -b N or --buffers=N Set buffer size
    -B or --auto-buffers Don't automatically allocate buffers for pipes
    -c or --clear-screen Repaint by clearing rather than scrolling
    -d or --dumb Suppress error messages about terminal capabilities
    -e or --quit-at-eof Exit automatically when reaching end of file
    -E or --QUIT-AT-EOF Exit automatically after reaching EOF twice
    -f or --force Force opening non-regular files
    -F or --quit-if-one-screen Exit if entire file fits on first screen
    -g or --hilite-search Highlight only the current match of a search pattern
    -G or --HILITE-SEARCH Don't highlight any matches of a search pattern
    -i or --ignore-case Ignore case in searches that do not contain uppercase
    -I or --IGNORE-CASE Ignore case in all searches
    -j N or --jump-target=N Position target line on screen
    -J or --status-column Display a status column on the left
    -k file or --lesskey-file=file Use a lesskey file
    -K or --quit-on-intr Exit less on interrupt
    -L or --no-lessopen Don't use the LESSOPEN preprocessor
    -m or --long-prompt Show detailed prompt
    -M or --LONG-PROMPT Show even more detailed prompt
    -n or --line-numbers Don't use line numbers
    -N or --LINE-NUMBERS Display line numbers
    -o file or --log-file=file Copy input to file
    -O file or --LOG-FILE=file Copy input to file (unconditionally overwrite)
    -p pattern or --pattern=pattern Start at pattern
    -P prompt or --prompt=prompt Define new prompt
    -q or --quiet Completely quiet (never ring the terminal bell)
    -Q or --QUIET Completely quiet, overrides -q
    -r or --raw-control-chars Display "raw" control characters
    -R or --RAW-CONTROL-CHARS Same as -r, but try to keep screen appearance
    -s or --squeeze-blank-lines Squeeze multiple blank lines into one
    -S or --chop-long-lines Cut off long lines rather than wrapping
    -t tag or --tag=tag Find a tag
    -T tagsfile or --tag-file=tagsfile Use alternative tags file
    -u or --underline-special Use underlining for special characters
    -U or --UNDERLINE-SPECIAL Don't use underlining for special characters
    -w or --hilite-unread Highlight first unread line after a forward move
    -W or --HILITE-UNREAD Highlight first unread line after any move
    -x N or --tabs=N Set tab stops
    -X or --no-init Don't use termcap init/deinit strings
    -y N or --max-forw-scroll=N Set forward scroll limit
    -z N or --window=N Set scroll size (in lines)
    -Z or --no-scroll Disable scrolling and follow terminal
    -" charset or --quotes=charset Set quote characters
    -~ or --tilde Don't display tildes after end of file
    -# N or --shift=N Set horizontal scroll amount

    Examples

    How to Use These Examples

    The examples below show common ways to use the less command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    less file.txt
    View the contents of file.txt.
    less -N file.txt
    View file.txt with line numbers.
    # Advanced Examples Advanced
    less -S file.txt View file.txt with long lines truncated rather than wrapped. less +100 file.txt Open file.txt at line 100. less +/pattern file.txt Open file.txt and search for "pattern". less file1.txt file2.txt View multiple files (use :n and :p to navigate between files). find /var -name "*.log" | less Pipe command output to less. less -X file.txt View file without clearing the screen when exiting. less -g file.txt Highlight only the current match of a search pattern. less -I file.txt Perform case-insensitive searches.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'less' command is a powerful and flexible file pager that allows users to view text files and command output with efficient navigation capabilities. It improves upon the older 'more' command by providing bidirectional scrolling and many additional features for examining file contents. Key features of the less command: 1. Bidirectional Navigation: Unlike simpler pagers, less allows both forward and backward navigation through files, making it more convenient for examining large documents. 2. Search Functionality: less offers robust search capabilities with both forward (/) and backward (?) search, support for regular expressions, and options for case sensitivity control. 3. Multiple File Support: less can open multiple files at once, allowing users to switch between them (using :n for next and :p for previous) without restarting the program. 4. Preprocessor Integration: Through the LESSOPEN environment variable, less can automatically process and display various file formats, even non-text files like PDFs or archives, by using appropriate preprocessors. 5. Customizable Display: Options for line numbering, line wrapping control, status information, and highlighting make less adaptable to different viewing needs and preferences. 6. Efficient Memory Usage: less doesn't need to load the entire file into memory, making it suitable for viewing very large files that other text editors might struggle with. 7. Command History: less maintains a history of commands, accessible with the 'q' key, making it easy to repeat complex searches or navigation commands. 8. Marking Positions: Users can mark positions in a file with lowercase letters and return to them later, which is invaluable when navigating large documents. Common use cases for the less command include: - Viewing log files, especially large ones that would be impractical to load into a text editor - Examining command output that's too lengthy to fit on the screen - Reading documentation files and manpages (in fact, the man command typically uses less as its pager) - Inspecting configuration files during system administration tasks - Quick review of text file contents without modifying them - Following log files in real-time with the follow mode (F) The less command is highly configurable through both command-line options and key bindings. Users can set preferences in the LESS environment variable or create a .lesskey file to customize key bindings. This flexibility, combined with its efficiency and powerful features, has made less the standard pager on most Unix-like systems, including Linux and macOS. For users new to less, some essential commands to remember while using it include: - Space or f: forward one page - b: backward one page - g: go to first line - G: go to last line - /pattern: search forward for pattern - ?pattern: search backward for pattern - n: repeat previous search - q: quit less

    Related Commands

    These commands are frequently used alongside less or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the less command works in different scenarios.

    $ less
    View All Commands