less (pager for viewing file content, allows scrolling)
The less command is an advanced file pager that lets you view file content one screen at a time. Unlike cat, it is designed for large files, allowing you to scroll both forwards and backwards and to search for text within the file.
Example 1: Open a file for viewing
# Open a large log file for interactive viewing
less /var/log/messages
Explanation: This command opens the specified log file in the less interface. It loads quickly because it doesn't read the entire file into memory at once, making it ideal for large files.
Example 2: Searching for text within less
# While viewing a file in less, press '/' then type your search term
/error
Explanation: Once a file is open in less, typing / followed by a text pattern (like "error") initiates a forward search. Pressing n finds the next occurrence, and N finds the previous one.
Example 3: Viewing a file with line numbers
# Open a file and display line numbers for each line
less -N /etc/fstab
Explanation: The -N option displays line numbers, which is very useful for navigating source code or configuration files. You can also toggle line numbers on and off by typing -N while inside less.
Example 4: Following a file as it grows
# View a log file and automatically scroll to the end as new lines are added
less +F /var/log/cron
Explanation: Starting less with +F is similar to tail -f. It goes to the end of the file and waits for new data, displaying it as it's written. Press Ctrl-C to stop following and regain interactive control.
Example 5: Opening multiple files
# Open multiple configuration files to view them sequentially
less httpd.conf ssl.conf
Explanation: You can provide multiple files to less. Once open, type :n to move to the next file in the list and :p to move to the previous file.
Example 6: Viewing output from another command
# Pipe the output of the 'dmesg' command into 'less' for scrollable viewing
dmesg | less
Explanation: less works perfectly with pipes. This allows you to take the potentially long output of any command and view it in a scrollable, searchable interface.
Example 7: Case-insensitive searching
# Open a file and make all searches ignore case
less -i application.log
Explanation: The -i option makes searching case-insensitive. After launching less this way, searching for /username will find "username", "UserName", and "USERNAME".