Copying files and directories from one path to another is one of the most frequent tasks a computer user performs. In Linux and UNIX operating systems, you can copy files using a command-line utility, such as the cp command in Linux. Unlike, a graphical user interface such as drag-drop, command-line utilities copy and paste files and directories in a pretty simple way.
In this tutorial, we will learn how to use the Linux cp command to copy files and directories.
Prerequisites
- A Linux computer.
- Basic knowledge of Linux commands.
- Several files and directories that you can use to try example commands.
What Does Cp Command Do in Linux
cp, also known as copy, is a Linux command that copies a single file, multiple files, and directories from the source to the destination. This command creates a replica of a file or directory on the disk with the same or different name. The content of the original file remains unaffected.
The cp command performs the following operations in Linux:
- Copies contents of one file to another file.
- Copies a file or directory from one directory to another directory.
- Copies multiple files to another directory.
The behavior and result of these operations depend on multiple factors, such as if the destination file or directory exists or not.
Syntax
cp is one among the commands from the GNU Core Utilities. The most common syntax for the cp command:
cp source_file destination_file
cp source_directory destination_directory
cp source_file1 source _file2 source_file3 destination_directory
cp [option] source_file destination_file
cp [option] source_directory destination_directory
How to Use Linux Cp Command
The Linux cp command has different modes of operations, which depends on the arguments you pass in the command:
1. Two File Name Arguments - If you need to copy the contents of the first file to the second file, then specify both file names as the arguments. If the destination file does not exist, then the cp command creates the file. However, if the file already exists, then it replaces the file without any warning. For example:
cp source_file destination_file
2. Two Directory Name Arguments - If you need to copy files and subdirectories from one directory to another directory, then specify both directory names as the arguments. Specify the -r
option to copy all subdirectories. If any of the files in the source does not exist in the destination directory, then the cp command creates the files. For example:
cp source_directory destination_directory
In this example, if the destination directory does not exist, then the cp command creates the directory and copies the content of the source as it is. However, if the destination exists, then the copied directory becomes a subdirectory of the destination.
3. Multiple Arguments - If you need to copy multiple files from the source to the destination, then specify the list of file names followed by the directory name. The destination must exist for this command to work. For example:
cp source_file1 source_file2 source_file3 destination_directory
Depending upon your requirement, you can choose any of the above operation modes.
CP Command Examples
The cp command has multiple options that enable you to perform different copy operations. The following examples show you some of these options in action.
Copying Source File to Destination file
To copy the content of a file to another file, use the following command:
cp users.txt admins.txt
By default, the cp command overwrites the file if it already exists. To avoid overwriting the content of the files, use the -n
option:
cp -n users.txt dev.txt
In this output, the cp command does not return any error and neither does replace the file.
Copying Single File to Destination Directory
To copy a single file from the source directory to the destination, use the following command:
cp users.txt docs/
In this case, the users.txt
file does not exist in the docs
directory, so the cp command copies the file. The trailing slash at the end of the directory is not mandatory.
To copy the content of another file to an existing file in the destination directory, type:
cp dev.txt docs/users.txt
The content of the file is replaced with the content of the original file.
Copying Directory to Destination Directory
To copy a directory from the source directory including files and subdirectories to the destination directory, use -r or -R option.
For example, here we copy the directory named design
in the current directory to another directory named docs
.
cp -r design/ docs/
You may use pwd command to check the current directory, cd to change directory, and ls to list files.
Copying Multiple Files and Directory to a Directory
You can use the cp command for copying files and directories from the source directory to the destination directory. The following command copies files named users.txt
, dev.txt
and directory named docs
to the design
directory:
cp -r users.txt dev.txt docs/ design/
The docs
directory in this example has a file, so we have to use the -r
or -R
option to recursively copy all the contents (all its files and subdirectories) of the docs
directory.
To copy all the contents of the source to the destination directory, type:
cp -r * /home/linuxopsys/design
The cp * command does not copy a directory from the source directory if the name of that particular directory is the same as the destination directory. For example:
cp -r * design
A directory named design
exists in the source and the name of the destination is also design
. Thus the command fails and returns an error.
To copy all png files from the current directory to another directory, type:
cp *.png /home/linuxopsys/images
The * wildcard indicates all, ie *.png means all files with extension png.
Copying Files in Interactive Mode
By default, cp command won't confirm to copy one file over another file with the same name.
To confirm overwriting the files in the destination directory and copy files interactively, use the -i
option:
cp -i users.txt docs/
Preserve Source File Metadata While Copying
Use -p option to preserve the source file metadata, such as ownership and timestamps during the copy operation.
Copy a file named dev.txt
by preserving its metadata to a directory named docs:
cp -p dev.txt docs/
When we copied the file for the first time without using any option, the last modified date changed, but when we used the -p
option, all the metadata remained intact.
View Output
When files are copied, to display all the copy operations on the terminal screen, use -v
option for verbosity:
cp -v -r * /home/linuxopsys/docs
CP Command Options
The following table lists and describes the options of the cp command in Linux:
Option | Description |
---|---|
-i | Warns before overwriting a file. |
-b | Creates a backup of the destination file and stores the backup copy in the same folder with a different name and format. |
-f | Replaces the file in the destination directory if the user does not have write permissions in the destination directory. |
-r or -R | Copies the entire directory structure recursively from the source directory to the destination directory. |
-p | Preserves the source file metadata, including last modified date, last access date, and ownership. |
-v | Prints command background operations on the terminal window. |
-n | Skips overwriting the duplicate files and copies the rest of the files. |
Conclusion
In this tutorial, we learned how to use cp command to copy files and directories. This command has multiple options that you can use as per your requirement. The Linux cp command works on all Linux distributions, such as Ubuntu, Red Hat, Fedora, and CentOS.
For more information browse cp man page or type cp --help
from the terminal.
Comments