How to use the command 'less' (with examples)

How to use the command 'less' (with examples)

The ’less’ command is a file viewer that allows for interactive reading of a file, enabling scrolling and search functionality. It is commonly used in the command line interface to quickly preview and navigate through the contents of a file without opening a text editor.

Use case 1: Open a file

Code:

less source_file

Motivation: This use case allows you to open a file in the ’less’ viewer and read its contents interactively. It is useful when you want to quickly review a file without the need to open it in a text editor.

Explanation: The ‘source_file’ argument represents the path to the file you want to open. This can be a relative or absolute path.

Example output: The content of the file ‘source_file’ will be displayed in the ’less’ viewer, allowing you to scroll up and down to read its contents.

Use case 2: Page down/up

Code:

<Space> (down)
b (up)

Motivation: When reviewing a large file, it can be helpful to scroll through the content by page rather than line by line. This use case allows you to navigate through the file quickly.

Explanation: Pressing the <Space> key will scroll the contents of the file down by one page. On the other hand, pressing the b key will scroll the contents up by one page.

Example output: If you press <Space> in the ’less’ viewer, the content of the file will scroll down by one page, allowing you to continue reading from where you left off.

Use case 3: Go to end/start of file

Code:

G (end)
g (start)

Motivation: Sometimes you may want to quickly jump to the beginning or end of a file. This use case allows you to navigate directly to the desired position in the file.

Explanation: Pressing the G key will move the cursor to the end of the file, while pressing the g key will move the cursor to the beginning of the file.

Example output: If you press G in the ’less’ viewer, the cursor will move to the end of the file, allowing you to view the last part of the content.

Use case 4: Forward search for a string

Code:

/something

Motivation: When dealing with large files, finding specific information can be time-consuming. This use case allows you to search for a specific string in the file, making it easier to locate relevant information.

Explanation: The /something argument represents the string you want to search for in the file. After entering the command, ’less’ will highlight the first occurrence of the string. Press n to navigate to the next occurrence, and N to navigate to the previous occurrence.

Example output: If you enter /something in the ’less’ viewer, it will search for the first occurrence of the string “something” in the file and highlight it.

Use case 5: Backward search for a string

Code:

?something

Motivation: Similar to the forward search, backward search allows you to quickly locate specific information in a file. This can be useful when you want to find a previous occurrence or backtrack in the file.

Explanation: The ?something argument represents the string you want to search for in the file. ’less’ will highlight the last occurrence of the string. Press n to navigate to the previous occurrence, and N to navigate to the next occurrence.

Example output: If you enter ?something in the ’less’ viewer, it will search for the last occurrence of the string “something” in the file and highlight it.

Use case 6: Follow the output of the currently opened file

Code:

F

Motivation: In case the file you have opened using ’less’ is being updated with new content, this use case allows you to follow the output in real-time, similar to the ’tail’ command.

Explanation: Pressing the F key will cause ’less’ to enter follow mode, where it will continuously display the new content being appended to the file.

Example output: If you press F in the ’less’ viewer, it will start following the file and display any new content that is added to the file.

Use case 7: Open the current file in an editor

Code:

v

Motivation: Sometimes you may want to edit the file you are currently viewing in the ’less’ viewer. This use case allows you to open the file in an editor, enabling you to make changes if needed.

Explanation: Pressing the v key will open the current file in the default editor configured on your system.

Example output: If you press v in the ’less’ viewer, it will open the current file in the default editor, allowing you to make changes to the content.

Use case 8: Exit

Code:

q

Motivation: Once you have finished reviewing the file, you may want to exit the ’less’ viewer. This use case allows you to exit the viewer and return to the command line interface.

Explanation: Pressing the q key will exit the ’less’ viewer and return to the command line.

Example output: If you press q in the ’less’ viewer, it will close the viewer interface and return you to the command line.

Conclusion:

The ’less’ command provides a simple and efficient way to preview and navigate through the contents of a file in a command line environment. With its various features, such as scrolling, searching, and following file updates, ’less’ is a powerful tool for quickly reviewing file content without the need for a text editor.

Related Posts

How to use the command mmdc (with examples)

How to use the command mmdc (with examples)

The mmdc command is a CLI for mermaid, a diagram generation tool with a domain-specific language.

Read More
How to use the command bmaptool (with examples)

How to use the command bmaptool (with examples)

bmaptool is a command-line tool used to create or copy block maps intelligently.

Read More
Bower Command Examples (with examples)

Bower Command Examples (with examples)

1: Installing a project’s dependencies bower install Motivation: This command is used to install all the dependencies listed in the bower.

Read More