When working on the command line, one of the most typical jobs is copying files and folders. On Linux, there are various commands for copying files, the most popular of which being
To copy files, use the
cp command, and to copy directories, use the
In this tutorial, you will copy files and directories on Linux. We will also address a few FAQs on how to copy files and directories on Linux.
cp Command to Copy Files
cp command is used to copy files and directories on Linux and Unix operating systems.
Copying a file in the current working directory is the most basic usage case. For instance, to transfer a file named
file.txt to a file named
file_backup.txt in the current directory, use the following command:
cp file.txt file_backup.txt
The target file will be overwritten if it already exists. Use the
-i option to get a confirmation prompt before overwriting the files.
cp -i file.txt file_backup.txt
When you use the
cp command to copy a file, the resulting file is automatically owned by the person who issued the command. To keep the file mode, ownership, and timestamps, use the
cp -p file.txt file_backup.txt
-v option is another useful option. When this option is selected, the program prints the following:
cp -v file.txt file_backup.txt
'file.txt' -> 'file_backup.txt'
Copy a File to a Directory
To copy a file to a directory, specify the directory's absolute or relative path. The file is transferred to the current directory when the destination directory is omitted.
We'll copy the file
file.txt to the
/backup directory in the following example:
cp file.txt /backup
The copied file will have the same name as the original, if simply the directory name is specified as a destination.
You must specify the desired file name if you wish to copy the file under a different name:
cp file.txt /backup/new_file.txt
The program above will save the file as
new_file.txt in the chosen directory.
Copy Multiple Files
Specify the names of the source files and directories first, then the destination directory as the last option to copy several files and directories at once:
cp file.txt dir file1.txt file2.txt dir1
The destination must be a directory when copying several files.
You can also utilize pattern matching with the
cp command. To copy all
.png files from the current directory to the
/backup directory, for example, type:
cp *.png /backup
Copying Directories with
-r option to copy a directory, including all of its files and subdirectories. We'll copy the directory
Pictures_backup in the example below:
cp -R Pictures Pictures_backup
The command above creates the destination directory and copies all files and subdirectories from the source directory to the destination directory recursively.
The source directory and its contents are copied to the destination directory if the destination directory already exists. Use the
-T option to copy only the files and subdirectories, not the target directory:
cp -RT Pictures Pictures_backup
The same options that are used to copy files can also be used to copy directories. The key difference is that you must use the
-R option when copying folders.
rsync Command to Copy Files and Directories
rsync is a command-line application that synchronizes files and directories between two places quickly and easily. It is capable of copying files to both local and remote places.
rsync has a plethora of settings that allow you to fine-tune every element of its operation.
-a option is the most useful because it copies directories in a recursive manner, transfers special and block devices, and preserves symbolic links, modification times, group, ownership, and rights.
You would use the following command to copy a single file from one location to another:
rsync -a file.txt file_backup.txt
rsync will overwrite the target file if it already exists.
To clone a directory, use the same command:
rsync -a /var/www/public_html/ /var/www/public_html_backup/
rsync behaves differently when it comes to source folders with a trailing slash
/. If the source directory has a trailing slash, the command will only transfer the contents of the source directory to the destination directory.
rsync will copy the source directory inside the destination directory if the trailing slash is missing. Including the trailing slash
/ on both the destination and source is always the safest approach.
Check out the following articles to learn more about
FAQs to Copy Files and Directories on Linux
Can I copy multiple files at once on Linux?
Yes, you can copy multiple files using the
cp command by specifying the names of all the source files followed by the destination directory. For example,
cp file1.txt file2.txt destination/.
How can I copy a directory on Linux?
To copy a directory along with its contents, use the
--recursive) option with the
cp command. For example,
cp -r source_directory/ destination/.
What is the difference between
cp command is used to copy files, while
cp -r is used to copy directories and their contents recursively.
How do I retain the timestamps while copying files?
To preserve the original timestamps (mtime, atime) of the files when copying, use the
--preserve) option with the
cp command. For example,
cp -p source.txt destination/.
Can I copy files from one remote server to another?
Yes, you can copy files from one remote server to another by using tools like
scp (secure copy) or
Is there a way to show the progress while copying large files or directories?
Yes, you can use the
rsync command with the
--progress option to display the progress while copying large files or directories. For example,
rsync --progress source_directory/ destination/.
How do I copy files and directories with a single command?
cp command can copy both files and directories. Simply provide the appropriate source and destination paths to copy files or directories accordingly.
rsync utility, we demonstrated how to copy files and directories on Linux and Unix-based systems.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.