What is less command in linux

9 Simple Ways to Effectively use Less Command in Linux

Less is an awesome Linux command utility for viewing text files. Here are some essential less command examples to use it effectively.

There are ways to read text files in Linux command line. Cat is perhaps the most elementary command that almost every Linux user knows in this regard. But cat is not always the best option for displaying the content of a file.

Imagine that you have a text file with over a thousand line. Using the cat command will simply fill the entire terminal screen with those lines. You cannot perform any more operations like searching for a particular text after you have used the cat command here.

This is where less command helps you big time.

What is the less command in Linux?

With less, you can read large text files without cluttering your terminal screen. You can also search for text and monitor files in real time with it.

Some people prefer using Vim for reading large text files. But less is faster than Vim or other such text editors because it doesn’t read the entire file before starting. Since less is ‘read only‘, you don’t have the risk of accidentally editing the files you are viewing.

The syntax for the less command is extremely simple:

There are numerous options with less command but it’s better to focus on the practical usage that will be more useful for you.

Let’s see some of the most useful examples of less command in Linux.

Practical examples of less command in Linux

Less Command examples in Linux

Let’s see how to use the less command in Linux with some practical examples.

It is better to work with a big file to understand the usage of the less command. Instead of creating a huge text file by hand, I would advise copying the existing file from /etc/services to your home directory or wherever you want to practice these commands.

/etc/services is a big file with hundreds of line and once copied, you can use it for your practice.

1. View a text file with less

As showed in the syntax, you can use the less command to view a file in the following fashion:

The output will be something like this:

View text files with less command in Linux

Note: If you view a short text file with less, you would see empty blank lines at the top. Don’t panic. There are no extra lines in your file. It’s just the way less displays them.

2. Exit from less

If you are not used to of less command, you might struggle to find how to exit less. Trust me it’s not at all complicated. Simply press ‘q’ at any given point to exit from less.

I added the exiting before so that you may follow the rest of the less command examples easily as you would need to exit the files between different examples (if you are practicing the commands while reading this article).

Читайте также:  Linux после установки черный экран

3. Moving around in less

The output of less is divided into sort of pages. You’ll see only the text that fills up to your terminal screen.

You can use the up and down arrow keys to move line by line. If you want to move page by page, use space key to move to next page and ‘b’ key to go back to the previous page.

If you want to move to the beginning of the file, use ‘g’ key. If you want to go to the end of the file, press ‘G’ key.

  • Up arrow – Move one line up
  • Down arrow – Move one line down
  • Space or PgDn – Move one page down
  • b or PgUp – Move one page up
  • g – Move to the beginning of the file
  • G – Move to the end of the file
  • ng – Move to the nth line

4. Display line numbers with less

If you want to see the line numbers in the less command output, you can use the option N in the following manner:

You’ll see an output like this:

Displaying line numbers in less

5. Finding text in less

If you have a large text file, it’s better to search for a specific piece of text rather than reading it line by line in order to find it manually.

To find a word or phrase or even a regex pattern, press / and type whatever you want to find.

As you can see, the matched text is highlighted.

Searching text in less

If there is more than one match, you can move to the next matched text by pressing ‘n’ key. You can move back to the previous match with ‘N’ key.

The ‘/pattern’ performs a forward search. You can use ‘?pattern’ to perform a backward search. Personally, I prefer doing a forward search and then pressing n or N to cycle through all the matches. No need to worry about a forward or backward search.

By default, search in less is case sensitive. To ignore case, you can use less with -I option

If you forgot to use this option, don’t worry. You can also press ‘-I’ key combination before performing a search inside less.

6. Marking interesting points

While you are scrolling through a big text file and you find something interesting, but you also need to continue checking the file, how would you remember that interesting point? The answer is marking.

Less allows you to add marks, sort of flags, to any line. You can add a mark by pressing the key ‘m’ followed by a letter.

When you want to go back to this mark, simply press ‘ followed by that letter:

You are not limited to a single mark point. You can add multiple marks in less. Simply use a different letter for each marked position like mb, mc etc.

Читайте также:  Pycharm remove license linux

Do note that this bookmarking is not permanent. You’ll lose it as soon as you exit the less command.

7. Monitor files in real-time with less command

You probably already know that you can use tail command to monitor log files in real-time.

You can also do the same with less command. You can see the new lines being added to a file in real-time by using the +F option.

It will display the last page of the file and then wait for the new data to be added. Note that you cannot perform the regular moving up and down, back and forth in this mode.

View files in real time with less command

To exit the real time monitoring of log files, press Ctrl+C. With this, you will be back to the normal view of the less command and can exit the file normally by pressing the Q key.

FREE Uptime Monitoring

8. View multiple files with less command

I’ll be honest with you. This is not my favorite less command example but you can totally do this.

To open multiple files with less, simply input the file names one by one:

You’ll see that it lists the file name along with its position in the list of the files.

Viewing multiple files with less

You can view other files in the list using these keys:

9. Using less command with pipes

The less command can be used in conjugation with other commands using pipes. It is particularly useful when you know that the output of a certain command is going to be huge.

For example, the output of dmesg command is usually in thousands of lines. You don’t want it to flood your screen and you won’t be able to analyze the output as well. Pipe it with less and you’ll have a more friendly way of reading this output.

Bonus Tip: Edit a file with less command in Linux

No, you cannot do that. You cannot edit a file in less.

One of the biggest advantages of less command is that it provides a ‘read-only’ view.

If you cannot edit text with less then why did I add this as a less command example? Because when you feel like you need to edit the file you are viewing, simply press the ‘v’ key.

It will open the file in the default command line text editor of your Linux system. For Ubuntu-based system, it should be opened in Nano editor.

There is more with less

I hope you find these less command examples useful while using Linux. Of course, there could be many more usage of less commands. You can explore them by going through all the options of less command.

There is a similar ‘more command‘ that is also popular. I prefer using less because it is more user friendly.

If you have any questions or suggestions, please share them in the comments section below.

Источник

How to Use the less Command in Linux with Examples

The less command is a Linux terminal pager that shows a file’s contents one screen at a time. It is useful when dealing with a large text file because it doesn’t load the entire file but accesses it page by page, resulting in fast loading speeds.

Читайте также:  Установить telegram linux debian

less is equipped with interactive features allowing users to navigate forward and backward through the file. The less tool is more advanced and versatile than other terminal pagers, such as more and most .

In this tutorial, you will learn to use the less command in Linux.

Learn to use the Linux less command.

Less Command Syntax

The general syntax for the less command is:

The [options] customize the less command output. Running the command without options outputs the input file’s contents in the default manner.

The following section lists the most common less command options.

Less Command Options

Without additional options, less displays the output in the default format and assumes default behavior. The options modify the output or change how less acts while processing the file.

The most used less command options are:

Option Description
-E less automatically exits upon reaching the end of file.
-f Forces less to open non-regular files (a directory or a device-special file).
-F Exit less if the entire file can be displayed on the first screen.
-g Highlights the string last found using search. By default, less highlights all strings matching the last search command.
-G Removes all highlights from strings found using search.
-i Ignores case sensitivity during search.
-J Displays a status column on the left side of the screen. The status column shows the lines that matched the current search and any lines marked using the m or M command.
-m Instructs less to prompt verbosely (similar to more ), showing the percentage into the file. By default, less prompts with a colon.
-M Instructs less to prompt even more verbosely than more .
-n Removes line numbers from the screen.
-N Displays line numbers at the beginning of each line.
-o[file_name] Causes less to copy its input to the specified file. This option applies only when the input file is a pipe ( | ), not an ordinary file. For existing files, less asks for confirmation before overwriting the file.
-p[pattern] Instruct less to start at the first occurrence of the specified pattern in the input file.
-Q Enforces quiet operation that silences the terminal bell.
-s Merges consecutive blank lines into a single blank line.
-X Disable clearing the screen after quitting less .
-z[n] Changes the default scrolling window size to the specified n lines.

For a complete list of options, refer to the less help file by running:

Less command options in the help file.

The less command accepts keyboard shortcuts that facilitate text navigation, especially when reading large files. The following table comprises the most commonly used shortcuts:

Shortcuts Action
Down Arrow, Enter, e, j One line forward.
Up Arrow, y, k One line backward.
Space bar, Page Down One page forward.
Page Up, b One page backward.
Right Arrow Scroll right.
Left Arrow Scroll left.
Home, g Jump to the beginning of the file.
End, G Jump to the end of the file.
/[string] Search forward for the specified string.
?[string] Search backward for the specified string.
n Next match during a search.
N Previous match during a search.
q Quit less.

For a detailed list of navigation options, run:

Navigation options in the less command help file.

Note: To easily manage files with many lines, you can use Linux split command.

Источник

Оцените статью
Adblock
detector