Sep 21, 2023 4 min read

Cp Command in Linux (Copy Files)

In this tutorial, we will explain how to use the cp command which is used for copying files and directories on Unix and Linux systems.

Cp Command in Linux (Copy Files)
Table of Contents

The "cp" command in Linux is a command-line utility used for copying files and directories from one location to another. It stands for "copy" and is a fundamental command for file management.

The "cp" command provides several options for various copying scenarios, such as preserving file attributes, copying recursively, overwriting existing files, and more.

In this tutorial, we will explain how to use the cp command. We will also address a few FAQs on cp Command in Linux (Copy Files).

How to Use cp command

The cp command's general syntax is as follows:

cp [OPTIONS] SOURCE... DESTINATION

A single file or directory may be used as the DESTINATION parameter, while the SOURCE argument may contain one or more files or directories.

  • When both the SOURCE and DESTINATION options are files, the cp command copies the first file to the second. If the file doesn't already exist, the command creates it.
  • When the SOURCE receives multiple files or folders as inputs, the DESTINATION option must be a directory. In this instance, the SOURCE files and folders are moved to the DESTINATION directory.
  • The cp command transfers the first directory into the second when the SOURCE and DESTINATION variables are both directories.

To copy files and directories, you need to read permission on the source file and write permission on the destination directory. Otherwise, you'll get a permission denied error.

Copying Files with cp Command

The simplest use of cp is to copy a file into the current working directory. You might run the following command to copy a file named file.txt to file backup.txt:

cp file file_backup

or

cp file{,_backup}

Specify the absolute or relative path to the destination directory when copying a file to another directory.

The duplicated file has the same name as the original file when simply the directory name is supplied as a destination.

In the example below, we'll copy the file file.txt to the /backup directory:

cp file.txt /backup

If you want to duplicate the file under a different name, you must give the desired file name. The following command will copy the file as new_file.txt to the directory you chose.

cp file.txt /backup/new_file.txt

By default, the destination file is overwritten if it already exists. Cp is instructed not to replace an existing file by the -n option.

Use the -i option to force cp to ask for confirmation.

cp -i file.txt file_backup.txt

Use the -u argument when running the command to only copy files that are more recent than the destination:

cp -u file.txt file_backup.txt

The user running the command owns the new file created when copying a file. Use the -p option to preserve the file mode, ownership, and timestamps:

cp -p file.txt file_backup.txt

Another important option is -v, which instructs cp to print verbose output:

cp -v file.txt file_backup.txt
Output

'file.txt' -> 'file_backup.txt'

Copying Directories with cp command

Use the -R or -r option to copy a directory, including all of its files and subdirectories.

We'll copy the directory Pictures to Pictures_backup in the example below:

cp -R Pictures Pictures_backup

The aforementioned command creates the target directory and recursively copies all files and subdirectories from the source directory to it.

If the destination directory already exists, the source directory and its contents are copied there.

To copy only the files and subdirectories and not the source directory, use the -T option:

cp -RT Pictures Pictures_backup

You can also duplicate a directory's contents only, excluding the directory itself, by using the wildcard character (*). The following command has the drawback of not copying the hidden files and folders (those beginning with a dot .):

cp -RT Pictures/* Pictures_backup/

While copying directories, you can use all the same settings that we did when transferring files in the previous section. The main difference is that when copying directories, you must always use the -R option.

Copy Multiple Files and Directories

The names of the files and directories to be copied should be specified, and the final parameter should be the location directory:

cp file.txt dir file1.txt  dir1

The destination must be a directory when copying several files.

FAQs to cp command in Linux (Copy Files)

Can I use the "cp" command to copy multiple files at once?

Yes, you can copy multiple files simultaneously by providing multiple source file names and specifying a destination directory.

What happens if the destination file or directory already exists during the copy?

By default, if the destination file or directory already exists, the "cp" command will overwrite it with the new copy. To prevent overwriting, you can use the "-n" (or "--no-clobber") option.

Can I preserve file attributes, such as permissions and timestamps, while copying files?

Yes, you can preserve file attributes by using the "-p" (or "--preserve") option with the "cp" command.

How can I display a progress bar while copying files with the "cp" command?

You can use the "-v" (or "--verbose") option with the "cp" command to display a verbose output, including a progress bar during the copy process.

Can I use the "cp" command to copy files between different directories or even across different systems?

Yes, the "cp" command allows you to copy files between directories on the same system. For copying files across different systems, you would typically use other methods like SSH, SCP, or Rsync.

Is there a way to confirm each file overwrite when using the "cp" command?

Yes, you can use the "-i" (or "--interactive") option to prompt for confirmation before overwriting existing files during the copy.

Are there any other useful options available with the "cp" command?

Yes, some other commonly used options include:

  • "-u" (or "--update") to copy only when the source file is newer than the destination file.
  • "-l" (or "--link") to create hard links instead of copying files.
  • "-s" (or "--symbolic-link") to create symbolic links instead of copying files.
  • "--help" to display the command's help documentation.

Conclusion

Using the cp command to copy files and directories is a basic process. Type man cp in your terminal to learn more about the possible cp options.

Use the rsync and scp utilities to copy files over a network.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.