less is what is known as a pager in the world of Linux. Pagers are used to view files or output piped in. As their name implies, they’re used when there are multiple pages of text that can’t fit on the screen. Some people use their favorite editor to read log files but I always use a pager because pagers are read-only, there’s no possibility you’ll accidentally make a change to a log file and save it like there is with your favorite editor.
less was written in 1983 by Mark Nudelman, who still maintains it today. The name of it is a play on the name of an older pager called more which is unable to move backwards if you were viewing something piped to it. It can go backwards in a file but not if something is piped in to it, compare
more /var/log/dmesg which will allow you to go backwards but not
cat /var/log/dmesg | more.
Most people use just the basic features of less, they open up a log file or pipe the output of something to it, then they move around or search for certain things. In case you don’t know, here’s some of the basic commands to get around inside less, you’ll recognize some of these as coming from vi.
Getting around inside less:
CTRL-F– go forward one page
ALT-V– go backwards one page
g– go to the first line
G– go to the last line
/pattern– search forwards for a pattern
?pattern– search backwards for a pattern
n– repeat previous search pattern in the same direction you’re already going.
N– repeat previous search pattern in the opposite direction you were going.
If you already know less, there’s nothing shocking in there, although I’ve seen a lot of people that didn’t know you can use
n to keep searching for your pattern – they use
/ and return to keep searching which becomes very annoying very quickly. Also, lots of people don’t realize that hitting
G when viewing a file that is being written to will make it re-read the file, including the new lines added.
That covers the basics and works great for most people. When you’re comfortable using something you usually don’t read the manual for it since you feel you already know it, but if you take the time to read the man page for less, there’s a lot of great features people don’t know about.
I’ve included some of the ones I use a lot below that make my life easier. You’ll want to type these in just as they appear, so when it says
-N you type that directly in to less, with the dash and everything. It might look like it’s a flag you pass it when you start it, and you can do that, but you can also type them after you’re already inside less.
Nice features inside less:
&pattern– only display lines that match your pattern
&!pattern– exclude lines that match your pattern
-N– enable/disable showing of line numbers
-I– enable/disable ignoring of case in search patterns
-G– enable/disable highlighting of matches
-S– enable/disable chopping off long lines instead of wrapping them
v– edit the file in your $EDITOR (technically it uses $VISUAL first and falls back to $EDITOR if $VISUAL isn’t defined)
F– keep reading the file as its being written, basically like
'a– set a mark, named ‘a’, at your current location then you can jump back to it using
'afrom anywhere in the file. replace ‘a’ with any character you want.
&!pattern to exclude lines that are normal inside a log file so the abnormal ones are easier to see. I use
v to make quick changes to config files if I see problems. I use -I to look for all possible capitalization schemes of the word ‘error’, I can’t tell you how many times something deciding to write ‘error’ as ‘Error’ or ‘ERROR’ has thrown me off.
Hopefully this will encourage you to read the man page for less since there’s more features I haven’t touched on. Did you know you can tell /pattern to only search what is on your screen instead of jumping to the first match it finds? Did you know less allows you to have multiple files open? You can switch between them and can even search all of them at the same time.Posted in: Linux