The less command enables you to view large amounts of text in a more convenient way than the cat command. For example, your /etc/passwd file is probably more than a screen long, so if you run cat /etc/passwd, you are not able to see what the lines at the top were. Using less /etc/passwd enables you to use the cursor keys to scroll up and down the output freely. Type q to quit and return to the shell.
On the surface, less sounds like a very easy command; however, it has the infamy of being one of the few Linux commands that have a parameter for every letter of the alphabet. That is, -a does something, -b does something else, -c, -d, -e ... -x, -y, -zthey all do things, with some letters even differentiating between upper- and lowercase. Furthermore, it is only the parameter used to invoke less. After you are viewing your text, even more commands are available to you. Make no mistakeless is a complex beast to master.
Input to less can be divided into two categories: what you type before running less and what you type while running it. The former category is easy, so we shall start there.
We have already discussed how many parameters less is capable of taking, but they can be distilled down to three that are very useful: -m, -n, and +. Adding -m (this is different from -m!) enables verbose prompting in less. Rather than just printing a colon and a flashing cursor, less prints the filename, the line numbers being shown, the total number of lines, and the percentage of how far you are through the file. Adding -n (again, this is different from -n) enables line numbering.
The last option, +, allows you to pass a command to less for it to execute as it starts. To use this, you first need to know the commands available to you in less, which means we need to move onto the second category of less input: what you type while less is running.
The basic navigation keys are the up, down, left, and right cursors; Home and End (for navigating to the start and end of a file); and Page Up and Page Down. Beyond that, the most common command is /, which initiates a text search. You type what you want to search for and press Enter to have less find the first match and highlight all subsequent matches. Type / again and press Enter to have less jump to the next match. The inverse of that is ?, which searches backward for text. Type ?, enter a search string, and press Enter to go to the first previous match of that string, or just use ? and press Enter to go to the next match preceding the current position. You can use / and ? interchangeably by searching for something with / and then using ? to go backward in the same search.
Searching with / and ? is commonly used with the + command-line parameter from earlier, which passes less a command for execution after the file has loaded. For example, you can tell less to load a file and place the cursor at the first match for the search hello, like this:
less +/hello myfile.txt
Or, to place the cursor at the last match for the search hello: less +?hello myfile.txt
Beyond the cursor keys, the controls primarily involve typing a number and then pressing a key. For example, to go to line 50 and type 50g, or to go to the 75% point of the file and type 75p. You can also place invisible mark points through the file by pressing m and then typing a single letter. Later, while in the same less session, you can press ' (a single quote) and then type the letter, and it will move you back to the same position. You can set up to 52 marks, named az and AZ.
One clever feature of less is that you can, at any time, press v to have your file opened inside your text editor. This defaults to Vim, but you can change that by setting the editor environment variable to something else.
If you have made it this far, you can already use less better than most users. You can, at this point, justifiably skip to the next section and consider yourself proficient with less. However, if you want to be a less guru, there are two more things to learn: how to view multiple files simultaneously and how to run shell commands.
Like most other file-based commands in Linux, less can take several files as its parameters. For example:
That loads all three files into less, starting at i.txt. When viewing several files, less usually tells you which file you are in, as well as numbering them: i.txt (file 1 of 3) should be at the bottom of the screen. That said, certain things will make that go away, so you should use -m anyway.
You can navigate between files by typing a colon and then pressing n to go to the next file or p to go to the previous file; these are referred to from now on as :n and :p. You can open another file for viewing by typing :e and providing a filename. This can be any file you have permission to read, including files outside the local directory. Use Tab to complete filenames. Files you open in this way are inserted one place after your current position, so if you are viewing file 1 of 4 and open a new file, the new file is numbered 2 of 5 and is opened for viewing straight away. To close a file and remove it from the list, use :d.
Viewing multiple files simultaneously has implications for searching. By default, less searches within only one file, but it is easy to search within all files. When you type / or ? to search, follow it with a *you should see EOF-ignore followed by a search prompt. You can now type a search and it will search in the current file; if nothing is found, it looks in subsequent files until it finds a match. Repeating searches is done by pressing Esc and then either n or n. The lowercase option repeats the search forward across files, and the uppercase repeats it backward.
The last thing you need to know is that you can get to a shell from less and execute commands. The simplest way to do this is just to type ! and press Enter. This launches a shell and leaves you free to type all the commands you want, as per normal. Type exit to return to less. You can also type specific commands by entering them after the exclamation mark, using the special character % for the current filename. For example, du -h % prints the size of the current file. Finally, you can use !! to repeat the previous command.
Was this article helpful?