474

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?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Stole
  • 4,978
  • 3
  • 20
  • 18
  • 1
    What about doing that with `more`? :-) – matanster Jan 03 '18 at 18:48
  • 6
    because: [`less` is more, but more `more` than `more` is, so `more` is less `less`, so use more `less` if you want less `more`.](https://www.slackbook.org/html/file-commands-pagers.html) – HongboZhu Jan 14 '20 at 11:35

5 Answers5

655

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.

jameshfisher
  • 34,029
  • 31
  • 121
  • 167
n1r3
  • 8,593
  • 3
  • 18
  • 19
  • 17
    ... and don't hit enter after `g`, or you will jump one further line. – HongboZhu Jan 14 '20 at 11:28
  • This nor the other answers work for [the BusyBox version of less](https://boxmatrix.info/wiki/Property:less): `less 9581553g -N file.txt less: can't open '9581553g': No such file or directory` and also: `less +G -N file.txt less: can't open '+G': No such file or directory` – Wimateeka Jul 23 '20 at 17:54
  • 3
    @Wimateeka you should enter these commands being in `less` not in shell. First you open the file `less file.txt` and then enter your command `9581553g` – Stalinko Oct 26 '20 at 03:22
  • 1
    That makes so much more sense. I was treating it like `sed` or `awk` where you could give specific line numbers as parameters. Thank you for clarifying. – Wimateeka Oct 26 '20 at 18:12
  • if its a large file, it may take some time to seek to the position....be patient – Chris Rutledge Oct 08 '21 at 12:21
275

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

less +320123 filename

If you want to see the line numbers too:

less +320123 -N filename

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:

less +320123 -j 10 filename
Ian Mackinnon
  • 13,381
  • 13
  • 51
  • 67
68

You can use sed for this too -

sed -n '320123'p filename 

This will print line number 320123.

If you want a range then you can do -

sed -n '320123,320150'p filename 

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

sed -n '320123,$'p filename 
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
47

From within less (in Linux):

 g and the line number to go forward

 G and the line number to go backwards

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:

less +12345 -N /var/log/hugelogfile

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)

runlevel0
  • 2,715
  • 2
  • 24
  • 31
  • 2
    interestingly, google decided to take parts of your answer for their displayed answer when googling: "less go to line" (a good answer imo) – Matthias Dec 06 '16 at 15:11
5

For editing this is possible in nano via +n from command line, e.g.,

nano +16 file.txt

To open file.txt to line 16.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • adding -c to the nano command is also useful - doing so will cause nano to always show the current line number while editing – Chris Rutledge Oct 08 '21 at 12:24