14

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
  • 4
    dd reads files, not folders. As it is, that's not the proper way to write files to the start of a drive. You need to create a FAT32 partition first and then use cp to copy the contents over. May 29, 2014 at 12:15
  • I think you want to use rsync, man rsync.
    – ppetraki
    Nov 14, 2014 at 14:21

4 Answers 4

11

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.

3
  • I get the dot in the of= dd parameter. How to get rid of it? I want to copy files from just one directory to another
    – Marecky
    May 23, 2021 at 14:58
  • dd: {}: No such file or directory Jul 17, 2023 at 1:35
  • dd is used to copy even an entire disk. For dd all are BYTES and bits, that's why it copies even the MBR at offset 0-512 of the disk. The problem is that people want to guess WHERE their files are in a disk. For instance, Thunar said all files in my usb stick = 12MB but... when I scanned the 2GB image with python I discover that some files were at offset 100MB in the disk, so a dd stopped at 15MB would be corrupted. Oct 18, 2023 at 1:28
8

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
  • 1
    -R / -r not required when using -a as -a always implies recursive. May 28, 2015 at 2:23
  • 2
    It is not an answer to the question, it its a recommendation. Sep 10, 2018 at 14:59
0

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
  • 2
    cp -a is preferable to cp -r because it preserves permissions, timestamps, symbolic links etc. cp -r would reset all files to be owned by you with a creation/modification date as now, and symbolic links would be followed and their destinations duplicated. May 28, 2015 at 2:26
0

Here's a solution that is based on @unfa's answer and also addresses @Marecky's comment:

To get rid of the local path indicator (dot), you can use find as mentioned in this Unix & Linux answer:

SOURCE="/media/source-dir"
TARGET="/media/target-dir"
cd "$SOURCE"
find * -type d -exec mkdir -p "$TARGET/{}" \;
find * -type f -exec dd if={} of="$TARGET/{}" bs=8M oflag=direct \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .