...

Linux copy and clone USB stick including partitions command nixCraft Updated Tutorials/Posts

linux-copy-and-clone-usb-stick-including-partitions-command-nixcraft-updated-tutorials-posts

I need to copy and clone existing data from a USB stick. How do I clone a bootable USB key/pen drive on Linux? How do I clone a USB stick including partitions on Linux operating system?

You can easily clone USB flash drives on Linux. It is useful for backups and other purposes such as cloning USB for installation. You need to use the dd command. It will clone a bootable USB (or USB hard disk) for backup. This page shows how to copy and clone USB stick on Linux operating systems.

Linux copy and clone USB stick command

dd command used for copy a file, converting and formatting according to the operands. The procedure clone a USB stick including partitions is as follows on Linux:

  1. Insert USB disk/stick or pen drive
  2. Open the terminal application
  3. Find out your USB disk/stick name using the lsblk command
  4. Run dd command as: dd if=/dev/usb/disk/sdX of=/path/to/backup.img bs=4M

Let us see all commands in details.

Copy and clone a USB stick including partitions on Linux

Naturally, the first step is to find out your USB stick name on Linux. Selecting the wrong device name can result in data loss.

Find USB disk name on Linux

Simply run the dmesg command after inserting the USB stick or key:
$ dmesg
Filter out info using the grep command:
$ dmesg | grep -i usb
$ dmesg | grep -i 'attached'

Sample outputs:

[ 5.793647] sd 0:0:0:0: Attached scsi generic sg0 type 0 [ 5.795335] sd 0:0:0:0: [sda] Attached SCSI disk
[ 7.421446] sd 6:0:0:0: Attached scsi generic sg1 type 0 [ 7.438791] sd 6:0:0:0: [sdb] Attached SCSI removable disk
[ 334.349540] sd 6:0:0:0: Attached scsi generic sg1 type 0 [ 334.365272] sd 6:0:0:0: [sdb] Attached SCSI removable disk

/dev/sdb is my usb stick. Additionally, one can run the following lsblk command lists information about all available or the specified block devices:
$ lsblk
OR use the fdisk command to list the Linux disk partition tables for the specified devices and its size:
$ sudo fdisk /dev/sdb
Linux find USB disk or pen key name commands

Use dd command to copy and clone a usb stick on Linux

The syntax is as follows:
dd if=/dev/sdX of=/path/to/file.img bs=SIZE
To clone a usb stick named /dev/sdb to ~/usb-opensuse-current.img, run:
$ sudo dd if=/dev/sdv of=~/usb-opensuse-current.img bs=4M
You can show progress copy bar with status option for the dd command:
$ sudo dd if=/dev/sdv of=~/usb-opensuse-current.img bs=4M status=progress
Linux copy and clone USB stick including partitions
Verify new file with the ls command:
$ ls -i ~/*.img
You may want to change file permission too as the sudo command created the image with root:root. Use the chown command as follows:
$ sudo chown vivek:users ~/usb-opensuse-current.img
$ ls -l ~/usb-opensuse-current.img

Optional: Copy file usb-opensuse-current.img to NAS server or external media

Be sure that you copy ~/usb-opensuse-current.img somewhere safe. I usually like to keep on a FreeNAS or Linux NAS server using the scp command:
$ scp ~/usb-opensuse-current.img user@IP:/path/to/safe/
OR
$ scp ~/usb-opensuse-current.img vivek@server1.cyberciti.biz:~/backups/x230/usb/

Say hello to ddresuce

First, you need to install ddrescue using the dnf command/yum command/apt command/apt-get command as per your Linux distro:
sudo dnf install ddrescue ## fedora ##
sudo yum install ddrescue ## centos/rhel from EPEL repo ##
sudo apt install gddrescue ## debian/ubuntu and friends ##

Now you can clone the disk easily:
ddrescue /dev/INPUT /dev/OUTPUT
sudo ddrescue /dev/sdb ~/my-usb.img

See “Linux: Save and Recover Data From Crashed Disks With ddrescue Command Like a Pro” for more information.

Conclusion

You just learned how to clone a USB stick including partitions on Linux operating system using the dd command. For more info see man pages by typing the following man command:
man dd
man lsblk
man dmesg
man fdisk

See also

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Discover more from WIREDGORILLA

Subscribe now to keep reading and get access to the full archive.

Continue reading