I don't like using the UI for these kinds of functions because Apple hates to leave things in any decently operating form now.
I have a 320GB Hard Drive and I need to clone it (hopefully with dd in terminal) to a 256GB drive. There is only 90GB being used on the drive currently so how can I clone but leave out the blank space?
I'd like to plug both drives into a Mac, open terminal and complete the clone straight from one to the other. No middle operation.
Here's my guide to archiving disks without the empty space using bash in Terminal.app
Warning: as with anything pertaining to the terminal, read, read, then read again. One typo can cost you a reinstall and possible lost data. Don't do this if you don't have a reasonable grasp on what you're doing. Feel free to contribute to this or improve if you are one of those people that do know what you're doing 😁
I have dozens of Mac boot drives, Time Machine drives etc. that have to be backed up precisely as they are now, left in cold-storage, and must be restorable in the future just in case. After quite a bit of finicky keyboard pecking, I've streamlined my process as such:
Plug it in
Physically attach your drive in whichever manner, be it internal SATA, USB, Thunderbolt. In my experience, the reads never exceeded about 100MB/s so temper you expectations.
Health Checkup
Before proceeding from here, be certain to use Disk Utility to verify and/or repair the partition or entire disk you intend to archive! I do not archive my disks unless they prove to have non-corrupted libraries or journals. If you skip this step and there was damage to a journal, for instance, you may not ever be able to repair that journal on the archived image.
Identify devices
You must know precisely the ID of the drive and/or partition you're going to back up. You mess this up, you might be bunged.
Entering this will return a list of drives, partitions, sizes, and names of everything plugged in regardless if they're mounted to the OS or not. I get:
Since I'm looking to backup my 1TB Time Machine drive, I look for the drive that has the same name as my Time Machine drive (TimeCapsule) and reports to be near the same size (930GB approximately). From this, I can say that my Time Machine backup drive has the ID: /dev/disk0 so the TimeCapsule partition has the ID: /dev/disk0s2
Release the drive
The next step requires that the drive be not "busy" and to ensure that we will release it from use by detaching it in the system. Detach the whole drive, not just the partition you wish to clone.
Clone
You can clone the whole drive but I feel that the partition information, EFI, and any other partitions that may share the drive just complicate things. If, for instance, you had a Windows Bootcamp partition that you no longer plan to use, you can skip backing that up by directing the utility directly to the partition you want. That's what I'm doing here.
sudo means it will authenticate that you have permission to do this. dd is the "data definition" command that accomplishes essentially cloning of data. if is "input file" and of is and Old English word likely derived from the Greek root apo or Latin ab.
I directed the output data to my Desktop and on my Desktop it will create a new file called King Cold with the file extension .dmg which is a disk image. Since HFS+ natively uses 512-byte sectors, I set the block size (bs) to 512. This will help when compressing the free space later, so resist the urge to set this number higher for the sake of speed.
Notice the "\" in "King Cold". That is used in 'NIX to treat whatever comes afterward literally. Meaning the space between King and Cold is not a part of the command but just a character that is going to be part of the name of the file created.
Wait
Seriously, wait. It can take hours, a day, a week depending on how large your archive material is. It took me over an hour to archive a 100GB partition.
Plug it in
When Terminal finishes executing the command, it will display something along the lines of a final report which says, "320923 bytes transferred in 54.2902834 secs". So long as it does not report an "error" anywhere upon completion, you can move on.
You have a new disk image (.dmg) in the location you set in the last step. We have to mount it to the system to manipulate.
Pay attention to the next line that Terminal prints. It will tell you your new disk ID.
In my case, I get:
Clear the Junk
Drives may compress better if the trash is leveled. This is accomplished by writing 0's to unused (unassigned) space on the disk image.
We are cloning data from our /dev/zero folder to the mounted disk image. The /dev/zero folder just returns 0 no matter how much the system calls to it. We are putting those 0's into a file called "zjunk". You must add _file to the end of the file name so it will create a plain file with no filetype extensions. The file will grow until the drive has zero space remaining. Again, we use the 512 block-size to keep everything aligned on the drive for better efficiency.
Terminal will error out whenever there is no more space on the drive.
Once you delete this file, the empty space on the disk image should be zeroed out. Here is how you delete that file.
Release the disk image
Now that we're done preparing the image for shrinking, we must make sure it is not in use by any system resources.
Remember that /dev/disk3 is the ID given to us whenever we attached this disk image.
Go small
Finally we can try to resize the .dmg so that it has minimal free space, thus creating a smaller archive file.
In a flash, the Terminal will return to a new prompt and you will see your disk image has shrunken, hopefully. This depends on how much of your partition was actually being utilized. Setting -sectors to min means it will use the fewest sectors possible to store the data that is written to the disk image.
Using this technique, I've shrunken my OS install partitions that I keep handy for restores by about 12GB. All other backups of my hard drives have been handily shrunken to their minimum size, leaving only 100MB-1GB of free space on these image files.
In closing
Of course I'm no expert but this has proven to be a good way to always have duplicates of my retired disks on-hand. You shouldn't use this method if you plan to mount the image, edit or add data, and then unmount the image. It is merely for reaching back into the archive to recover data more easily. Since you have no free-space on these images, things can go badly if you choose to treat these as live-drives. You can, however, restore these disk images to larger drives and they can be used as live boot drives again.
Beware:
I'm not sure how hdiutil treats writing 0's to an image on an SSD. All of my archives are on clackers so write fatigue is far far less of a factor than on an SSD. Please do your own research and if you have info, please share.
Let me know how this works for you.
Edit: Added instructions to verify and repair disks first!
I have a 320GB Hard Drive and I need to clone it (hopefully with dd in terminal) to a 256GB drive. There is only 90GB being used on the drive currently so how can I clone but leave out the blank space?
I'd like to plug both drives into a Mac, open terminal and complete the clone straight from one to the other. No middle operation.
Here's my guide to archiving disks without the empty space using bash in Terminal.app
Warning: as with anything pertaining to the terminal, read, read, then read again. One typo can cost you a reinstall and possible lost data. Don't do this if you don't have a reasonable grasp on what you're doing. Feel free to contribute to this or improve if you are one of those people that do know what you're doing 😁
I have dozens of Mac boot drives, Time Machine drives etc. that have to be backed up precisely as they are now, left in cold-storage, and must be restorable in the future just in case. After quite a bit of finicky keyboard pecking, I've streamlined my process as such:
Plug it in
Physically attach your drive in whichever manner, be it internal SATA, USB, Thunderbolt. In my experience, the reads never exceeded about 100MB/s so temper you expectations.
Health Checkup
Before proceeding from here, be certain to use Disk Utility to verify and/or repair the partition or entire disk you intend to archive! I do not archive my disks unless they prove to have non-corrupted libraries or journals. If you skip this step and there was damage to a journal, for instance, you may not ever be able to repair that journal on the archived image.
Identify devices
You must know precisely the ID of the drive and/or partition you're going to back up. You mess this up, you might be bunged.
Bash:
diskutil list
Entering this will return a list of drives, partitions, sizes, and names of everything plugged in regardless if they're mounted to the OS or not. I get:
Bash:
/dev/disk0
0 GUID_partition_scheme 930.3 Gi disk0
1 EFI 200.0 Mi disk0s1
2 Apple_HFS TimeCapsule 931.2 Gi disk0s2
/dev/disk1
0 GUID_partition_scheme 68.1 Ti disk1
1 EFI 200.0 Mi disk1s1
2 Apple_HFS Legion of Doom 68.1 Ti disk1s2
Since I'm looking to backup my 1TB Time Machine drive, I look for the drive that has the same name as my Time Machine drive (TimeCapsule) and reports to be near the same size (930GB approximately). From this, I can say that my Time Machine backup drive has the ID: /dev/disk0 so the TimeCapsule partition has the ID: /dev/disk0s2
Release the drive
The next step requires that the drive be not "busy" and to ensure that we will release it from use by detaching it in the system. Detach the whole drive, not just the partition you wish to clone.
Bash:
hdiutil detach /dev/disk0
Clone
You can clone the whole drive but I feel that the partition information, EFI, and any other partitions that may share the drive just complicate things. If, for instance, you had a Windows Bootcamp partition that you no longer plan to use, you can skip backing that up by directing the utility directly to the partition you want. That's what I'm doing here.
Bash:
sudo dd if=/dev/disk0s2 of=/Users/CheetosLand/Desktop/King\ Cold.dmg bs=512
sudo means it will authenticate that you have permission to do this. dd is the "data definition" command that accomplishes essentially cloning of data. if is "input file" and of is and Old English word likely derived from the Greek root apo or Latin ab.
I directed the output data to my Desktop and on my Desktop it will create a new file called King Cold with the file extension .dmg which is a disk image. Since HFS+ natively uses 512-byte sectors, I set the block size (bs) to 512. This will help when compressing the free space later, so resist the urge to set this number higher for the sake of speed.
Notice the "\" in "King Cold". That is used in 'NIX to treat whatever comes afterward literally. Meaning the space between King and Cold is not a part of the command but just a character that is going to be part of the name of the file created.
Wait
Seriously, wait. It can take hours, a day, a week depending on how large your archive material is. It took me over an hour to archive a 100GB partition.
Plug it in
When Terminal finishes executing the command, it will display something along the lines of a final report which says, "320923 bytes transferred in 54.2902834 secs". So long as it does not report an "error" anywhere upon completion, you can move on.
You have a new disk image (.dmg) in the location you set in the last step. We have to mount it to the system to manipulate.
Bash:
hdiutil attach /Users/CheetosLand/Desktop/King\ Cold.dmg
Pay attention to the next line that Terminal prints. It will tell you your new disk ID.
In my case, I get:
Bash:
/dev/disk3 /Volumes/TimeCapsule
Clear the Junk
Drives may compress better if the trash is leveled. This is accomplished by writing 0's to unused (unassigned) space on the disk image.
Bash:
dd if=/dev/zero of=/Volumes/TimeCapsule/zjunk_file bs=512
We are cloning data from our /dev/zero folder to the mounted disk image. The /dev/zero folder just returns 0 no matter how much the system calls to it. We are putting those 0's into a file called "zjunk". You must add _file to the end of the file name so it will create a plain file with no filetype extensions. The file will grow until the drive has zero space remaining. Again, we use the 512 block-size to keep everything aligned on the drive for better efficiency.
Terminal will error out whenever there is no more space on the drive.
Once you delete this file, the empty space on the disk image should be zeroed out. Here is how you delete that file.
Bash:
rm /Volumes/TimeCapsule/zjunk_file
Release the disk image
Now that we're done preparing the image for shrinking, we must make sure it is not in use by any system resources.
Bash:
hdiutil detach /dev/disk3
Remember that /dev/disk3 is the ID given to us whenever we attached this disk image.
Go small
Finally we can try to resize the .dmg so that it has minimal free space, thus creating a smaller archive file.
Bash:
hdiutil resize -sectors min /Users/CheetosLand/Desktop/King\ Cold.dmg
In a flash, the Terminal will return to a new prompt and you will see your disk image has shrunken, hopefully. This depends on how much of your partition was actually being utilized. Setting -sectors to min means it will use the fewest sectors possible to store the data that is written to the disk image.
Using this technique, I've shrunken my OS install partitions that I keep handy for restores by about 12GB. All other backups of my hard drives have been handily shrunken to their minimum size, leaving only 100MB-1GB of free space on these image files.
In closing
Of course I'm no expert but this has proven to be a good way to always have duplicates of my retired disks on-hand. You shouldn't use this method if you plan to mount the image, edit or add data, and then unmount the image. It is merely for reaching back into the archive to recover data more easily. Since you have no free-space on these images, things can go badly if you choose to treat these as live-drives. You can, however, restore these disk images to larger drives and they can be used as live boot drives again.
Beware:
I'm not sure how hdiutil treats writing 0's to an image on an SSD. All of my archives are on clackers so write fatigue is far far less of a factor than on an SSD. Please do your own research and if you have info, please share.
Let me know how this works for you.
Edit: Added instructions to verify and repair disks first!
Last edited: