IT management/strategy

Unix tip: Making do with less

May 20, 2009, 11:26 AM — 

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

I like it!
Comments

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.
| reply

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.
| reply

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)
| reply
peer-to-peer

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

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

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.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace