Linux copy file to usb

Linux copy and clone USB stick including partitions command

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.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

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:

/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

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/sdb of=

/*.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

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

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

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Copying files to a USB drive [closed]

Want to improve this question? Update the question so it’s on-topic for Ask Ubuntu.

Closed 4 years ago .

Currently, i am trying to copy a directory with files that contain my works website from a Gentoo server to a USB drive. Ive combed through many pages of you youtube and google and cannot seem to get it to copy. Im sure there is something im dong wrong here, anyone have any ideas?

Читайте также:  Как проверить свойства windows

cp -r /opt/biweb/app/ /data/dev/sdb1/

Edit: mount points

2 Answers 2

The command you want is:

cp -r /opt/biweb/app/* /dev/sdb1/

Don’t forget the asterix (*)! The above command will copy everything in the /app folder into the /sdb1 folder.

If you want to copy the app folder itself into the destination, then do:

cp -r /opt/biweb/app /dev/sdb1/

The above has no trailing slash after «app». This will copy the app folder as well as its contents.

The device description for the partition on the USB drive is

where x is the drive letter and n is the partition number, In your case it seems to be /dev/sdb1 . But you should not write directly to the device. Instead you should mount it and write to the file system at the mountpoint. First you should create a mountpoint, or use one that already exists. Text after # is a comment (not used as a command).

or in your case

You may want to make sure that you are allowed to write to the USB pendrive from a regular user by the following method,

Edit 1: Sometimes (I would even say often) the partition on the USB drive will be mounted automatically. You will find it with the following commands,

The automatic mounting may or may not make it read-write for the regular user, but it will usually be possible to write with superuser privileges, with sudo .

You can inspect how it is mounted with the command

but it will display a lot of information (about everything that is mounted).

Edit 2: copying command

After finding out that the pendrive is automatically mounted on /data , the following command line should work, if [the partition in] the USB drive is mounted read/write and with permissions for your regular user ID.

It should create a directory /data/app on the USB drive with the content (the directory tree and the files). If it does not work, you can try the special mounting method, that I showed above, but modified for the current mountpoint,

Edit 3: Please edit your original question, where you can use formatting tools.

Источник

Copy a file to a usb automatically on Linux bash

I’m looking for a way to copy a file to usb drive then inserted, automaticaly. I need to:

  • Detect when a new usb is inserted
  • Detect the mount point (/media/something by default) — my system mounts usb drives automaticaly.
  • copy a file onto it.

I need to do this with linux bash. I already searched for some useful commands as udev, mount, udevinfo but really don’t know how to used them (combine them) to get what I need.

2 Answers 2

I have not written any udev rules myself, but the answer from «etola» in this Ubuntu forum thread describes exactly your use case (execute a script when a device shows up). However it’s IMHO sensitive to each device that you plug in, so you may need more than one entry.

There’s also a manual about writing udev rules.

I kind of got it, it is functional, but the only things I have not fixed is:

  1. The usb-device can be accesed through 2 mount points.
  2. You need to umount the device manually
  3. I could not find a way to find out the /dev where the usb is mounted, so it is a constant.

the good thing is that you don’t need to copy any file onto the usb, just plug in and the file will be copied onto it automatically. Hope this helps someone else, or can be improved. (sorry the explanation below is in spanish. )

  1. Cree una regla de udev llamada 81-audo-copy.rules con el siguiente código:

Lo que hace es que cuando se inserta la memoria usb ejecuta el script de la ubicacion (el código 81 es importante, ya que dependiendo de lo que pongamos se ejecuta antes o despues de las demas reglas, o no se ejecuta. El resto del nombre puede ser lo que querramos).

  1. acá el contenido de «autocopiar», el archivo debe ser ejecutable

sudo mount /dev/sdb2 /mnt/acopiar -t vfat -o umask=000 cp -v /home/adario/paraCopiar/loqueQuieroCopiar.zip /mnt/acopiar

Lo que hace es asignar otro punto de montaje al usb que ya se ha montado, èsto para que sepamos el nombre de la ubicación.

  1. Por ultimo, para que el script no nos pida contraseña cada vez que se ejecute, agregamos al archivo sudoers
Читайте также:  Download windows essential security 64 bit

Источник

How to properly copy files from hard drive to USB flash drive in tty4 terminal?

How to properly copy files from hard drive to USB flash drive in tty4 terminal?(mount, copy, unmount, exit, etc)

P.S.Can no exit from tty4 terminal via Ctrl+Alt+F7 command, only with Alt+SysRq+B (kernel restart).

2 Answers 2

Plug in the device

lsblk to find the device name of your usb device. Naming is /dev/sdXY . Where X is any english letter and Y is integer, typically 1.

If the device was mounted, you will see the mountpoint, for example:

If not, mount it. Follow to the step #3

udisksctl mount -b /dev/sdXY , device name same as in previous step. ( /dev/sdb1 in my example) The mount folder will be reported back to you to use in the next step. For example, suppose lsblk tells me this:

sdc 8:32 1 7.5G 0 disk └─sdc1 8:33 1 7.5G 0 part

Then I will do the following:

You can see it automatically created /media/xieerqi/A669-34EF folder and mounted my pen drive there. Also , big advantage is that you do not need sudo .

Use rsync or cp or mv to get your files to the folder reported in step 3. Consult manual pages on usage of these commands. cp and mv are simplest. mv FILE DESTINATION — in my example (where FILE is the thing you want to move to the drive)

rsync is the best for backup however.

For example, to backup TESTDIR to my usb drive, I can do this:

  1. udisksctl unmount -b /dev/sdXY . Remove the device

NOTE: some drives mount to directories that have names with spaces. If you run rsync or mv with not quoted names like that, your data will not be copied to correct destination. Always quote pathnames that have spaces in them.

Источник

How do I copy a file larger than 4GB to a USB flash drive?

I am trying to copy a 6GB file to a USB flash drive but it won’t copy.

6 Answers 6

This is due to FAT32 limitation. Files larger than 4GB cannot be stored on a FAT32 volume. Formatting the flash drive as exFAT or NTFS will resolve this issue.

WARNING: Back up your data. Formatting will delete all the data on your device.

If you don’t want to reformat your USB drive or need it to be FAT32 you can simply split your big file into parts. Most archive managers come with the split option and for the command line there’s split , e.g. for your case:

See man split for the full documentation. This will create the following files:

The filesize of output.file.aa matches exactly the maximum file size of your FAT32 formatted USB drive, which is 4 Gibibyte (GiB, that’s not the same as Gigabyte GB) minus 1 Byte.
Thanks to Gilles for this important addition.

Before you can access the file again you need to merge its parts first. On Linux systems you can do so with:

If you’re afraid that this could sort the files incorrectly read In Bash, are wildcard expansions guaranteed to be in order?
The corresponding command on Windows systems is:

Along with many other useful GNU utilities split can be installed in Windows too, see GNU utilities for Win32.

Problem: FAT32 has a 4GiB limit for file size

Different scenarios and files systems are examined looking for alternatives taking into account

  • the file size problem
  • which operating systems that should read/write the USB drive

Linux only

If you intend to use the drive only with Ubuntu (and other linux distros), it is a good idea to use a linux file system, for example ext4. This way you might get higher read/write speed (depending on which process is the bottleneck), and you will get higher flexibility concerning ownership and permissions.

  • You can use the GUI program gparted to create the ext4 file system.

Full compatibility with Linux, Windows and MacOS

Windows has problems with linux file systems, and I think MacOS has problems both with linux file systems and NTFS. So if you want ‘full compatibility’ for reading and writing, only FAT32, UDF and exFAT remain.

FAT32 has a 4GiB (gibibyte, base 2) limit for file size.

Can be created in all three operating systems.

Maintain (repair) in Windows, if you have access to Windows.

Can be maintained in Ubuntu with dosfsck , that comes with the package dosfstools ,

Читайте также:  Energy efficient windows перевод

where x is the drive letter and n is the partition number, for example /dev/sdb1 for the first partition in drive b.

exFAT is another option. It is new compared to FAT32 and UDF and claimed to work well

natively with Windows

More about exFAT

Edit August 2021:

Since this answer was written, the support for exFAT is bundled in the new versions of standard Ubuntu, so you need no extra packages to run it.

end of edit

I have started to test exFAT in Ubuntu. I intend to edit this answer, when I have more experience of using it. Let us start with the following links,

ExFAT is a proprietary file system of Microsoft optimized for

and this command line to install [read/write] support for exFAT in Ubuntu

Can be created in Windows and MacOS and in Linux with the following command line

where x is the drive letter and i is the partition number, for example /dev/sdb1 .

  • Can be maintained in Windows.

My first test results:

  • Creating the exFAT file system worked well in Ubuntu (16.04 LTS (64-bit), installed system with the Xenial kernel series (4.4.0-93-generic), up to date).
  • Writing and reading a file greater than 4 GiB were successful. Ubuntu and exFAT co-operated correctly and fast (I checked with md5sum and the speed was limited by the USB system).
  • Ubuntu and Windows could read what was created in the other operating system. (I have no MacOS for testing, and have to rely on the reports from other people.)

Windows

So if you want full read/write access from Ubuntu and Windows, I would suggest that you use NTFS, which has journaling and is very debugged and polished as the [proprietary] file system for Windows. (It is also possible to use exFAT.)

In Ubuntu you can use the GUI program gparted to create an NTFS file system.

In Windows it is easy to create NTFS and exFAT file systems (they are native).

UDF probably lacks tools to repair the file system,

Maybe it is possible to find repair tools in Windows via this link: fsck tools for UDF, and there is some tool available as source code

Can be created in Ubuntu

Compatible with linux style links.

Compatible with linux style permissions. You can create and modify permissions of individual files (which is not possible with FAT and NTFS).

A UDF partition will not be prompted for formatting by Windows 10 (while the linux ext4 file system is affected, and can be destroyed by mistake).

So, to use it, assuming your USB stick is /dev/sdx :

Install the package udftools

Create a partition table and one partition with gparted or gnome-disks

Wipe the first mibibyte of the target partition with the risky dd (double-check the command line!)

Run mkudffs ,

Wipe the first mibibyte of the partition to erase the previous file system information (or other remaining data), to prevent you USB stick from being detected as a FAT after it has been formatted with UDF.

The -b 512 is to force a file system block size equal to the USB stick’s physical block size, as required by the UDF specification. Adapt it if you have the luck of having a USB stick with a more appropriate block size.

After that, your USB stick will be usable for reading and writing with GNU/Linux but also with current versions of Windows (read-only with the outdated version XP). Unfortunately this version of UDF created in Linux cannot be managed by MacOS (tested in several versions of MacOS in November 2020).

MacOS

Edit August 2021: HFS+ (journaled)

Since this answer was written, I discovered that Linux can read and write HFS+ (journaled), so use Disk Utility.app on your Mac to format the partition with HFS+ (journaled) in order to have a drive (for example an external drive) that can transfer or share data between Ubuntu and MacOS.

end of edit

In MacOS it is possible to use FAT32 and exFAT.

You can also use ext4 with a workaround, Ubuntu Server with an SSH server in a virtual machine. (I think the same workaround would work also with Windows.) This may be worthwhile when you intend to access a lot of files via the drive and its file system, but probably not with a small USB2 pendrive.

Источник

Оцените статью