cp command is a UNIX CLI tool for creating copies of files and directories across a filesystem. Although modern desktop Linux distributions provide the copy functionality via GUI, the command-line method is still more efficient when working with multiple files.
This article will introduce the
cp command in Linux and provide examples of its use.
- Sudo privileges.
- Access to the terminal.
cp Command Syntax
cp command syntax for copying files and directories is simple but offers various options for performing advanced actions. Below is the basic form of the command for copying files:
cp [options] [source] [destination]
To copy directories, add the
-r (recursive) option:
cp -r [options] [source] [destination]
If the source command argument is a file, the destination can be another file or a directory. If the source is a directory, the destination must also be a directory.
Note: If a destination directory does not exist, the
cp command cannot create it and reports an error. Ensure you have first created the destination with the mkdir command.
cp Command Options
cp command options allow users to see more details about the copying operation, control the process, create file backups, and see the help file. The table below contains the most commonly used
|Preserve the source's metadata, such as creation date, permissions, and extended attributes.
|Create backups for destination files. The backup file has the (~) suffix unless --suffix is used.
|If the destination file/directory already exists, replace it with the source.
|Ask before overwriting the destination with the source.
|Create hard links instead of new files. The destination file will have the same inode attribute as the source.
|Do not overwrite the destination file if it exists.
|-R, -r, --recursive
|Copy the source directory recursively.
|Provide a custom suffix for the backup file.
|Specify a directory for the source files.
|Replace files only if they satisfy the update condition. The short option replaces files if the destination is older than the source. The long option lets the user specify the condition (all, none, or older).
|Output information about the copying process.
|Show version information.
cp Command Examples
The options listed above allow the
cp command to be customized to fit a specific scenario. Below are examples of the operations that
cp can perform.
Copy Multiple Files to Directory
Copy file-1.txt, file-2.txt, and file-3.txt to the test-dir directory.
cp file-1.txt file-2.txt file-3.txt test-dir
The command has no output, but use the ls command to verify that the files are now in test-dir.
Use Wildcard Symbols
Use the (*) symbol as a wild card if the filenames follow a pattern. The following example copies all the files that start with file- to test-dir.
cp file-* test-dir
Use Interactive Prompt on File Overwrite
Copy file-1.txt to test-dir. Show verbose output and prompt to overwrite if the file exists at the destination.
cp -v -i file-1.txt test-dir
Create File Backup
Copy file-1.txt to test-dir and create a backup file with the .bak extension. Show verbose output.
cp -v -b -S .bak file-1.txt test-dir
This tutorial presented the syntax and the commonly used options for the
cp command in Linux. It also offered examples of the command to help you understand its use.
To take the next step in mastering the Linux command line, read Linux Commands All Users Should Know.