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.
|Copy Files and Directories
|Root or Sudo Privileges
|Ubuntu, Manjaro, Fedora, etc.
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:
|Print what files are being copied.
|Preserved the access mode and the timestamp of the source file.
|Force the process by removing or replacing the existing files.
|It will ask you if you want to do the current action. Use this to make sure you don’t lose any files.
|If file “
A” is copied as file “
B” and file “
B” already exists in the same location, then it will rename it to “
|Recursively copy all the files and subdirectories into a directory.
|Avoid overwriting existing files.
|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
Or, you can copy the file to a different path by specifying the absolute or relative path.
$ cp file.txt ~/Documents/
If the file already exists in the destination path, give it a different name.
$ cp file.txt ~/Documents/file2.txt
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/
If multiple files share a common syntax or order, like “
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/
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 “
$ cp -r mydir/ ~/Documents/
Displaying the File Names While Being Copied
-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/
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/
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/
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/
This way, your existing file will not get replaced or deleted while copying.
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.