The cp command came from the word copy. This command is used to copy files and directories on Unix and Linux systems. As a Linux user, you will use the cp command almost every time you log into your machine.
The cp command was first introduced in early Unix systems and now works with almost all Linux and Unix-like distributions.
In this article, I will explain how to use the cp command with multiple examples.
Contents
How to use the cp Command
The general syntax for the cp command is given below:
cp [OPTIONS] [SOURCE] [DESTINATION]
There are some basic rules you should remember when using the cp command:
- The source can be a single file, multiple files, or directories, whereas the destination can be a file or a directory.
- If the file or directory you are targeting is located outside the current working directory, you need to specify its explicit file path.
- If the file already exists in the destination folder, it will be overwritten.
Syntax of the cp Command
Besides the general cp command, the syntax slightly changes when copying a single file, multiple files, or directories. The basic syntax for different use cases is given below.
Copy a Single File
cp source_file destination_file
Copy Multiple Files
cp file1 file2 file3 destination_dir
Copy a Directory
cp -R source_dir destination_dir
Copy Multiple Directories
cp - R dir_1 dir_2 dir_3 destination_dir
Copy Files and Directories Simultaneously
cp -R dir_1 dir_2 file_1.txt file_2.txt destination_dir
cp Flags
Like many other Linux commands, the cp command also provides several flags to enhance or customize its operation. Some important options available in the cp command are discussed below.
-r or -R | Copy directories recursively (including all files and subdirectories) |
-i | Ask for confirmation before overwriting a file |
-n | Do not overwrite an existing file |
-u | Copy only if the source file is newer than the destination file |
-v | Show detailed output of the copying process |
-a | Archive mode; preserves permissions, timestamps, links, etc. |
-p | Preserve file attributes such as ownership, permissions, and timestamps |
-f | Force copying by removing the destination file if needed |
-l | Create a hard link instead of copying the file |
-s | Create a symbolic (soft) link instead of copying |
-t | Specify the destination directory first |
--parents | Preserve the directory structure of the source file |
View all the options of the cp command and their descriptions using the command below, or check the cp man page for detailed information.
man cp
or
cp --help
We will see the detailed uses of the important options of the cp command in the following section.
Copy a File in Linux
Basic Syntax
cp source_file destination_file
An important thing to note is that if the file already exists in the destination directory, it will be overwritten without warning.
Copy a file within the current directory
You can copy files within your current directory by typing a command like this:
cp notes.txt notes_backup.txt
You must use a different name if you copy within the same directory.
Copy a file to another directory
To copy the file to another directory, write the destination path explicitly.
cp notes.txt /home/backup/notes.txt
If the source or destination is located outside the current working directory, you need to specify the complete path.
cp /Downloads/notes.txt /home/backup/notes.txt
Copy Files to a Directory in Linux
You can copy multiple files at once to a directory using the following cp command syntax.
cp src_file1 src_file2 destination_dir
Example:
Suppose we want to copy multiple files to the /home/notes_backup/ directory. Then execute the command below.
cp note_1.txt note_2.txt note_3.txt /home/notes_backup/
Copy Directories in Linux
By default, the cp command is designed to copy files only. A directory contains multiple files and possibly subdirectories. Therefore, you must use the -r or -R flag to copy a directory and all of its contents.
The basic syntax for copying a directory in Linux is shown below.
cp -R source_dir destination_dir
Here, the `-R` option is used to copy the directory recursively.
Example:
The example below will copy all the contents of /var/www/public_html to /home/backup.
cp -R /var/www/public_html /home/backup
Copy Multiple Directories at Once
We can copy multiple directories simultaneously using the syntax below.
cp -R source_dir1 source_dir2 source_dir3 destination_dir
Example:
The example below copies the Documents, /Downloads/Musics/, and notes directories to the /home/backup/ directory.
cp -R Documents /Downloads/Musics/ notes /home/backup/
You can also use both files and directories as sources in the same command.
cp -R file1.txt file2.txt dir1 dir2 /backup/
Use of flags Available in cp Command
While copying files, you have a few options to control whether existing files should be overwritten.
In a normal copy operation, if a file with the same name already exists in the destination directory, it will be overwritten without any confirmation or notice.
Use the -i option for confirmation before overwriting
The -i option prompts for confirmation before overwriting an existing file.
cp -i file.txt /backup/file.txt
If file.txt already exists in the backup directory, the system will ask:
overwrite 'backup/file.txt'?
You can then type y to overwrite the file or n to cancel the operation.
Use the -n option to skip overwriting duplicate files
The -n option prevents existing files in the destination directory from being overwritten.
cp -n file.txt /backup/file.txt
Use the -u option to copy only if the source is newer
The -u option copies or overwrites the file only if the source file is newer than the destination file.
cp -u file.txt /backup/file.txt
This option is useful when you want to update files without overwriting newer versions in the destination directory.
Advanced Uses of cp Command
Verbose Option
The verbose option (-v) makes the cp command display each file as it is being copied.
cp -v movie.mp4 /home/tmp
Preserve File Attributes
The -p flag tells the cp command to preserve file attributes, such as file permissions, ownership, and timestamps, while copying.
cp -p file.txt backup/
Use the `-a` option (archive mode) to preserve even more file information.
cp -a file.txt backup/
The -a (archive) option preserves permissions, ownership, timestamps, and symbolic links.
Use of wildcard with cp Command in Linux
Wildcards allow you to select and copy multiple files that match a specific pattern instead of specifying each file name individually.
Example:
To copy all the text files inside a directory, use the command below:
cp /home/notes/*.txt /home/backup/
Copy files with a single-character variation
cp file?.txt /home/backup/
This command copies files that match patterns like:
file1.txt
file2.txt
fileA.txt
Copy files within a range
cp file[1-3].txt backup/
This command copies:
file1.txt
file2.txt
file3.txt
Preserve the Parent Directory Structure
To preserve the parent directory structure, use the --parents option.
To understand this, compare it with the normal copy process. Suppose your file is located in dir1/dir2/file.txt, and you run the following command:
cp dir1/dir2/file.txt backup/
Result:
backup/
└── file.txt
Only the file is copied, and the directory structure (dir1/dir2) is not preserved.
Now run the same command with --parents:
cp --parents dir1/dir2/file.txt backup/
Result:
backup/
└── dir1/
└── dir2/
└── file.txt
Now the entire directory path is recreated inside the backup directory.
Copy Only the Contents of a Directory Using
By default, when you copy a directory to another directory, the cp command places the source directory inside the destination directory. However, when the -T option is used, the destination is treated as the exact target location, and only the contents of the source directory are copied, instead of creating a nested directory.
Let’s understand the difference with examples.
In the normal copy process, if you run:
cp -r dir1 dir2
The output directory structure:
dir2/
└── dir1/
├── file1
└── file2
Here, the directory dir1 is copied inside dir2.
Now run the command with the -T option:
cp -rT dir1 dir2
The output directory:
dir2/
├── file1
└── file2
In this case, the contents of dir1 are copied directly into dir2, without creating the dir1 directory inside it.