In this guide, we will learn about copying files and directories in Linux.
Prerequisites:
To perform the steps that are demonstrated in this guide, you need the following components:
- A properly configured Linux system. For learning and testing, an Ubuntu VM will suffice.
- Read access to the files/directories to be copied. Learn more about file permissions in Linux.
Copying Files and Directories
Copying is one of the most fundamental operations that you have to perform when operating a computer. In Linux, there are multiple tools that are available for copying files and directories:
- cp: The default tool for copying files and directories. It’s available on all Linux distros.
- rsync: A powerful tool that is primarily used for file synchronization. However, we can also use it for copying files and directories.
For demonstration purposes, we configured two directories: /tmp/source and /tmp/destination.
Copying Files and Directories Using Cp
The command structure of cp is as follows:
For all the available options, check out the man page:
Copying a File
The following command copies the “1.txt” from /tmp/source to /tmp/destination:
If you specify a different file name as the destination, cp renames it accordingly:
Copying Multiple Files
The following command copies all the text files under /tmp/source to /tmp/destination:
If the source files are named in a pattern, cp can work with that:
Copying a Directory
In the next example, we will copy the “subdir1” to /tmp/destination:
Here, the “-r” flag tells the cp command to copy the directory and all its content recursively to the destination.
If a different directory name is specified in the destination, cp renames it accordingly:
If the destination directory already exists, cp copies the source directory and everything it contains. However, we can specify to only copy the files and sub-directories, not the target directory:
Copying Files and Directories Using Rsync
The primary usage of rsync is synchronizing the files between the local/remote servers. It comes with numerous additional features. However, we can also use it for “syncing” files from one directory to another (copying in other words).
The rsync command structure is as follows:
Check out the man page for all the available options:
Copying Files
The following command copies the “1.txt” from /tmp/source to /tmp/destination:
Here, the “-a” parameter tells rsync to operate in archive mode.
We can also copy the file with a different name in the destination:
To copy multiple files, specify the files one by one or describe the pattern to match:
Copying Directories
Unlike cp, to copy the directories with its files and sub-directories, there’s no change of syntax with rsync. When working in archive mode, rsync automatically copies all the contents recursively.
One key difference here is the trailing slash (/).
- If a slash is present in the source directory, rsync copies the contents of the source directory to the destination.
- If a slash isn’t present in the source directory, rsync copies the source directory inside the destination directory.
The following commands demonstrate the difference perfectly:
Copying Files to Remote Machine Using Scp
The scp command is a specialized tool that copies the files and directories over SSH. It requires having the OpenSSH server installed on the remote host.
The fundamentals of the scp command are the same as the cp command. Learn more about the scp command.
Copying Files
In the following command, we copy the Ubuntu 22.04 ISO to the /tmp directory on a remote host:
To copy a file from a remote host, use the following command:
Copying Directories
To copy a directory to a remote host, we use the following scp command:
Here, the “–r” parameter is to operate in recursive mode (needed to copy the directory).
Conclusion
We showcased the various ways of copying the files in Linux. We demonstrated how to use the cp and the rsync commands to copy the files and directories locally. We also showcased how to use the scp command to copy the files and directories to a remote host.
For more advanced copying and backup configurations, rsync is a better option. It can make a system backup, sync with multiple remote hosts, update new files, and more.