Jun 282011
 

bashless it’s a fundamental command if you work on the Linux terminal.

Doing a “man less” you can see:

NAME
less - opposite of more

I love Gnu/Linux man pages, in this case it refer to more another command that do similar things (but less).

The Linux command less is similar to cat, but with less you can scroll the file instead of showing the file at once. With less command you can scroll up in the file as well as down, where with the Linux command more you can only scroll down the file.



Basic Usage

The basic usage of less is:

less filename

This command simply open the file (readonly) and show it on screen, you can scroll up and down with arrow keys (and other shortcut) and do search with vi syntax; less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi.

Search Options

Like vi editor you can use:

/pattern
?pattern

Respectively search forward (with /) and backward (with ?) in the file for the N-th line containing the pattern. N defaults to 1. The pattern is a regular expression as recognized by the regular expression library supplied by your system, after the first result you can use:

n Repeat previous search, for N-th line containing the last pattern.

N Repeat previous search, but in the reverse direction

Work with multiple files

less can have as arguments multiple files, something such as:

less /var/log/syslog /var/log/kern.log /var/log/dmesg

The terminal will show the first file, but you can move between them with the commands:

:n     Examine the next file.  If a number N is specified, the N-th next file is examined.

:p     Examine the previous file in the command line list.  If a number N is specified, the N-th previous file is examined.

Or you can remove a file from the list with

:d     Remove the current file from the list of files.

Follow mode

Make less behave like tail -f.

Using +F will put less in follow mode. This works similar to ‘tail -f’. To stop scrolling, use the interrupt. Then you’ll get the normal benefits of less (scroll, etc.).

Pressing SHIFT-F will resume the ‘tailling’.

Marked Scrolling

While you are scrolling your file you find something interesting, but you need to continue check the files, how you will remember that point ?

mark it !

While you are in less use m"anyletter" (m followed by a letter) to mark the current position with that letter.

To return to a marked position use '"anyletter" (singlequote, followed by a letter) this return to the position which was previously marked with that letter.

Other options

If less is launched with the option -N it will print line numbers before each line.

To exit from less you have many options:

q or Q or :q or :Q or ZZ

Navigation options while in less :

G – go to the end of file

g – go to the start of file

Enter edit mode while in less

v  Invokes an editor to edit the current file being viewed.  The editor is  taken  from  the  environment  variable  VISUAL  if
   defined, or EDITOR if VISUAL is not defined, or defaults to "vi" if neither VISUAL nor EDITOR is defined.

Conclusions

less it’s a great companion in your terminal journey, it’s really useful in pipe, to open big files or to open files without locking them. The options presented here will help you in use in a more effective way, i suggest you to make practice with the mark and search options, that used together can be a really powerful tool.

Popular Posts:

Flattr this!

  One Response to “Do more with less”

  1. […] insert line numbers if you want. If you want a full run-down on what you can do with less go here: http://linuxaria.com/howto/bash-linux-less?lang=en. It pretty much outlines all the really useful options for […]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*