LinuxCommandLibrary

less-than

View file content, one page at a time

TLDR

Redirect a file to stdin (achieves the same effect as cat file.txt |)

$ [command] < [path/to/file.txt]
copy

Create a here document and pass that into stdin (requires a multiline command)
$ [command] << [EOF] <Enter> [multiline_text] <Enter> [EOF]
copy

Create a here string and pass that into stdin (achieves the same effect as echo string |)
$ [command] <<< [string]
copy

Process data from a file and write the output to another file
$ [command] < [path/to/file.txt] > [path/to/file2.txt]
copy

Write a here document into a file
$ cat << [EOF] > [path/to/file.txt] <Enter> [multiline_data] <Enter> [EOF]
copy

Disregard leading tabs (good for scripts with indentation but does not work for spaces)
$ cat <<- [EOF] > [path/to/file.txt] <Enter> [multiline_data] <Enter> [EOF]
copy

Pass command output to a program as a file descriptor
$ diff <([command1]) <([command2])
copy

Open a persistent file descriptor
$ exec [3]<[path/to/file]
copy

SYNOPSIS

less [options] file...
command | less [options]

PARAMETERS

-i
    Ignore case distinctions during searches.

-N
    Display line numbers at the beginning of each line.

-S
    Chop long lines; do not wrap them to the next line. Lines longer than the screen width will be truncated.

-R
    Interpret ANSI color escape sequences, useful for viewing colored log files or command output.

-F
    Quit less automatically if the entire file fits on the first screen, acting like cat(1) for small files.

-X
    Disable sending terminal initialization/de-initialization strings. Useful in scripts to prevent screen clearing or changes.

-p pattern
    Start at the first occurrence of pattern in the file.

DESCRIPTION

less is a powerful terminal pager program used to view text files or command output one screenful at a time. It is an enhancement over the traditional more(1) command, providing superior navigation capabilities. Unlike more, less allows users to move both forward and backward through the file, search for patterns, and navigate efficiently even in very large files without loading the entire file into memory.

It's commonly used for reading log files, source code, or the output of commands that produce a lot of text, like ls -lR or dmesg. Its flexibility in navigation, searching, and handling of various file types (including compressed ones via filters) makes it an indispensable tool for Linux users and system administrators.

CAVEATS

While less is excellent for viewing, it is not a text editor. Changes to the file content cannot be made directly within less. For editing, use tools like vi(1) or nano(1). Its performance with extremely large binary files might be suboptimal, as it's primarily designed for text.

KEY BINDINGS

less uses a variety of keyboard commands for navigation and searching, many of which are similar to vi(1).

  • Space or f: Scroll forward one screen.
  • b: Scroll backward one screen.
  • g: Go to the beginning of the file.
  • G: Go to the end of the file.
  • /pattern: Search forward for pattern.
  • ?pattern: Search backward for pattern.
  • n: Repeat the last search in the same direction.
  • N: Repeat the last search in the opposite direction.
  • q: Quit less.

ENVIRONMENT VARIABLES

less's behavior can be customized using environment variables, notably:

  • LESS: Specifies default command-line options. For example, export LESS='-iN' will always enable case-insensitive search and line numbers.
  • LESSKEY: Points to a file containing custom key bindings for less.
  • LESSOPEN / LESSCLOSE: Define commands for pre-processing files before opening and after closing, allowing less to handle compressed files (like .gz or .bz2) transparently.

HISTORY

less was created by Mark Nudelman in 1983 as an improved version of the Unix more(1) command. The name itself is a humorous play on more, implying it is 'less' restrictive and offers 'more' functionality. It quickly gained popularity due to its ability to move backward in a file, a feature lacking in more. Over the years, it has become a standard utility on virtually all Unix-like operating systems and is often the default pager for commands like man(1).

SEE ALSO

more(1), cat(1), tail(1), head(1), grep(1), vi(1), man(1)

Copied to clipboard