I am working with a lot of huge log files. I usually connect to my UNIX and Linux servers using Putty or Telnet and then read the files using standard UNIX / Linux commands. Other IT pros prefer to use the X-window graphical interface, but if you have a low bandwidth connection it is better to use a command line terminal to get things faster.
If you need to view logs without editing them, use the less command. “Less” on Linux and UNIX allows you to view the file and perform basic searches. Unlike “more”, it allows both forward and backward navigation, so you can search for any search string anywhere in the document.
Less is better than more
To open a file with less, enter the command less followed by the filename on a UNIX or Linux command line:
less file name
In the example below, I opened the verbose.log.mo file
Less will open the file and display the file name at the bottom left of the terminal. To find a line in a file, enter a forward slash, then the line you want to search for and press Enter. In the example below, I typed / error to find the string “error”.
You will see the file view move to the line where the line is. Found matches are highlighted for easy viewing.
To search for the next occurrence of a string, simply press the n key. Clicking on the capital letter “N” will jump to the previous match.
Here are the other keyboard shortcuts I found from the Wiki article on “less”:
- [Arrows] / [Page Up] / [Page Down] / [Home] / [End]: Navigation.
- [Space]: next page.
- b: Previous page.
- n g: Jump to line number n . By default, this is the beginning of the file.
- n G: Move to line number n . By default, this is the end of the file.
- / pattern : Search for pattern . Regular expressions can be used.
- n: Move to next match (after successful search).
- N: Move to the previous match.
- m letter : Mark the current position with a letter .
- ‘ letter : return to position letter . [‘= single quote]
- ‘ ^ or g: Move to the beginning of the file.
- ‘$ or G: Move to the end of the file.
- s: Save current content (from another program, such as grep
- =: information about the file.
- F: Continuously read information from a file and watch its end. Useful for viewing logs. To exit this mode, use Ctrl + C.
- – option : Toggle command line option – option .
- h: Help.
- q: Quit.
What I like most about “less” is that it automatically adjusts to the width and height of the terminal window. Text when viewed with “smaller” is automatically wrapped if you resize the terminal window. This is less powerful than the older tool called “larger”, which tends to truncate text as the terminal width decreases.
If you have a lot of text or ASCII files to view on Linux or UNIX, be sure to master “less” as this is the best command for viewing files using the command line terminal. Enjoy!