Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I used to use cat to view files. Then I learned that less is usually better, and is a must if the file is longer than a few dozen rows.

My question: Is there ever a reason to use cat instead of less? Is there any situation where cat is a better solution?

share|improve this question

migrated from serverfault.com Sep 27 '11 at 20:37

This question came from our site for professional system and network administrators.

5  
I use most ;) –  new123456 Sep 28 '11 at 1:28
13  
less and cat solve different problems and they are each better than the other in their own problem domain –  David Heffernan Sep 28 '11 at 16:24
2  
I only use cat to pipe it's output into less afterwards. –  FUZxxl Sep 29 '11 at 19:02
2  
@FUZxxl: Unless you're using cat on multiple files, that's a Useless Use of Cat –  rahmu Sep 30 '11 at 6:44
    
I often use cat to print very short files to the console, the files in /proc and /sys are very good candidates. This way I can see the content of multiple files simultaneously. –  Feuermurmel 2 days ago

15 Answers 15

up vote 10 down vote accepted

I personally prefer view for static content or tail -f for dynamic content.

This does not answer your question, though. There is a saying "why use more if you have less" ;-)

But there are cases where I prefer cat to less: I usually work with X11-windows. These windows have a scroll-buffer which can be set to some hundred lines.

Doing a cat for - let's say 200 lines and then using the mouse with the scroll-bar is more comfortable to me than using less in these cases.

share|improve this answer
1  
By view you mean the usual vim -R, or you have something else with that name? –  manatwork Sep 29 '11 at 6:55
    
No - just vi (or vim) in read-only-mode. CentOS sets that alias by default. This will not trigger HIDS when viewing a file in a secured directory. –  Nils Sep 29 '11 at 20:32

Although both commands allow you to view the content of a file, their original purposes are quite different.

less extends the capabilities of more. The latter was created to view the content of a file one screenful at a time. less adds features such as backward movements and better memory management (no need to read the entire file before being able to see the first lines).

cat concatenates files and prints the result on the standard output. If you provide only one file, you will see the content of that file. It becomes 'powerful' when you provide multiple files. A good example is the combination of split and cat. The first command will divide a large file in small portions. The second one will then concatenate the small portions into a single file.

Back to your question, cat would be preferred in an autonomous script requiring files to be read entirely (or concatenated) without interaction. In terms of file viewing, I think it's more a question of taste.

share|improve this answer
1  
less always allows to search within the file, which cat does not. –  Bernhard Aug 11 '13 at 6:30

I usually use cat when I need to type a command based on something in the file. cat is more convenient since you can see the file (if it's small) while you have access to the shell prompt. It also allows for pipe lining.

share|improve this answer
2  
+1 for pipe the content of a file to another program. –  Johan Sep 28 '11 at 7:28
3  
For one file, redirecting the standard input is also possible and often preferable. –  AProgrammer Sep 28 '11 at 9:41
Is there any situation where cat is a better solution?

When you are dealing with more that one file and wish to concatenate them.

From the man page:

cat f - g
        Output f's contents, then standard input, then g's contents.
share|improve this answer

There are people who argue violently that the only purpose of cat is to concatenate files. For every other use, there is more (or less).

But this fails to take into account one quite important fact: cat is one character less to type. I use those commands so often that one character less is a no-brainer.

Another reason is when you want to create a file. I often copy-and-paste text from the web into a file like this (command prompt shown):

$ cat > filename
<Cmd-V>
<Ctrl-D>
$

That is, I open a file filename for write access, paste the content and close the stream by pressing Ctrl-D. Neither less nor more can do that, and it’s faster than opening an editor.

share|improve this answer
4  
If you use zsh as a shell, you can set it up so that $<filename (where $ is the prompt) invokes $PAGER with stdin connected to filename. That is even fewer characters to type than cat. –  Kevin Cathcart Sep 28 '11 at 17:33
    
@Kevin Nice to know but I’m a bash user. –  Konrad Rudolph Sep 28 '11 at 17:34
2  
On Mac OS X, I use pbpaste > filename, and I believe X has something similar. I do still use cat for quick file creation, just not for pasting. –  cobbal Sep 29 '11 at 17:34
    
@cobbal Hehe, I never thought of that even though I use pbcopy / pbpaste for other purposes. –  Konrad Rudolph Sep 29 '11 at 18:15
1  
It's actually two characters less - you have to press q to exit less :D –  Martin von Wittich Sep 15 '13 at 18:46

Sometimes you don't want the pagination that less does and just want the complete file so you'd use cat.

share|improve this answer

cat can be used to concat several binary files into one big one:

cat data.001 data.002 data.003 > bigdata.dat
share|improve this answer

I use cat to grep (multiple) files.

cat <filename1> [<filename2> <filename3>] | grep -i "string of interest"

I know grep supports filenames, but If you are grepping the same file for different search terms, editing a command where the search term is the last thing on the line is easier then having to ctrl+arrow-key back through the line.

share|improve this answer

I guess, because of the poularity of Unix variants now, many people don't have to administer their systems (with a vengence). When things go tits-up you might just be able to reboot and enter your system in what is called a restricted environment.

You get a command line and access to a small set of what is deemed useful commands that are small and usually statically linked. You might get vi as an editor or even the smaller ed, but not emacs or vim. You would get cat, but not less. The idea is to give you enough tools to repair your system without taking too much resources as those resources may be incorrectly configured, or all used up. the less command onder those circumstances is superfluous.

share|improve this answer

And less is not pre-installed on all GNU/Linux distributions. (eg. Gentoo) cat is everywhere, probably.

share|improve this answer

Piping cat through grep is useful:

cat <filename> | grep -i "string of interest"
share|improve this answer
4  
Not to mention that grep takes a filename as an argument, or you could use redirection. The pipe is expensive. –  Chris Down Sep 27 '11 at 21:06
19  
Congratulations, you win a 'useless use of cat' award! –  ThatGraemeGuy Sep 27 '11 at 21:14
3  
@TimKennedy - "when you've got less" is in the question title... –  Chris Down Sep 27 '11 at 21:35
5  
Upvoted. I do this regularly. If I want to grep a file multiple time for different search terms, this is easier. grep wants grep [search term] [filename] Therefore, it's more of a pain to edit the search term if I am using the terminal history. By piping to grep, the search term is the last thing on the line, and easier to edit. –  Fake Name Sep 28 '11 at 3:39
9  
You can still have the ordering you want without using cat: < [filename] grep [search term] and < filename tail -n 1000 | grep pattern. Redirection can go pretty much anywhere on a command line. –  camh Sep 28 '11 at 7:18

I use less -FX, which makes less behave like cat when a file can be displayed on one screen. From the less(1) manpage:

-F or --quit-if-one-screen
       Causes less to automatically exit if the entire file can be dis-
       played on the first screen.

-X or --no-init
       Disables sending the termcap initialization and deinitialization
       strings  to  the  terminal.   This is sometimes desirable if the
       deinitialization string does something unnecessary, like  clear-
       ing the screen.
share|improve this answer

There can be an issue with privilege escalation, as within 'less' you can press 'v' to edit a file or '!' to send a shell command.

You might want to allow some users to view a file that can only be read by the superuser, but not allow those users to edit the file or to use superuser privileges in general. You could do this by editing '/etc/sudoers' to allow them to use 'sudo /bin/cat /etc/importantfile'. You wouldn't want to allow 'sudo /usr/bin/less /etc/importantfile', because they could use 'v' to edit the file, or use '!' to launch a shell with full superuser privileges.

Of course, the users could use 'sudo /bin/cat /etc/importantfile | less' and still use 'less', without the security risks.

share|improve this answer

For convenience. cat have 3 characters while more/less have 4 characters. And typing cat only require your left hand only.

share|improve this answer
    
Why is only using your left hand an advantage? I don't use the mouse on the terminal... –  Bernhard Aug 17 '13 at 9:09

The cat stays on the screen. Everything less disapears.

Clarified by/for Volker Siegel:

The output of cat stays on the screen. Everything shown by less disapears after closing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.