Skip to content

Linux less Command: Advanced File Viewing and Navigation

Published: at 03:37 PMSuggest Changes

The less command is a powerful file viewer in Linux that allows you to navigate through files efficiently. Unlike cat, it provides forward and backward navigation, search capabilities, and doesn’t load the entire file into memory.

Basic Syntax

less [options] file(s)

Common Options

Basic Movement

Search Navigation

Real-World Examples

1. Basic File Viewing

# View file with line numbers
$ less -N large_file.txt

# View multiple files
$ less file1.txt file2.txt

# View with raw control characters (colors)
$ less -R colored_output.log

2. Log File Analysis

# View log file from end
$ less +G /var/log/syslog

# Follow log updates
$ less +F /var/log/apache2/access.log

# Search in logs
$ less +/error /var/log/syslog

3. Source Code Reading

# View with line numbers and no wrap
$ less -NS source_code.cpp

# View multiple files
$ less *.py

Common Use Cases

  1. Log Analysis

    # View large log files
    less /var/log/syslog
    
    # Monitor log in real-time
    less +F /var/log/apache2/error.log
    
  2. Code Review

    # View source code
    less -N source.cpp
    
    # Compare files (use :n and :p)
    less file1.cpp file2.cpp
    
  3. Documentation Reading

    # View man pages
    man less | less
    
    # Read formatted text
    less README.md
    

Tips and Tricks

  1. Pattern Matching

    # Highlight search matches
    less -p"pattern" file.txt
    
    # Case-insensitive search
    less -i file.txt
    
  2. Line Numbers and Marks

    # Show line numbers
    less -N file.txt
    
    # Set mark at current position (m + letter)
    ma
    # Jump to mark (' + letter)
    'a
    
  3. Multiple File Handling

    # Navigate between files
    :n  # next file
    :p  # previous file
    

Best Practices

  1. Use Appropriate Options

    # For logs with color
    less -R logfile.log
    
    # For code review
    less -NMS sourcecode.cpp
    
  2. Efficient Navigation

    # Jump to percentage
    50p  # Go to 50% of file
    
    # Jump to line
    :100  # Go to line 100
    
  3. Search Effectively

    # Search and highlight
    less -p"ERROR" log.txt
    
    # Filter view
    &error  # Show only lines with "error"
    

Common Errors and Solutions

  1. Binary File Display

    # View binary safely
    less -f binary_file
    
    # Alternative: use hexdump
    hexdump -C binary_file | less
    
  2. Large File Handling

    # Memory efficient viewing
    less +F large_file.log
    
    # Use with tail
    tail -f large_file.log | less +F
    
  3. Character Encoding Issues

    # Handle UTF-8
    LESSCHARSET=utf-8 less file.txt
    

Advanced Usage

1. Custom Prompt

# Show detailed status line
export LESS='-M'

# Custom prompt format
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'

2. Preprocessing

# View compressed files
zless compressed.gz

# View formatted output
json_pp < data.json | less -R

3. Advanced Filtering

# Multiple pattern matching
&pattern1|pattern2

# Exclude pattern
&!pattern

Keyboard Shortcuts Reference

h           Help
q           Quit
f           Forward one window
b           Backward one window
d           Forward half window
u           Backward half window
j or ↓      Forward one line
k or ↑      Backward one line
G           End of file
g           Beginning of file

Searching

/pattern    Search forward
?pattern    Search backward
n           Next occurrence
N           Previous occurrence
&pattern    Show only matching lines

Marks

ma          Mark current position as 'a'
'a          Go to mark 'a'
''          Return to previous position

Remember that less is more than just a file viewer – it’s a powerful tool for navigating and analyzing text files. Its ability to handle large files efficiently and provide advanced search capabilities makes it indispensable for system administration and development tasks.


Previous Post
Linux head and tail Commands: View File Beginnings and Endings
Next Post
Linux cat Command: Concatenate and Display File Contents