Transferring files between disk partitions is a common task in Linux administration. Whether you’re reorganizing your storage or need to move data for backup purposes, knowing how to efficiently transfer files is essential. In this guide, we’ll explore various methods to transfer files between Linux disk partitions with command-line examples.
Method 1: Using cp
Command
The cp
command is one of the most straightforward ways to copy files in Linux.
cp /path/to/source/file /path/to/destination/
For example, to copy a file named example.txt
from /home/user/documents/
to /mnt/data/
, you would use:
cp /home/user/documents/example.txt /mnt/data/
Method 2: Using mv
Command
The mv
command is used to move files and directories. It’s also commonly used to transfer files between partitions by essentially moving them from one location to another.
mv /path/to/source/file /path/to/destination/
For instance, to move a file named data.csv
from /mnt/old_partition/
to /mnt/new_partition/
, you would use:
mv /mnt/old_partition/data.csv /mnt/new_partition/
Method 3: Using rsync
Command
rsync
is a powerful utility for efficiently transferring and synchronizing files between directories, even across different partitions.
On Ubuntu and Debian-based systems, you can install rsync
using the apt
package manager.
sudo apt update
sudo apt install rsync
For CentOS and Red Hat Enterprise Linux (RHEL), rsync
can be installed using the yum
package manager.
sudo yum install rsync
Now, here is the rsync command syntax for moving files between different directories or partitions:
rsync -av /path/to/source/directory/ /path/to/destination/directory/
For example, to synchronize the contents of /home/user/documents/
to /mnt/backup/
, you would use:
rsync -av /home/user/documents/ /mnt/backup/
Method 4: Using tar
Command
The tar
command can create archives of files and directories, which can then be moved between partitions and extracted at the destination.
tar -cvf archive.tar /path/to/source/directory/
To create an archive of /mnt/photos/
and move it to /mnt/backup/
, you would use:
tar -cvf /mnt/backup/photos_backup.tar /mnt/photos/
Conclusion
Transferring files between Linux disk partitions can be achieved using various command-line utilities like cp
, mv
, rsync
, and tar
. Each method offers its advantages, so choose the one that best fits your requirements for speed, efficiency, and ease of use. With these commands in your arsenal, managing your files across partitions becomes a straightforward task.