How to Use the Command 'less' (with Examples)
The less
command is a powerful utility for interacting with files in Unix-like systems. It allows users to view the contents of files in an interactive manner, progressively displaying portions as you scroll through it. Unlike other file view commands, less
does not read the entire file before starting, which makes it very efficient when dealing with large files. Moreover, it offers functionalities for searching within the file, navigating to specific lines, and even editing the file as needed. Let’s explore various use cases of less
to understand its capabilities.
Use case 1: Open a file for interactive reading
Code:
less source_file
Motivation: This command is used when you want to examine the contents of a file without editing it. The less
command is particularly useful because it allows for both forward and backward navigation through the file.
Explanation: In this command, less
is the command itself, and source_file
is the placeholder for the actual filename you wish to view. There are no additional arguments, making it straightforward—just replace source_file
with your desired file.
Example Output:
Upon executing the command, the terminal displays the beginning of source_file
. You can move up and down through the content using the keyboard.
Use case 2: Page down/up
Code:
<Space> (down), b (up)
Motivation: Paging through a document is essential when dealing with content that doesn’t fit on a single screen. The less
command provides intuitive keyboard shortcuts for seamless vertical navigation.
Explanation: <Space>
is a key binding in less
for moving down one page of text, while b
moves up one page. Here, <Space>
doesn’t need to be enclosed in command syntax since it’s a keypress action within the less
environment.
Example Output:
Using <Space>
will advance the view by one screenful of content, while pressing b
will return you to a previous screenful.
Use case 3: Go to the end/start of a file
Code:
G (end), g (start)
Motivation: Quickly navigating to the start or end of a file can save time, especially in large documents. The less
command supports instant jumping to these positions.
Explanation: Within the less
interactive session, pressing G
takes you directly to the end of the file, while pressing g
returns you to the start. These keypress commands streamline navigation compared to scrolling manually.
Example Output:
Pressing G
takes you to the bottom-most part of the document, while g
brings you back to the very start.
Use case 4: Forward search for a string
Code:
/something
Motivation: When looking for specific information within a file, searching for a particular string is invaluable. The forward search allows you to quickly locate desired content.
Explanation: While viewing a file in less
, typing /
followed by something
allows you to search for the string “something” within the document. This is case-sensitive and aligns with Unix-style search patterns. After initiating the search, use n
to navigate forward to the next instance and N
to go backwards.
Example Output:
The first occurrence of “something” within the file will be highlighted, and subsequent instances can be navigated through n
and N
.
Use case 5: Backward search for a string
Code:
?something
Motivation: Just like forward searching, backward searching helps users examine content in reverse order, providing flexibility in how information is retrieved.
Explanation: Pressing ?
followed by something
initiates a backward search for “something” in the file. Much like the forward search, n
and N
continue to be useful for navigation through found items.
Example Output:
The search will highlight the first instance of “something” preceding the current screen position, which can then be cycled through backwards using n
and N
.
Use case 6: Follow the output of the currently opened file
Code:
F
Motivation: Monitoring logs files as they’re being updated in real-time is essential for tasks like debugging or tracking changes. The less
command’s follow mode is very useful for this purpose.
Explanation: Within less
, pressing F
will place you in a mode similar to tail -f
, where new content appended to the file displays live. This is particularly useful for log files.
Example Output:
The command will display new lines as they’re added to the open file, mirroring real-time updates.
Use case 7: Open the current file in an editor
Code:
v
Motivation: Sometimes while viewing a file, you may decide you want to edit it. Less
allows you to seamlessly transition to an editor.
Explanation: Pressing v
within less
opens the currently-viewed file in the default text editor (typically vi
or vim
), allowing you to make edits as needed.
Example Output:
Your text editor opens inside the terminal with the current file loaded and ready for editing.
Use case 8: Exit
Code:
q
Motivation: Exiting the less
viewer is a routine task once you are done reading or searching a file. This action is a simple keystroke away.
Explanation: Pressing q
while in the less
view signals the program to quit, returning you to the terminal prompt.
Example Output:
The less
command exits, and the command line becomes active again for subsequent commands.
Conclusion
The less
command is an essential utility for anyone working in a Unix-like environment, providing fast, versatile file viewing options without the overhead of loading entire files into memory. With its intuitive navigation, convenient search capabilities, and easy access to editing, less
offers robust functionality to increase productivity in handling file content.