cat a > b
cp a b
If they are functionally the same for all intents and purposes, which one is faster?
In terms of functionality I think they are the same.
If I had to venture a guess about which is faster, I would say the cp command because its purpose is to do file operations just for copy purposes, so it would be optimized for that.
cat, by contrast, is meant concatenate files, meaning joining several files into a series. If no file is specified, it will display a file to the console (thanks to @bahamat for reminding us). In this example the output is redirected to another file. I think this indirection would be less efficient than a direct
I don't know if the difference would be noticeable for regular sized files, though it would be interesting to time these on very large files. I guess one could do repeated trials with /usr/bin/time and see if one is consistently faster/slower than the other.
Do you have any particular reason to ask about this, or is it just simple curiosity (nothing wrong with that at all of course)
Functionally similar, but specifically different. Essentially, they both read a bunch of data from the first file, write it to another file.
When I do an strace on Linux:
There isn't much difference: both copy the content of the old file into a new file with the same content. Both overwrite the target if it is an existing file.
Some old systems might stop copying or truncate lines if you try to copy binary files with
If the destination doesn't exist,
You can protect against overwriting the target file when doing
It is often useful to preserve the date of the original file. You can use
Performance will vary, depending on the size of the file, the filesystem, the kind of source and target disk, the operating system, etc. For raw disk copies under Linux, I found next to no difference.
Look like cat is