Copying files and directories is one of the most common tasks for any system administrator.
The cp command is a very simple command used to copy files and directories in Linux.
You can also use Rsync command to backup and copy files and directories. In order to copy files and directories, you must have write permission on the target directory.
In this tutorial, we’ll explain how to copy files and directories with the cp command.
Syntax of CP Command
The basic syntax for cp command is shown below:
cp [option] [source] [destination]
A brief explanation of each option is shown below (Note that these are Case Sensitive):
- -i : Used to prompt before copying.
- -v : Display verbose output during the copying process.
- -d : Used to copy a link file.
- -b : Create a backup copy of the destination file before overwriting.
- -n : Don’t overwrite an existing file.
- -R : Used to copy directory recursively.
- -p : Used to keeps the file creation date and file permissions.
Copy a Single File with cp Command
To copy a single file from your current working directory to a different location use the following syntax:
cp filename.txt /destination-directory-path
For example, copy a file named file1.txt from your working directory to /mnt directory, run the following command:
cp file1.txt /mnt/
The above command will overwrite the existing file if it exists in the /mnt. In this case, you can use -i to get a confirmation prompt before overwriting the files.
cp -i file1.txt /mnt/
You will be asked to prompt whether you want to overwrite the existing file or not.
cp: overwrite ‘/mnt/file1.txt’? y
To copy a single file from the specific location to other location, use the following syntax:
cp /source/filename.txt /destination/
For example, copy a single file named fstab from the /etc directory to the /opt directory with verbose output, run the following command:
cp -v /etc/fstab /opt/
Copy Multiple Files with cp Command
To copy multiple files named file1.txt, file2.txt and file3.txt from your current working directory to /opt/ directory, run the following command:
cp -v file1.txt file2.txt file3.txt /opt/
If you want to copy all files located inside /etc directory to /mnt directory, run the following command:
cp /etc/* /opt/
If you want to copy all files with .png extension located inside /etc directory to /mnt directory, run the following command:
cp -v /etc/*.png /mnt/
Copy Directory with cp Command
If you want to copy an entire directory with its sub-directory and files you can use -R option with the cp command.
For example, copy /etc directory with all sub-directory and files to the /opt, run the following command:
cp -vR /etc /opt/
The above command will create a destination directory and copy /etc directory with all sub-directries to the /opt directory.
If the /etc directory exists then it will copy /etc directory with all sub-directoiries and files inside /opt/etc/.
When you copy files and directory, the new files and directory will be owned by the user who runs the command.
You can use the cp command with -p option to preserve the file mode, permissions and timestamps.
cp -pR /etc /opt/
Copy Files and Directories with rsync command
The rsync is another command-line tool in Linux to copy files and directories between two locations.
Compared to the cp command, rsync also allows to copy files and directories to local and remote locations, as we’ve discussed in this RSync backup tutorial.
To copy a single file, run the rsync command with -a option as shown below:
rsync -va file1.txt /mnt/
The above command will copy file1.txt from your current working directory to /mnt directory. If the destination file exists, rsync will overwrite it.
You can also use rsync command to copy the directory and all of its content to the other location.
For example, to copy /etc directory will all sub-directories and files to the /opt directory, run the following command:
rsync -av /etc /opt/
The option -a is very powerful, it will copy the directory and all its sub-directories recursively, and preserve symbolic links, modification times, group, ownership, and permissions.
In the above guide, you learned how to copy files and directories with cp and rsync command. You can now try the cp command with other options and explore the more functionality.