Shared VPS Dedicated WP Professional WP Professional Plus
WP Professional Plus
cat are all file viewers but each has their own particular uses.
cat simply prints the file to the standard output (your screen usually) in one big rush. If it is a simple file with say 10 or 20 lines this is the command to use.
more allows you to do the same as
cat except it pauses the scrolling every time a screen full has been displayed allowing you to use the space bar to get the next page.
less does what
more does but allows you to scroll backwards as well as paging forward.
cat is your first choice for viewing a file because it is fast and simple. It is also your best choice if you are piping the output to another command. There are options for this command such as
-s. To number all lines use
-n, to number all lines except blank ones use
-b and to skip blank lines use
-s. You can combine these and for instance do
more command is a bit more useful if you are wanting to page output to look for specific items. However in most cases
less is probably more useful. In general you may use
more for paging output from other commands especially ones that may contain encoding. If you are wanting to read a static text file then
less is most suitable. To stop the output use CTRL+C
less is your goto command for file viewing. The basic usage is
less filename. You then use the f key to go down a page and the b key to go up a page. This is its main advantage over
more, the ability to go back and forward.
less also uses a lot of the same commands as
vim. So for instance to go to line 100 you would type in
100G where G means go. To quit
less use the q key.
The other most useful option of
less is the R key. This refreshes the page, this is useful in cases where the file may be changing.
less can also be used instead of
tail -f logname.log. To do this use
less +F logname.log, you can then CTRL + C to go to editing mode to scroll back and forward.
That is really the basics of using
less. It has lots of features though and
man less is worth a read in case there are any features that you would find useful in your particular use cases.
Which should I use and when?
The following are some general recommendations about which utility is useful when.
If you are piping the output to another command then use
cat in pretty much every case.
If you are not dealing with a file or the output contains ANSI codes then it is generally better to use
more. An example of this is if you want to list a directory's contents with color and then page it.
ls | more and
ls to less don’t work. You need to use
ls with the
--color=yes option because when you pipe the output it strips it of the ANSI codes. If you try
ls --color=yes | more and
ls --color=yes | less, you will see that the more output is exactly what you needed whereas the
less output has lots of strange characters and no color. You can fix this by using ls –color=yes | less -R however it is much simpler to simply type in
| more than
| less -R
If you are simply wanting to read a static file that won’t be changing then
less is always your best choice.