Linux less show line number

9 Simple Ways to Effectively use Less Command in Linux

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

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:

Viewing text file with less

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 existing 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).

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
Читайте также:  Как пользоваться tcp optimizer windows

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 are 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 press 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.

Tip: If you use &pattern it will display only the lines that matched the pattern.

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? 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.

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.

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.

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:

  • :n – view the next file in the list
  • :p – view the previous file in the list

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.

Читайте также:  Scp ������ mac os

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 it in the comments section below.

Источник

How to get an specific amount of lines in the console with less? [closed]

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago .

Can I get the specified number lines at a time by using the less command? I want just ex. show 20 lines even if my screen allows more.

3 Answers 3

less works with screens of text. The «screen» is the full size of the terminal.

less —window=n can tell less to only use so many rows at a time. That being said the option is not always available.

If you only want «some» output try tail -n 20 /file.txt for the last 20 lines, or I personally use head -n 20 | tail -n 10 to get the middle 10 lines.

Display a file from line number X:

Use the -N option to output line numbers

displays from line number 15000 with line numbers displayed

from less manual, to scroll n lines at a time, but shows a whole screen-full.

Changes the default scrolling window size to n lines. The default is one screenful. The z and w commands can also be used to change the window size. The «z» may be omitted for compatibility with some versions of more. If the number n is negative, it indicates n lines less than the current screen size. For example, if the screen is 24 lines, -z-4 sets the scrolling window to 20 lines. If the screen is resized to 40 lines, the scrolling window automatically changes to 36 lines.

Источник

less Command Display Line Numbers

I ‘m a new Linux user. How can I display line numbers using the less command on Linux or Unix-like system using bash/ksh shell?

You can easily display line numbers using less command. All you have to do is pass either -N or —LINE-NUMBERS option to the less command. This option forces less to show a line number at the beginning of each line in the screen.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix operating system
Est. reading time 2 minutes

How to display line numbers in less

The basic syntax is as follow for both GNU/Linux and BSD version of less found on FreeBSD/macOS and other operating systems:
less -N filename
## OR ##
less —LINE-NUMBERS filename

In this example, show line numbers for /etc/passwd file using GNU/less, type:

Fig. 01: less displaying the line numbers

Suppresses line numbers

The default (to use line numbers) may cause less to run more slowly in some cases, especially with a very large input file. Suppressing line numbers with the -n option will avoid this problem. In other words we can hide displaying line numbers:
less -n /path/to/file

How to toggle line number without quitting less

Say you started less as follows:
less /etc/hosts
Now you want line numbers? What would you do? Press:
-N
Finally hit the ENTER key.

How do I force less to display line number by default?

The less command can read various options from the environment variable “LESS”. For example, to avoid typing less -N filename each time less is invoked, you might type following setenv command on tcsh/csh shell

Источник

Going to a specific line number using Less in Unix

I have a file that has around million lines. I need to go to line number 320123 to check the data. How do I do that?

5 Answers 5

With n being the line number:

  • ng : Jump to line number n. Default is the start of the file.
  • nG : Jump to line number n. Default is the end of the file.

So to go to line number 320123, you would type 320123g .

Copy-pasted straight from Wikipedia.

To open at a specific line straight from the command line, use:

If you want to see the line numbers too:

You can also choose to display a specific line of the file at a specific line of the terminal, for when you need a few lines of context. For example, this will open the file with line 320123 on the 10th line of the terminal:

You can use sed for this too —

This will print line number 320123.

If you want a range then you can do —

If you want from a particular line to the very end then —

From within less (in Linux):

Used alone, g and G will take you to the first and last line in a file respectively; used with a number they are both equivalent.

An example; you want to go to line 320123 of a file,

press ‘g’ and after the colon type in the number 320123

Additionally you can type ‘-N’ inside less to activate / deactivate the line numbers. You can as a matter of fact pass any command line switches from inside the program, such as -j or -N.

NOTE: You can provide the line number in the command line to start less (less +number -N) which will be much faster than doing it from inside the program:

This will open a file displaying the line numbers and starting at line 12345

Source: man 1 less and built-in help in less (less 418)

Источник

This post and this website contains affiliate links. See my disclosure about affiliate links.

how to show line numbers in the “less” command line file viewer

No matter which file viewer you are using, you more often than not would want to show line numbers along with the contents of the file. The less file viewer is no different. First, let me describe what the less utility does, before I tell you how to show line numbers in less.

The less is a command line utility that allows you to view text file content easily right from the command line. It is similar to the more utility with some additional functionalities. The main difference between the two utilities lies in its scrolling capabilities: more allows you to scroll forward and view pages or a screen full of content, while less allows you to scroll both forward and backwards while viewing content as pages.

The less utility does not show line numbers of the lines by default when viewing the file content. Quite often it is informative to display the line numbers while viewing the file. You have several different options if you want to show line numbers with the less utility.

Command Line Option

If you are typing in the less command from the command prompt, then you can easily use the command line argument -N or –LINE-NUMBERS to show line numbers at the beginning of each line on the display.

bash$ less —LINE-NUMBERS filename.txt

Or you can use the short code or abbreviation -N with the less command as in the example below.

bash$ less -N filetwo.txt

Of course, you will need to remember to use the command line option as you are typing in the command. Not a big deal most of the time, but you do have other options if you forget. You can show line numbers and toggle it from inside the viewer as well.

Inside the Viewer

You can also toggle the line numbers from inside the less viewer, as you are viewing the file content. This is useful if you are already inside the viewer or if you want to remove the line number display. As with most command line options of less, you can also use it from with in the viewer…

When the file content is being displayed, just type -N using the keyboard and followed by Enter to display line numbers. You can hide the line numbers by typing -N (or -n) again followed by Enter from with in the viewer. This is a quick way to toggle line numbers and much more convenient than the command line option.

Changing the Default

If you want to always show line numbers when using less, then you can modify your environment in order to do so. There are a couple of ways to do this: by using a command alias or by modifying the environment variable.

You can create an alias for the less command such that the -N option is always passed on to the less command. In bash, you can create a new alias as shown below. The syntax might vary slightly depending on the shell you are using…

bash$ alias less=»less -N»

Another option is to create or modify the environment variable called LESS. Whatever options you set in this environment variable will be used as the default by the less utility.

bash$ export LESS=»-N»;

Of course, you can now add these to your command configuration files such as .bashrc or .cshrc or .bash_profile etc to make this change permanent and persist it over shutdowns and reboots.

Источник

Читайте также:  После сброса биос не запускается windows
Оцените статью