cp command

In this tutorial:

Command structure Examples

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.

Command Structure

Command: cp
Synopsis: cd [OPTION]… SOURCE… DIRECTORY

List of Options:

NOTE: Some options have only a short name, some only a long name, and some both. If an option has both, then you may use either format.
Short NameLong nameDescription
-a–archiveThis is the same as using the option cobination:
-dR –preserve=all
–backup[=CONTROL]Make a backup of each existing destination file.
-bLike –backup, but does not use an argument
–copy-contentsCopy contents of special files when recursive.
-dSame as the –no-deference — preserve=links combination
-f–force If an existing destination file cannot be opened, remove it and then try again.
-i–interactivePrompt before an overwrite occurs
-HFollow command-line symbolic links in the source.
-l–linkLink files instead of copying them
-n–no-cobblerDon’t overwrite an existing file.
-P–no-deferenceNever follow symbolic links in the source
-pThis 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’
-cThis 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).
–parentsUses the full source file name under the DIRECTORY
-R, -r–recursiveCopy the directories recursively.
–remove-destinationRemoves the existing destination file before opening it. This option is the opposite of –force.
–strip-trailing-slashesRemoves any trailing slashes form he source parameter.
-s–symbolic-linkThis option will override the normal backup suffix with the entered parameter.
-t–target-directory=DIRECTORYCopies all documents from the source into the specified DIRECTORY
-u–updateThis copies source files only when they are newer than the destination files or if the destination file is missing.
-v–verboseThis explains what is being done by displaying it on the console.
-x–one-file-systemMake a backup of each existing destination file.
-z–context=TEXTThis option sets the security context of the copied file to CONTEXT

Examples

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

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+