For normal file copying in *nix, I've only ever seen people use cp (eg. cp /mnt/mydevice/myfile ~/myfile
), but I eventually ran into dd
, which appears to do the exact same thing (dd if=/mnt/mydevice/myfile of=~/myfile
). I do see that they have some different parameters available (dd seems better at fine-tuned copying), but they appear redundant. Do these tools really do the same thing?
2 Answers
To answer your main question, no, they do not do the same thing.
dd works on the file you specify, making it able to copy data between devices, or from a device to a file. This is commonly used for moving data if devices specifically are involved (create an iso image from a cd-rom disc for example: dd if=/dev/cdrom of=mycdrom.iso), or backup raw devices (sometimes used in RAC databases: dd if=/dev/raw/raw1 of=device_raw1)
cp is used for duplicating file content to a new file or to a new location. things you specifically want there are preservation of ownership, timestamp and mode (rights), and being able to recurse the operation (=being able to copy directories).
-
4Ah! Okay, so dd copies the raw file, whereas cp copies the contents (which allows it to for instance copy directories without breaking the filesystem). Thanks! Jun 21, 2013 at 18:20
-
7@Kruug, I must be missing something because I still don't see what
cp
can do thatdd
cannot.dd
is also able to "duplicating file content to a new file or to a new location" and "preservation of ownership, timestamp and mode" right?– PacerierMay 2, 2015 at 12:10 -
4@Pacerier from what I can understand,
cp
is a more focused utility whereasdd
is more general.dd
can do all of whatcp
can do, butcp
can only do some of whatdd
can do.– KruugMay 5, 2015 at 17:01 -
I do not think that the difference is explained clearly enoug. What cp can not do and dd can? And the other way round?– gornApr 26, 2016 at 22:31
-
1@Pacerier:
dd
cannot copy multiple files, recurse in directories -cp *.txt dest/
andcp -R mydir/ dest/
are 2 simple examples of thingscp
can do thatdd
cannot. Sep 5, 2020 at 9:12
They do the same thing UNLESS you are specifying one of the options to dd
which limits which bytes are copied, such as seek
or skip
or count
or if you use the dd
options to mutate bytes such as conv
. If you aren't using one of these options to dd
and are just using the more commonly seen options like if
, of
, bs
then both utilities do the same thing: open both files, read from the input, write to the output until either the input is exhausted or the output cannot accept more bytes.
There is a lot of superstition about reading and writing "device" files stating that you must use dd
for these, but it is just that, superstition. dd
isn't doing anything different, we are just opening files and reading and writing bytes.
-
More or less this... if you use of=destiny depending on the device it is, some problems may occur, ex. USB flash disk. I discover that I have to use >> destiny and remove of=xxx parameter for it to work. If I use of=destiny strange problems occur because I oper with skip and iflags=skip_bytes flag... so, no so much superstition. Needs care and tests because of=destiny may not work correctly in some conditions where >>destiny goes smooth. I wrote a script to save large file to pendrive with steps. Full 400MB copy used to mess sdcard disk partition. Jan 3, 2017 at 16:12
dd
./dev/sda
) withcp
.