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.

cp a b and cat a > b, what's the difference?

In x86 install script of linux kernel's source tree (arch/x86/boot/install.sh), both are used:

cat $2 > $4/vmlinuz
cp $3 $4/System.map

Why don't they just keep the same format if one is better than the other?

share|improve this question

migrated from stackoverflow.com Jan 29 '12 at 6:58

This question came from our site for professional and enthusiast programmers.

4 Answers 4

up vote 7 down vote accepted

One more issue comes to my mind where cat vs. cp makes a significant difference:

By definition, cat will expand sparse files, filling in the gaps with "real" zero bytes, while cp at least can be told to preserve the holes.

Sparse files are files where sequences of zero bytes have been replaced by metadata to preserve space. You can test by creating one with dd, and duplicate it with the tools of your choice.

  1. Create a sparse file (changing to /tmp beforehand to avoid trouble - see final note):

    15> cd /tmp
    16> dd if=/dev/null of=sparsetest bs=512b seek=5 
    0+0 records in 
    0+0 records out 
    0 bytes (0 B) copied, 5.9256e-05 s, 0.0 kB/s
    
  2. size it - it should not take any space.

    17> du -sh sparsetest
    0       sparsetest
    
  3. copy it with cp and check size

    18> cp sparsetest sparsecp
    19> du -sh sparsecp
    0       sparsecp
    
  4. now copy it with cat and check size

    20> cat sparsetest > sparsecat
    21> du -sh sparsecat
    1.3M    sparsecat
    
  5. try your preferred tools to check on their behaviour

  6. don't forget to clean up.

Final note of caution: Experiments like these have the inherent chance of rising your fame with your local sysadmin if you're doing them on a filesystem that's part of his backup plan, or critical for the well-being of the system. Depending on his choice of tool for backup, he might end up needing more tape media than he ever considered possible to back up that one 0-byte file which gets expanded to terabytes of zeroes.

Other files which cannot be copied with neither cat nor cp would include device-special files, etc. It depends on your implementation of copying tool if it is able to duplicate the device node, or if it would merrily copy its contents instead.

share|improve this answer
1  
So cp makes a file just like the original, while cat creates a new file with the same content. –  TheoYou Jan 30 '12 at 15:29
    
Both tools operate on content, but cp (at least "modern" implementations) is aware of some specialities nowadays, like holes (old implementations of cat will run into that trap). There also are filesystems which are unaware of the concept of sparse files, for example HFS+ (MacOS) or FAT (MSDOS, USB-Sticks, etc), causing them to be blown up to their full size. So there are constellations where cp or cat won't make a difference in practice. –  Tatjana Heuser Jan 30 '12 at 15:47

Both have equivalent functionality in those two cases, but cp is purely a file operation. "Take this file and make a copy of it over there".

cat, on the other hand, is intended to dump the contents of a file out to the console. "Take this file and display it on the screen" and then have a ninja attack the screen and redirect the output elsewhere.

cp would generally be more efficient, as there's no redirection going only, merely a direct copying of bytes from location A to location B.

cat would be read bytes -> output to console -> intercept output -> redirect to new file.

share|improve this answer
3  
cat won't really output to console -> intercept output -> redirect to new file, output file for cat can be stdout or a normal file, it'll just output to the file, as long as input is not the same as output. –  Theo Jan 29 '12 at 3:14
2  
cat has nothing to do with the console. Both cat and cp read from the input file and write to the output file. With cat, the output file is opened by the shell, whereas with cp, the output file is opened by cp; this makes no difference in performance. cp may be faster, but for a completely different reason: some implementations of cp try to guess the right chunk size for performance depending on the source and target devices; an implementation of cat wouldn't bother. –  Gilles Jan 29 '12 at 23:48

It's really a matter of preference, IMHO.

There is technically no real difference unless you use the cp command with the -p switch to preserve file ownership/group. Otherwise, it's the same thing functionally. Marc's answer is much more clearly verbose though and accurate.

share|improve this answer
3  
cp without -p does preserve some permissions. For example, if the source file is executable, cp will make the target file executable, but cat will not. –  Keith Thompson Jan 29 '12 at 2:35
    
Good point! So vmlinuz won't be executable whether $2 is. –  Theo Jan 29 '12 at 2:38

According to Keith's comment, cp preserves some permissions, and cat creates the new file as umask indicates. So $2's permission is not preserved that $4/vmlinuz is pretty clean, while if some strange permission is set on $3, $4/System.map will keep that.

share|improve this answer
    
is that the reason to attribute cat 's fasterness? –  Nikhil Mulley Jan 29 '12 at 16:13
    
Is cat faster? –  TheoYou Jan 30 '12 at 13:11

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.