The "cat" command is one of the most popular linux commands. It is used to display a file in the terminal. However, one problem is that when using "cat" to read a big file, it will display the whole file in the terminal so you will have to scroll up a huge wall of text to read the file. The "less" command is somewhat similar to "cat", it is also used to read a file but "less" displays only a part of the file instead of the whole file. To read files with "less", you just need to scroll up and down. The real advantage of "less" is that because it doesnt read the entire file so with very huge files, "less" will start faster than "cat" or any text editor like vi.
The usage of "less" is simple like this:
"less" displays the beginning part of the file only, to scroll up and down, you can use the keys "j" and "k" or the arrow keys, you can use the "enter" key to scroll down as well.
Here are some tips you can use with "less":
- Display line numbers
To display line numbers before each line, the command is:
less -N filename
- Open multiple files
You can open multiple files with "less", the command is:
With this command, only the first file will be displayed in the terminal. To move to the next file, you just need to type this command in the terminal:
less filename1 filename2 filename3
To move back to the previous file, the command will be:
You can also replace "n" with "p" in the above commands with a certain number N and the N-th file will be displayed.
To remove the current file, the command will be:
- Bookmark scrolling
When opening a huge file, there will be some certain lines you need to check again. Instead of remember the line numbers, you can bookmark the line with this command:
Just replace "&" with any letter you want and keep scrolling down to read the file. When you want to go back to the bookmarked line, the command will be:
(Replace "&" with the letter you picked before.)
You can search forward through the file with this command:
The command to search backward is:
To quit less, you can just hit the letter "q" or hit "Shift + zz"
To know more about "less", just run: