Книга: Fedora™ Unleashed, 2008 edition
Paging Through Output with less
Paging Through Output with less
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 alpha bet. That is, -a
does something, -b
does something else, -c
, -d
, -e
… -x
, -y
, -z
— they all do things, with some letters even differentiating between upper- and lowercase. Furthermore, these are only the parameters used to invoke less
. After you begin viewing your text, even more commands are available to you. Make no mistake — less
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 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
. Instead of 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, +
, enables 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 done 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, you can tell it 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, type 50g
, or to go to the 75% point of the file, 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 you will move back to the same position. You can set up to 52 marks, named a-z and A-Z.
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:
$ less -MN 1.txt 2.txt 3.txt
That command loads all three files into less
, starting at 1.txt
. When viewing several files, less
usually tells you in which file you are, as well as numbering them: 1.txt (file 1 of 3)
should be at the bottom of the screen. That said, certain things do 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 the Tab key 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 straightaway. 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. You can repeat searches by pressing the Esc key 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 the 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.
- Printing the Contents of a File with cat
- Changing Directories with cd
- Changing File Access Permissions with chmod
- Copying Files with cp
- Printing Disk Use with du
- Finding Files by Searching with find
- Searches for a String in Input with grep
- Paging Through Output with less
- Creating Links Between Files with ln
- Finding Files from an Index with locate
- Listing Files in the Current Directory with ls
- Reading Manual Pages with man
- Making Directories with mkdir
- Moving Files with mv
- Listing Processes with ps
- Deleting Files and Directories with rm
- Printing the Last Lines of a File with tail
- Printing Resource Usage with top
- Printing the Location of a Command with which
- Moving Files with mv
- Printing the Location of a Command with which
- Demand Paging
- Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
- Drawbacks with restore
- OUTPUT chain
- Letting DHCP requests through iptables
- 7. AGGREGATION WITH INDEPENDENT WORKS
- Конструкция with-do
- Lesson Summary
- 3. Hexadecimal – the way we communicate with micros
- CHAPTER 3 Working with GNOME