Duplicating and copy documents and registries is quite possibly the most widely recognized and commonly used task you will perform when working with the Linux command line. In Linux, there are a few orders to duplicate documents, the cp and rsync orders are the most well known.
Most Linux user frequently utilize and use the cp command when copying files and the rsync command when copying directories.
To be able to copy files and directories, you must have at least read permissions on the source files and write permissions on the destination directories.
Copying Files with the cp Command
The cp command is a command line utility for copying files and directories on a Linux system from one location to another on the same machine.
The simplest use case is to copy files in the current working directory . For example, to copy a file named
file.txtin the current directory as
file_backup.txt,run the following command:
cp file.txt file_backup.txt
If the destination file exists, then files with the same name and extension will be overwritten. To request confirmation, use the options
cp -i file.txt file_backup.txt
When using the cp command to copy files, the new files will be owned by the user performing the command. Use option
-pto preserve file mode, ownership and timestamps :
cp -p file.txt file_backup.txt
Another option that could be useful is
-v. When using this option the command will print what is being done:
cp -v file.txt file_backup.txt
'file.txt' -> 'file_backup.txt'
Copy Files to Another Directory
To copy a file to another directory, you need to specify an absolute or relative path to the destination directory. In the following example, we copy the file
file.txtto the directory
cp file.txt /backup
When specifying only the directory name as the destination, the copied file will have the same name as the original file.
If you want to copy a file with a different name, you need to specify the desired file name:
cp file.txt /backup/file_baru.txt
The above command will copy the file to the directory specified as
Copy multiple files
It is also possible to copy multiple files at once by specifying the file names followed by the destination directory:
cp file.txt file1.txt file2.txt directory
When copying multiple files, the destination must be a directory.
The cp command also allows you to use pattern matching. For example, to copy all files
pngfrom current directory to directory
/backupthat you will use:
cp *.png /backup
How to Copy Directory in Linux with cp Command
To copy a directory, including all files and subdirectories, use the -R option. In the following example we copy the directory
~/Picturesto the directory
cp -R Pictures Pictures_backup
The above command will create a destination directory and recursively copy all files and subdirectories from the source to the destination directory.
The options used when copying files can also be used when copying directories. The main difference is that when copying directories you need to use options
How to Copy Files and Directories on Linux with the rsync Command
Rsync is a fast and versatile command line utility. Rsync syncs files and directories between two locations. Rsync can be used to copy files to either a local location or a remote location.
Rsync provides a number of options that control every aspect of its behavior. The most used options are
-awhich will copy directories recursively, transfer custom devices and block, preserve symbolic links , modification times, groups, ownership, and file permissions.
To copy one file from one location to another you can use the following command:
rsync -a file.txt file_backup.txt
If the destination file exists, rsync will overwrite it.
You can use the same command as above to copy directories with rsync:
rsync -a / var / www / public_html / / var / www / public_html_backup /
Note that Rsync considers slashes
/at the end of the source with a different concept. If you add a trailing slash to the source directory, it will only copy the contents of the directory to the destination directory. When the trailing slash is removed, rsync will copy the directory source into the destination directory.
In this guide, we’ve learned how to copy files and directories on Linux and Unix based systems using the cp and rsync utilities.