In this tutorial:
At first, the cp command seems like a very straightforward command. While it is certainly a simple concept to copy a file or directory from one location to another, it has many different options involved to tailor the process. The cp command is also one of the most used Linux commands. Below we go over the options you can use with the command followed by some of the more commonly used option combinations.
Synopsis: cd [OPTION]… SOURCE… DIRECTORY
List of Options:
|Short Name||Long name||Description|
|-a||–archive||This is the same as using the option cobination:|
|–backup[=CONTROL]||Make a backup of each existing destination file.|
|-b||Like –backup, but does not use an argument|
|–copy-contents||Copy contents of special files when recursive.|
|-d||Same as the –no-deference — preserve=links combination|
|-f||–force||If an existing destination file cannot be opened, remove it and then try again.|
|-i||–interactive||Prompt before an overwrite occurs|
|-H||Follow command-line symbolic links in the source.|
|-l||–link||Link files instead of copying them|
|-n||–no-cobbler||Don’t overwrite an existing file.|
|-P||–no-deference||Never follow symbolic links in the source|
|-p||This is the same as using –preserve=mode,ownership,timestamps|
|–preserve[=ATTR_LIST]||This option preserves the specified attributes. ‘mode’,’ownership’,’timestamps’ are the default attributes preserved. Additional attributes are ‘context’,links’,’xattr’,’all’|
|-c||This is the same as –preserve=context|
|–no-preserve[=ATTR_LIST]||When using this command, list the attributes you do not want to preserve (same available attributes as –preserve).|
|–parents||Uses the full source file name under the DIRECTORY|
|-R, -r||–recursive||Copy the directories recursively.|
|–remove-destination||Removes the existing destination file before opening it. This option is the opposite of –force.|
|–strip-trailing-slashes||Removes any trailing slashes form he source parameter.|
|-s||–symbolic-link||This option will override the normal backup suffix with the entered parameter.|
|-t||–target-directory=DIRECTORY||Copies all documents from the source into the specified DIRECTORY|
|-u||–update||This copies source files only when they are newer than the destination files or if the destination file is missing.|
|-v||–verbose||This explains what is being done by displaying it on the console.|
|-x||–one-file-system||Make a backup of each existing destination file.|
|-z||–context=TEXT||This option sets the security context of the copied file to CONTEXT|
Using the options above, you can create many different combinations to tailor the copy to your own needs. Below are a few examples of the more common uses of the cp command.
Make a backup of a file
This basic format allows you to create a backup copy of a specific file. This is highly recommended before making any changes to a file.
# cp test.txt test.txt.bak # ls test.txt test.txt.bak
Copy a file from one directory to another
In this example you can see we are copying the test.txt file from the current directory to the sub directory named files. This performs very similarly to the mv command, except this one leaves a copy in the current location in addition to creating the copy in the files folder.
# cp test.txt files/test.txt
Copy a file and preserve attribute information
When copying a file, by default it will not copy attributes such as the owner or timestamp. Using the –preserve option you can force it to also copy those attributes. First we list the files in the directory to show you the owners and timestamps.
# ls -l drwxr-xr-x 2 userna5 userna5 4096 Oct 21 14:42 ./ drwxr-x--- 31 userna5 nobody 4096 Oct 21 09:53 ../ -rw-r--r-- 1 userna5 userna5 896 Sep 29 15:04 test.txt -rw-r--r-- 1 userna5 userna5 1212 Sep 29 15:04 index.php
Next, we run the command to copy the test.txt file to test.txt.bak, using the –preserve command. By default, this retains the timestamp, mode (permissions), and ownership.
# cp --preserve test.txt test2.txt
Now you can see below that the new file retained the permissions, user, and timestamp.
# ls -l drwxr-xr-x 2 userna5 userna5 4096 Oct 21 14:42 ./ drwxr-x--- 31 userna5 nobody 4096 Oct 21 09:53 ../ -rw-r--r-- 1 userna5 userna5 896 Sep 29 15:04 test.txt -rw-r--r-- 1 userna5 userna5 896 Sep 29 15:04 test2.txt -rw-r--r-- 1 userna5 userna5 1212 Sep 29 15:04 index.php
Copy a directory
Here we show you how to copy a directory and its contents to another directory. This moves the entire folder as well, so if we copy the test folder into the files folder, then there will be a test folder inside the files folder and a copy of all the contents of the test folder will be inside the files/test folder. Note that we use the -r option when performing this. The -r option stands for recursive which means that it copies all of the files inside the source folder.
# cp -r files test