Linux command
less 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
View a file
less [file]
View a file with line numbers
less -N [file]
Search for a pattern
/[pattern]
View the output
[command] | less
Follow a file
less +F [file]
Open a file at a specific line
less +[line_number] [file]
说明
less is a terminal pager program used to view the contents of text files one screen at a time. Unlike more, it allows backward navigation through files and does not require reading the entire file before displaying, making it efficient for large files. The program provides powerful search capabilities with regular expression support, can display line numbers, and handles multiple files in a single session. It is commonly used as the default pager for manual pages and is invaluable for examining log files and command output. Less can read from standard input, making it ideal for piping command output. The follow mode (+F) allows real-time monitoring of growing files, similar to tail -f but with the full navigation capabilities of less.
参数
- -N, --LINE-NUMBERS
- Display line numbers at the beginning of each line
- -S, --chop-long-lines
- Chop long lines instead of wrapping
- -i, --ignore-case
- Ignore case in searches (unless pattern has uppercase)
- -I, --IGNORE-CASE
- Ignore case in searches unconditionally
- -F, --quit-if-one-screen
- Exit immediately if entire file fits on first screen
- -R, --RAW-CONTROL-CHARS
- Output raw control characters (preserves colors)
- -X, --no-init
- Don't clear the screen on exit
- -g, --hilite-search
- Highlight only the current match, not all matches
- -n, --line-numbers
- Suppress line numbers (for performance)
- +F
- Follow mode, scroll forward and wait for more data
- + _command_
- Run specified command on startup (e.g., +/pattern to search)
- -p _pattern_
- Start at first occurrence of pattern
- -o _file_
- Copy input to file (when reading from pipe)
FAQ
What is the less command used for?
less is a terminal pager program used to view the contents of text files one screen at a time. Unlike more, it allows backward navigation through files and does not require reading the entire file before displaying, making it efficient for large files. The program provides powerful search capabilities with regular expression support, can display line numbers, and handles multiple files in a single session. It is commonly used as the default pager for manual pages and is invaluable for examining log files and command output. Less can read from standard input, making it ideal for piping command output. The follow mode (+F) allows real-time monitoring of growing files, similar to tail -f but with the full navigation capabilities of less.
How do I run a basic less example?
Run `less [file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -N, --LINE-NUMBERS do in less?
Display line numbers at the beginning of each line