Linux pagers: less is more

When you are on the Linux command line and you want to view the contents of a file, you can use the less program. less is a newer pager program that replaces the older pager program, more. Hence the joke: less is more.

less is new and improved, so in almost all cases it will be preferred over more (maybe there will sometimes be compatibility issues with very old programs, or shell scripts, in which case more will be used).

Both  less and more are usually included with any Linux distribution (I have been using Ubuntu Server). The typical use case is piping output from another program, such as apt or ls, to less so that it can be easily viewed by the user. For example, $ ls -al /etc returns a few pages of output on a 800 x 600 pixel standard terminal. Unless the output is redirected to a file or piped to a pager, the user can only read the last page. To pipe to a pager, you can do the following:

$ ls -al /etc | less

This works, but it will strip the color-decorated output. In order to retain the color coding, you can do:

$ ls -al --color /etc | less -R

This also works:

$ ls -al --color=always /etc | less -R

To redirect output to a file, you can do the following:

$ ls -al /etc > ls.txt

This will write the output to a file ls.txt in the current working directory, but it will not be displayed on the terminal standard output. To write the output to both file and standard output, pipe the output of ls to the tee program:

$ ls -al /etc | tee ls.txt

This writes the output from ls to both the terminal standard output and also a file called ls.txt in the current working directory.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.