I work with a lot of huge log files. I usually connect to my UNIX and Linux servers using Putty or Telnet, then I read the files using standard UNIX/Linux commands.  Other IT folks prefer to use an X-window GUI, but if you have a low-bandwidth connection, it is better to use a command line terminal to get things done faster.

If you need to browse logs without having to edit them, use the “less” command.  “Less” in Linux and UNIX lets you view the file and allows you to do basic find operations.  Unlike “more”, it allows for both forward and backward navigation so you can look for any search string at any point in the document.

Less Is Better Than More

To open a file using less,  type the less command followed by the file name in the UNIX or Linux prompt:

less filename

In the example below I opened a file called verbose.log.mo

image

Less will open the file and display the file name at the lower left portion of the terminal.  To find a string in the file, type forward slash followed by the string that you want to search and then hit Enter.  In the example below, I typed /error to search for the string “error”.

image

You will see that the view of the file will jump to the line where the string is located.  The located string matches are highlighted for easier viewing.

image

To search for the next instance of the string just press the “n” key.  Pressing the uppercase “N” will bring you to the previous match.

Here are other keyboard shortcuts I found from Wiki’s entry on “less”:

  • [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.
  • [Space bar]: Next page.
  • b: Previous page.
  • ng: Jump to line number n. Default is the start of the file.
  • nG: Jump to line number n. Default is the end of the file.
  • /pattern: Search for pattern. Regular expressions can be used.
  • n: Go to next match (after a successful search).
  • N: Go to previous match.
  • mletter: Mark the current position with letter.
  • letter: Return to position letter. [ = single quote]
  • ‘^ or g: Go to start of file.
  • ‘$ or G: Go to end of file.
  • s: Save current content (got from another program like grep) in a file.
  • =: File information.
  • F: continually read information from file and follow its end. Useful for logs watching. Use Ctrl+C to exit this mode.
  • option: Toggle command-line option –option.
  • h: Help.
  • q: Quit.

What I like most about “less” is that it automatically adjusts with the width and height of the terminal window.  The text when viewed with “less” is automatically wrapped if you resize the window of the terminal.  This makes less more advance than the older tool called “more”, which tends to cut the text as you tighten the width of the terminal.

If you have a lot of text or ASCII files to view in Linux or UNIX, make sure to master “less” as it is a superior command for viewing files using the command line terminal. Enjoy!