Tuesday, May 19, 2009

Doing more with less

If you are like me, a Windows convert, you are probably popping open nano, emacs or some other command line editor a lot as you are looking through configuration files.  Yeah I can use vi when I have to but I’m happier and more productive in a full screen text editor.  Anyhoo, I’ve used the “more” command in Linux and Windows for quite a long time and I like it for quick checks, but now that I’ve been playing with Linux a lot I’ve been seen how much more powerful the “less” command is.

To see the contents of a file just type “less filename.txt”.  It automatically displays the file on the screen and magically the page up, page down, home and end keys all work for navigation.  Even better, you can run a simple search by proceeding the search term with a forward slash.  For example, to search a file for the word “apple” while in a less session just type “/apple”. Cool.  You even get the benefit of text highlighting to make the search more effective. (If you hate the highlighting just hit Esc-u to turn it off.) Once you  are in a search, the letter n takes you to the next found word and a capital N takes you to the previous found word.  Pretty cool.  To exit a less session just type q for quit.  Of course the beauty of all this is that you aren’t actually editing the file so you can do no harm.

To see what line you are on or how far you are reading into a file start the program with the –M switch (less –M filename.txt)  Here are some other cool tricks (from a shameless cut and paste):

Quit at end-of-file
To make less automatically quit as soon as it reaches the end of the file (so you don't have to hit "q"), set the -E option.
Verbose prompt
To see a more verbose prompt, set the -m or -M option. You can also design your own prompt; see the man page for details.
Clear the whole screen
To make less clear and repaint the screen rather than scrolling when you move to a new page of text, set the -C option.
Case-less searches
To treat upper-case and lower-case letters the same in searches, set the -I option.
Start at a specific place in the file
To start at a specific line number, say line 150, use "less +150 filename". To start where a specific pattern first appears, use "less +/pattern filename". To start at the end of the file, use "less +G filename".
Scan all instances of a pattern in a set of files
To search multiple files, use "/*pattern" instead of just "/pattern". To do this from the command line, use "less '+/*pattern' ...". Note that you may need to quote the "+/*pattern" argument to prevent your shell from interpreting the "*".
Watch a growing file
Use the F command to go to the end of the file and keep displaying more text as the file grows. You can do this from the command line by using "less +F ...".
Change keys
The lesskey program lets you change the meaning of any key or sequence of keys. See the lesskey man page for details.
Save your favorite options
If you want certain options to be in effect whenever you run less, without needing to type them in every time, just set your "LESS" environment variable to the options you want. (If you don't know how to set an environment variable, consult the documentation for your system or your shell.) For example, if your LESS environment variable is set to "-IE", every time you run less it will do case-less searches and quit at end-of-file.

Happy viewing!!!

No comments: