In UNIX/Linux systems, the cp command is the most commonly used command that allows you to copy files and directories from one location to another.
If you don’t know how to use this command, this article will show you how to use it in a variety of ways, from the most basic to the most advanced.
Tutorial Details
Description | Copy Files and Directories |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | cp |
Internet Required | No |
Syntax of the cp Command
The cp command takes three arguments: one is the option, and the other is the source and destination paths.
$ cp [OPTION] [SOURCE] [DESTINATION]
There are a few things you should know before using this command, followed by:
- If the “
[DESTINATION]
” is not specified with the “[SOURCE]
“, then it will force you to specify it. - If “
[SOURCE]
” and “[DESTINATION]
” are in the same location, any already existing file that matches the name of “[DESTINATION]
” will get replaced without any warning (or use the “-i
” flag to get the confirmation message before replacing).
The following are a few known options that you should know before using this command:
Options | Description |
---|---|
-v | Print what files are being copied. |
-p | Preserved the access mode and the timestamp of the source file. |
-f | Force the process by removing or replacing the existing files. |
-i | It will ask you if you want to do the current action. Use this to make sure you don’t lose any files. |
-b | If file “A ” is copied as file “B ” and file “B ” already exists in the same location, then it will rename it to “B~ “. |
-r | Recursively copy all the files and subdirectories into a directory. |
-n | Avoid overwriting existing files. |
-u | Overwrite if the target file is old or doesn’t exist. |
Copying a File Using the cp Command
If you intend to copy the file to the same location, then rename it while copying.
$ cp file.txt file1.txt
Output:
Or, you can copy the file to a different path by specifying the absolute or relative path.
$ cp file.txt ~/Documents/
Output:
If the file already exists in the destination path, give it a different name.
$ cp file.txt ~/Documents/file2.txt
Output:
You can copy several files with their full paths to the same destination path by putting a space between each file.
$ cp file.txt file2.txt ~/Documents/
Output:
If multiple files share a common syntax or order, like “file1.txt
“, “file2.txt
“,.., “file.5
“, then instead of specifying their full paths, you can use the wildcard to specify all files that share the same filename.
$ cp file*.txt ~/Documents/
Output:
The following is a list of wildcard usage.
$ cp *.txt ~/Documents/ #Copy all files sharing the "txt" extension
$ cp *.jpg ~/Documents/ #Copy all files sharing the "jpg" extension
Copying an Entire Directory and its Subdirectories to a New Path
You can copy all of the files and subdirectories from the source directory to the destination directory by using the “-r
” flag.
$ cp -r mydir/ ~/Documents/
Output:
Displaying the File Names While Being Copied
The “-v
” flag will print each file’s and subdirectory’s name in the output while they are being copied to the destination directory.
$ cp -rv mydir/ ~/Documents/
Output:
Prompting for Permission Before Copying the Files
If the source files already exist in the destination path, they get replaced. To prevent this from happening, you can use the “-i
” flag to prompt for “yes
” or “no
” before overwriting the files.
$ cp -ri mydir/ ~/Documents/
Output:
Backup the Already Existing Files
If the file is already present in the destination path, the “-b
” flag will create a copy and add “~
” to the end of the filename.
$ cp -rb mydir/ ~/Documents/
Output:
Only Copying the Unique Files
You can use the “-n
” flag instead of the first two commands. This will only copy new files and directories from the source to the destination directory, leaving the ones that are already there alone.
$ cp -rn mydir/ ~/Documents/
Output:
This way, your existing file will not get replaced or deleted while copying.
Final Tip!
When copying files or directories from one path to another, the cp command is best; however, some users prefer the rsync command; it is entirely up to you.
To learn more about the cp command, you can check its man page using the “man cp
” command.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.