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?


Solution 1:

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.

Solution 2:

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/