How to use the dd command to copy the content of a folder

I'm trying to use the dd command to copy the content of the folder boot0 to the my disks intial bytes.

This is the command :

sudo dd if=boot0/ of=/dev/sdb ibs=440 obs=440 count=1 

But I get this error :

dd: error reading ‘boot0/’: Is a directory
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000209512 s, 0.0 kB/s

How can I solve this problem?

2

3 Answers

It can be done

We need to workaround two problems:

  1. dd doesn't know what to do with directories
  2. dd can only copy one file at a time

First let's define input and output directories:

SOURCE="/media/source-dir"
TARGET="/media/target-dir"

Now let's cd into the source directory so find will report relative directories we can easily manipulate:

cd "$SOURCE"

Duplicate the directory tree from $SOURCE to $TARGET

find . -type d -exec mkdir -p "$TARGET{}" \;

Duplicate files from $SOURCE to $TARGET omitting write cache (but utilising read cache!)

find . -type f -exec dd if={} of="$TARGET{}" bs=8M oflag=direct \;

Please note that this won't preserve file modification times, ownership and other attributes.

1

The main purpose of dd utility is to convert and copy files.

For example:

dd if=filename of=filename2 conv=ucase
dd if=/dev/urandom of=myrandom bs=100 count=1

If you'd like to copy the content of the folder use either rsync:

rsync -vuar src/ dst/

or cp utility:

cp -va src/. dst/
2

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).

Try this command :

 cp -r /home/source_folder/* /home/destination_folder
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like