Copying with Linux CP Command

Anyone who wants to work with the command line under one of the numerous open source distributions based on Linux needs to familiarize themselves with the many Linux commands out there. These allow you to navigate folder structures, edit data, or copy files and directories. The CP command (short for “copy”) allows you to make copies of selected files or entire folders.

$1 Domain Names – Register yours today!
  • Simple registration
  • Premium TLDs at great prices
  • 24/7 personal consultant included
  • Free privacy protection for eligible domains

Why do you need CP in Linux?

Copying files and folders is one of the basics of any operating system and is performed by most users on a daily basis. Copies are used to share files with other people or to keep them for backup. Usually this tool is a fixed (and already installed) part of every Linux installation – no matter which distribution you use. CP belongs to GNU Core Utilities (short: “coreutils”). This package with the most important tools is available for free use.

Syntax in Linux CP

The basic function of CP is simple: copy one or more files to a location specified by the user. For this, two pieces of information are important: the name of the object and the destination of the action. The syntax of CP is structured like this:

cp [Original] [Destination]

To do this, navigate to the directory of the file to be copied and then specify the file name and the path where the new file should be saved in the command.

cp text.txt /home/user/destinationfolder/

In this example, we create a new file in the destination folder with the same name as the source file. If you want to copy several files at once, list them one after the other within the same command line.

cp text1.txt text2.txt text3.txt /home/user/destinationfolder/

In the previous examples we have always given the copy the same name as the source file. You can freely define the file name by entering it in the destination parameter:

cp text.txt /home/user/destinationfolder/text_copy.txt

If you omit the path of the destination folder, the copy will be created with a new file name in the same folder as the original file.

cp text1.txt text2.txt

Special CP functions

The Linux CP command boasts even more functions. For this you use the options field:

cp [Option(s)] [Original(s)] [Destination]

The following parameters (flags) can be entered into the field to make the copying work more extensive or easier.

Parameter Description Example
-a Copies the file with the same permission settings and metadata as the original. cp -a text.txt /home/user/destinationfolder/
--archive Copies the file with the same permission settings and metadata as the original. cp --archive text.txt /home/ user/destinationfolder/
-b Creates a copy in the buffer if the original and the destination have the same file name but different contents. cp -b text.txt /home/user/destinationfolder/
--backup Creates a copy in the buffer if the original and the destination have the same file name but different contents. cp --backup text.txt /home/user/destinationfolder/
--backup=numbered Creates a new numbered file if the original and destination have the same file name but different contents. cp --backup==numbered text.txt /home/user/destinationfolder/
--backup=existing Creates a new numbered file if the original and destination have the same file name but different contents – and there are already numbered backups. cp --backup==existing text.txt /home/user/destinationfolder/
-d Copies symbolic links. cp -d text.txt /home/user/destinationfolder/
-f Forces overwriting when copying. cp -f text.txt /home/user/destinationfolder/text.txt
--force Forces overwriting when copying. cp --force text.txt /home/user/destinationfolder/text.txt
-i Asks permission before overwriting files with the same name. cp -i text.txt /home/user/destinationfolder/
--interactive Asks permission before overwriting files with the same name. cp --interactive text.txt /home/user/destinationfolder/
-l Creates a hard link instead of a copy. cp -l text.txt /home/user/destinationfolder/
--link Creates a hard link instead of a copy. cp --link text.txt /home/user/destinationfolder/
-n Existing files are never overwritten. cp -n text.txt /home/user/destinationfolder/
--no-cobbler Existing files are never overwritten. cp –no-cobbler text.txt /home/user/destinationfolder/
-p Attributes of the original file are inherited during copying. cp -p text.txt text1.txt
--preserve Attributes of the original file are inherited during copying. cp --preserve text.txt text1.txt
--preserve=mode The mode of the original file is inherited during copying. cp --preserve==mode text.txt text1.txt
--preserve=ownership The ownership of the original file is inherited when it is copied. cp --preserve==ownership text.txt text1.txt
--preserve=timestamp The timestamp of the original file is inherited during copying. cp --preserve==timestamp text.txt text1.txt
--preserve=links Links of the original file are inherited when copying. cp --preserve==links text.txt text1.txt
--preserve=context The security context of the original file is inherited during copying. cp --preserve==context text.txt text1.txt
--preserve=xattr Extended attributes of the original file are inherited during copying. cp --preserve==xattr text.txt text1.txt
--preserve=all All attributes of the original file are inherited during copying. cp --preserve==all text.txt text1.txt
-P Symbolic links are saved as such. cp -P text.txt /home/user/destinationfolder/
--no-dereference Symbolic links are saved as such. cp --no-dereference text.txt /home/user/destinationfolder /
-r Directories are copied recursively including subdirectories. cp -r /home/user/originalfolder/ /home/user/destinationfolder/
-R Directories are copied recursively including subdirectories. cp -R /home/user/originalfolder/ /home/user/destinationfolder/
--recursive Directories are copied recursively including subdirectories. cp --recursive /home/user/originalfolder/ /home/user/destinationfolder/
-s Creates a symbolic link to the original file. cp -s text.txt /home/user/destinationfolder/
--symbolic-link Creates a symbolic link to the original file. cp --symbolic-link text.txt /home/user/destinationfolder/
-S Overwrites a backup suffix when copying with --backup. cp --backup=simple -S text.txt /home/user/destinationfolder/
--suffix=own_suffix Overwrites a backup suffix with its own term when copying with --backup. cp --backup=simple –suffix=own_suffix text.txt /home/user/destinationfolder/
-u Copies the file only if the destination file is older than the original. cp -u text.txt /home/user/destinationfolder/text.txt
--update Copies the file only if the destination file is older than the original. cp --update text.txt /home/user/destinationfolder/text.txt
-v Outputs a message after the copy operation. cp -v text.txt text1.txt
--verbose Outputs a message after the copy operation. cp --verbose text.txt text1.txt
Tip

You can also combine many of these options. To do this, write the parameters and separate them with a space in the command field.

Examples of CP in practice

From the large number of options available for the copy command you can see that CP on Linux can be used for more than just the simple copying of files and folders. By combining the different parameters, you can manage large-scale tasks. The following three examples follow typical usage scenarios in the everyday life of a Linux user.

Numbered backup

Let’s assume that you’re working on a file and want to create a backup of it quite regularly. So that you can also track changes and possibly return to an older status, individual files are created. For this we use the advanced backup option.

cp -f -v --backup=numbered text.txt text.txt

We will create this backup under the same file name and also in the same folder. To make this possible, we use the “force” option. Since we have additionally activated the “numbered” option, new files with numbered file names are still nevertheless in each case. Thanks to “verbose” you get a notification after each copy operation.

In the console, it looks like this:

~$ cp -v -f --backup=numbered text.txt text.txt
'text.txt' -> 'text.txt.~1~'
~$ cp -v -f --backup=numbered text.txt text.txt
'text.txt' -> 'text.txt.~2~'
~$ cp -v -f --backup=numbered text.txt text.txt
'text.txt' -> 'text.txt.~3~'
~$ ls
text.txt  text.txt.~1~  text.txt.~2~  text.txt.~3~
Tip

In Linux, you can use the “ls” command to display a list of the files and subfolders that exist in the folder.

Sorting files with wildcards

You have a folder packed full of files: pictures, documents, and applications are stored all jumbled up. Now let’s assume that you want to copy all images into one folder. Instead of entering all images individually into the command, we work with the asterisk (*), which acts as a wildcard. In our example, we replace the complete file name with an asterisk and specify only the file extensions.

cp *.jpg *.png /home/user/pictures

This command copies all files with JPG and PNG extensions to the images folder. All other file formats are ignored. The original files remain because you only copy data with CP and aren’t moving them.

Copying directories

Recursive copying includes a folder together with its subfolders. So in this case, the directory structure is carried over. This does not work for simple copying with CP: If you try to copy a folder without its directories, you will only get an error message. Instead, you have to set the parameter “-r” or “-recursive” or “-R”.

In our example we want to make a backup copy of a folder including its contents and subfolders regularly. So that not everything has to be copied every time, we additionally use the “update” function.

cp -r -u /home/user/Original/ /home/user/Backups/

When you run the command for the first time, the “Original” directory is created inside the backup folder. All files and subdirectories are copied into it. With large amounts of data, this can take some time. In using the option “-u” you will always only copy changed data.

Alternatives to Linux CP

Despite the numerous options at hand with CP, the command can only be used in a relatively limited way. For example, if you want to create a copy and then delete the original, you have to resort to MV (move). With this command, too, you enter the source object and the destination path.

A bit more sophisticated than CP is the DD command. With this one you have the additional possibility to change the format of the data during the copy process. For this the process proceeds bit by bit. The command is suitable, for example, if you want to copy complete hard disks or partitions.

The Linux command PYCP is also interesting. With this you get a graphical progress display during the copy process. However, you need Python 3.5 (or newer) for it. In newer Ubuntu versions, the programming language is pre-installed by default.

The Linux SCP command can also be helpful. With Secure Copy you can transfer data from one local computer to another or vice versa. This is done via SSH and is therefore an encrypted operation.

Summary

With Linux CP you can copy files and folders quickly and relatively easily. With many options at hand, more complex tasks can also be completed. To perform regular tasks, you can also easily place the command in shell scripts. This way, you can write yourself a script for a weekly backup, for example.

For the daily use of the Linux terminal, it’s a good idea to familiarize yourself with some more commands. For example, learn more about these commands to simplify the workflow:

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top