Introduction
This guide will show you how to copy files and directories on Linux by running commands from the command line. Also, the commands listed below detail how to create system-wide backups or filter and copy only specific files.
Using the cp command to copy files and directories on Linux The cp command is the primary method
for copying files and directories on Linux
. Virtually all Linux distributions can use cp. The basic format
of the command is: cp [additional_option] source_file target_file
For example:
cp my_file.txt my_file2.txt
This Linux command creates a copy of the my_file.txt file and renames the new file to my_file2.txt
.
By default, the cp command runs in the same directory in which you are working. However, the same file cannot exist twice in the same directory. You will need to rename the destination file to copy it to the same location. Some users will add _old, some will add a number, and some will even change the three-letter extension (e.g. .bak instead of .txt).
You may not receive a warning before Linux overwrites your file; be careful or refer to the -i option below.
Additional options
Additional options can be used in combination with the cp command
: –
- v verbose: shows the progress of multiple copied files-p
- preserve: maintains the same attributes, such as creation date and file permissions-f force: force copy by deleting an existing file first-i
- interactive : asks for confirmation, strongly recommended
- R recursive: copies all files and subfolders to a directory-u
- only if the source is newer than
–
update: copy
the destination How to copy a file to another directory on
Linux To copy
a file from the directory you
are working in to a different location, use the command:
cp my_file.txt /new_directory
There is no need to rename the file unless there is already one with the same name in the destination directory.
To specify a path for the source file:
cp /etc/my_file.txt /new_directory
This allows you to copy without having to change directories. The cp command will create the /new_directory if it does not exist.
To rename and
copy a file to a different path: cp my_file.txt /new_directory/my_file2.txt
This option is useful for backing up configuration files or copying data to
a storage device. Copy multiple files from one directory to
another on Linux
You may need to copy more than one file at a time
.
List each file to be copied before the destination directory:
cp my_file.txt my_file2.txt my_file3.txt /new_directory This example created a copy of all three files in the /new_directory folder.
Use a wildcard to specify all files that share a string:
cp /pictures/*.jpg /new_directory
This would find all files with the .jpg extension in the /pictures directory and copy them to the /new_directory folder.
To copy an entire folder and its subfolders and files, use the
-R: cp -R /documents /new_directory
-R stands for recursive, which means “everything in that location”. This would copy all files, as well as all directories, to the /new_directory folder.
Copy using the
rsync command
The rsync command on Linux is used to synchronize or transfer data between two locations. Usage is similar to cp, but there are some key differences to be aware of.
To copy a single
file, enter the following in a terminal: rsync -a my_file.txt /new_directory/my_file_backup.txt
- The -a option means everything, and is included with the rsync commands – this preserves subdirectories, symbolic links, and other metadata
- my_file.txt file in the working directory. Replace
- new_directory/ with the destination.
- Usage of my_file_backup.txt as destination indicates that the file will be renamed during copying.
. Replace the
/
To copy a directory with rsync
, enter the following: rsync -a
/etc/docker/ /home/backup/docker/
This copies the contents of the /etc/docker/ directory to /home/backup/docker/. Be sure to keep the bars. Omitting the forward slash in the source directory will copy the contents to a subdirectory.
To skip files from being copied, see our guide on how to exclude files and directories from data transfer using the rsync command.
Other options
The ls command
is a useful companion to the cp command on Linux
.
To list the contents of a directory, enter the command:
ls /directory
The previous example shows all the files in /directory. Use this command after copying to verify that the files were copied successfully.
To change directories, use cd and the directory name. For example:
cd /other_directory
The command prompt changes to show that you have changed directories
.
Conclusion
Now you understand how to copy files on Linux. The cp command is a versatile and powerful tool for managing and backing up files.