Unix tip: Making do with less

By Sandra Henry-Stocker  4 comments

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.

4 comments

    Anonymous 2 years ago
    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 filesBut, 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)
    Anonymous 2 years ago in reply to Anonymous
    Thanks for useful tips (specially with "F")!
    Anonymous 2 years ago
    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.
    Anonymous 2 years ago
    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.

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      IT Management/StrategyWhite Papers & Webcasts

      White Paper

      Evaluator Group: Storage Federation - IT Without Limits (Analysis of HP Peer Motion with Storage Federation)

      As the role of IT increases within organizations, the need to move data when and where it is needed is critical to support emerging business requirements. This has become increasingly difficult due to the huge growth of data volumes. This white paper sponsored by HP + Intel evaluates a solution that aims to enable the movement of data without physical limitations. Read now and see how this could enable agility and efficiency.

      White Paper

      ESG Lab Validation Report: HP Data Protector & Deduplication Solutions

      Many organizations have deployed disk-to-disk backup technologies to improve the speed and reliability of their backup and disaster recovery operations. A growing number of these now look to data deduplication to enhance retention periods and reduce costs. This ESG Lab Validation Report sponsored by HP + Intel examines a number of backup and recovery solutions and evaluates their ease of implementation as well as their ability to improve reliability and reduce costs.

      White Paper

      Business Value of Blade

      The nature of the blade platform makes system management, monitoring and provisioning easy and efficient. Access this resource to learn how blade migration will save your data center time and money while increasing performance.

      White Paper

      Accelerate time to application value

      For your IT organization to keep pace with the business, you need a new, faster approach to infrastructure deployment-an approach that increases agility and accelerates time to application value. That's HP Converged Systems. Built on Converged Infrastructure, these systems deliver the industry's first portfolio of pre-integrated, tested, and optimized infrastructure solutions for applications running in virtual, cloud, dedicated, or hybrid environments.

      White Paper

      Converged Infrastructure for Dummies

      As you know, everything is mobile, connected, interactive, and immediate. This is exactly why organizations need a highly agile IT infrastructure in order to keep pace with extreme fluctuations in business demand. This book will help you understand why infrastructure convergence has been widely accepted as the optimal approach for simplifying and accelerating your IT to deliver services at the speed of business while also shifting significantly more IT resources from operations to innovation.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question