Linux cp Command: How to Copy Files and Directories

Linux cp command How to copy files and directories

In this tutorial, we explain how to use the cp command in Linux to copy files or directories. Examples are included below.

Linux is one of the most popular Operating Systems on the planet and is the basis for all Android devices. It is open-source and has some clear advantages over other systems such as Windows or macOS.

Within Linux, there are a considerable number of different commands that programmers use regularly.

What is the cp command?

The copy (cp) command is one of the basic and most commonly used of all the Linux commands. It enables the user to copy files or directories from one place to another. When files are copied, the source file name remains the same, but the target file name can be changed if needed.

The common syntax for the cp command

The common syntax form for this command is:

cp [Options] [Source] [Destination]

Common options for the cp command

-v Verbose mode (Show Progress)
-n Do not overwrite an existing fie
-d Copy a link file
-r/R Copy directories recursively
-i Prompt before overwrite
-b Make a backup of each existing destination file
-p Preserve the specified attributes

How to copy files and directories

Below, we’ll go through the methods used to copy files and directories using the cp command.

How to make a copy of a file

To copy a file using the cp command, enter the name of the file you want to copy followed by the destination.

In the example below, the file linxscrew.txt is copied to a new file called linxscrew_updated.txt. The cp command also creates the new file.

cp linxscrew.txt linxscrew_updated.txt

Result:

ls
linxscrew.txt linxscrew_updated.txt

How to copy multiple files to a new location

To copy more than one file using the cp command, pass the names of the files followed by the directory destination. In this example, we are going to copy three files named “linxscrew1.txt“, “linxscrew2.txt“, and “linxscrew3.txt“. We are going to copy them from the directory called “/home/docs” to the directory named “/home/new_docs“. All file names must be separated by a space.

cp /home/docs linuxscrew1.txt linuxscrew1.txt linuxscrew1.txt /home/new_docs

To check that the files have been copied correctly to the new location, you can use the Is command, as shown below:

ls -lh /home/new_docs

How to copy a directory

The cp command does not copy directory contents by default. Therefore, if you want to recursively copy a directory from one place to another, you will need to use the – r option in conjunction with the cp command. This will result in the entire directory, including the subdirectories, being copied to the new target directory. The directory name will remain the same.

The following command will copy the entire directory called linuxscrew, including subdirectories, from the location docs to the location named new_docs:

cp -r /home/docs/linuxscrew/ /home/newdocs/

How to copy multiple directories

This is similar to copying a single directory and allows you to recursively copy multiple directories using a single command. In the example below, we want to copy the directories called linuxscrew_files and linuxscrew_images to the directory called /home/all_linuxscrew_docs.

cp -r /home/linuxscrew_files/ /home/linuxscrew_images/ /home/all_linuxscrew_docs/

How to copy files of a specific format

If you want to only copy files of a specific format on Linux, you can use the wildcard “*” with the relevant file extension. In the example below, we are copying all files that end .txt into the target directory, but the method can be used with any file extension.

cp /home/linuxscrew/*.txt /home/text_files/

How to avoid overwriting an existing file

If you don’t wish to overwrite an existing file when you copy a file in Linux, use the –n option. This will check the target directory for a file of that name and will only carry out the command if there is NOT a file with that name already present. If there is, the command will still be executed but no changes will be made. In the example below the filename is linuxscrew.txt.

cp -n /home/linuxscrew/linuxscrew.txt /home/all_linuxscrew_docs/

How to copy a symlink file

By default, symlinks are excluded when performing a copy operation. If you want to copy a link file then you will need to use the -d option, as shown in the example below:

cp –d /hone/linuxscrew/linuxscrew.txt /home/all_linuxscrew_docs/
SHARE:
nv-author-image

Stefan Durand

My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *