Unix tip: Making do with less
The head, tail and more commands are among the first ten or twenty commands that most beginning Unix users are going to learn. A far more intriguing command for many of the tasks they will end up using these commands for is the seemingly modest, but far more versatile, less.
The name "less" is obviously something of a spoof, meant to contrast the less command with the more familiar more. It's versatility, however, has converted a lot of Unix users to exclusive use of less.
Less allows you view files and, at the same time, do basic find operations. Unlike more, it allows you to search both forwards and backwards for your search string.
To open a file using less, type the less command followed by the file name. You can search for text with vi commands such as / (look forward) and ? (look backward). You can page down with f (forward) and up with b (backward). You can move up or down one line at a time with e (down one) or y (up one) or with the more obvious up and down arrows that I much prefer. Alternately, you can use the Enter key to move down one line at a time as you would with more.
Less also allows you to move ahead or back a page at a time using the space bar (ahead) or pressing "b" (back). Pressing h brings up a help screen with a summary of less commands. As in vi, you can move to the top of the file with g and the bottom with G. Your left and right arrow keys will move you one character at a time to the left or right within your text.
The n (next) command will repeat your command as it does in vi. Typing N will bring up your previous match.
Less also adjusts to the size of your window. Displaying a log file in an 80 columns window with lines wrapped around when you prefer to see them one line per screen line? No problem, just stretch your window! The text will adjust itself before your eyes. Or, if you prefer, use your left and right arrows to move to the left and the right within your text.
And this is just scratching the surface! Take a look at the help menu and you'll see there's a lot more to this little command than you'd expect.
And here's one idea you might not have thought of. If you need to look at a log file, such as syslog or messages, you are most likely going to be more interested in the more recent lines. So, you are likely to use the tail command. If you then need to see a little more, you might issue a second tail command, giving it an argument such as -50. A more effective way to do this might be to use the less command, then jump down to the very bottom of the file with a shift-g sequence. From there, you back scroll back up in the file indefinitely (well, at least up to the top of the file). Enter q to exit.
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
jfruh
Apple syncing patent can't come soon enough
pasmith
New Twitter features borrow from 3rd party clients
Esther Schindler
Open Source Changes the Software Acquisition Process
mikelgan
How to set up continuous podcast play on the new iTunes
David Strom
Five important Windows 7 mobility features
sjvn
Guard your Wi-Fi for your own sake
Sandra Henry-Stocker
Grepping on Whole Words
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.














Getting the most out of making do
I've used less for years. However, a co-worker suggested to me that I look into the text file browser "most". It performs similar functionality to more and less. However, it provides a variety of additional functions that the older pagers don't.For instance, most can display an arbitrary number of windows containing either different views of the same file or different files. It can display files with embedded binary, and can attempt to display underlining and overstrike sequences. If the file being viewed has lines wider than the screen, most by default shows you the first part of the line, and allows you to use the RIGHT and LEFT keys to shift across the line. This makes viewing some files much easier.
My muscle memory still types less , but I do agree that the most command is quite a handy alternative way to view text files.
Don't forget shift-f
Another really useful feature of "less" is the ability to mimic "tail -f", i.e. to follow the data as its appended to the file you're viewing. Shift-f does that.Also, since shift-g was mentioned in the article, shift-g also re-reads the file to see if more data had been appended, and adjusts to the new end of file if it has.
highlighting the words that match pattern
After regular use of "less", I have enjoyed the special features like -- switching to "tail -f" mode with a "F"
- ability to switch options within the command, ignore-case (-i), show line numbers (-N)
- ESC-/, for searching for a pattern across multiple files
But, the best thing about "less" is the ability to highlight the pattern that's searched! This feature works even as it's appending the file (with "F", tail -f mode)