CP Command in Linux with Examples [Copy File in Linux]

Increasing your productivity by going with the shortcuts is always beneficial, For that cp command in Linux is also helpful to do that. This guide will help you to understand copy files in Linux concepts.

Updated: 14 Apr, 23 by Antoniy Yushkevych 5 Min

List of content you will read in this article:

Linux offers flexible and simple commands for performing various operations on the files and folders. One of the commonly performed tasks is to copy files in Linux by running specific commands along with various options for additional functionality. 

In this article, we will be learning various cp command in Linux for copying files along with various options. Apart from this, you will get to know how to create system-wide backups for copying specific files.

You can use the cp command in Linux to copy files and directories from one location to another. It works showhome similar to the mv command that uses to move the file or directory from one location to another. Well, every Linux distribution uses the cp command. Below is the basic format of the cp command that you can use.

cp [additional_option] source_file target_file

For example:

cp my_file_name.txt my_file2_name.txt

The above Linux command will help in creating a copy of the my_file_name and then renaming the new file to the my_file_name.

By default, the copy command in Linux will get executed in the same directory you are in. however, two files with the same name cannot exist in the same directory. If you want to copy a file that already exists in the target location, you need to change the name of the file to differentiate the new file.

Below are some additional copy command options that you can use with the cp command to perform various operations. 

  • –v  verbose: This option will help in showing the progress of the running command that is copying the files.
  • –p  preserve: It helps in keeping the same attributes of the copied file such as creation date and file permissions.
  • –f  force: It will help in forcing the copy file by deleting the existing file first
  • –i  interactive: This option will provide a prompt for confirmation before moving the file.
  • –R recursive: This option will copy all files and subfolders within a directory
  • –u update: This option will allow to update only if the source is newer than the destination.

If you want to copy a specific file from the current working directory to a different location other than the working directory, you can use the following copy command to do so.

cp my_file_name.txt /new_directory

You can even copy the file from a directory other than the current working directory by specifying the path of the file in the source and specifying the path to the target as shown below.

cp /etc/my_file_name.txt /new_directory

The above command will allow you to copy the file without changing the directory. If the new_directory does not exist then the copy file command will create this directory.

If we want to rename and copy the file to a different path, you can run the following command as shown below.

cp my_file_name.txt /new_directory/my_file_name2.txt

This option is also useful if you want to create backups of configuration files, or for copying data to a storage device.

Using the cp file command, you will also be able to copy multiple files at the same time. You can mention multiple files separated by spaces followed by the path where you want to copy those files.

cp my_file_name.txt my_file2_name.txt my_file3_name.txt /new_directory

The above command will not move the files but create a copy of these files to the destination path. You can even use the wildcard character for copying the files as shown below.

cp /pictures/*.jpg /new_directory

The above command will search for all the files with the .jpg extension and then copy them to the destination path.

If you want to copy the entire folder along with its sub-directory then you can use the “-R” option along with the cp command. Where, “-R” stands for the recursive operation. 

cp –R /documents /new_directory

You can use the rsync command for synchronizing or transferring the data between two different locations. It is somehow similar to the cp Linux command but shows some differences. If you want to copy a single file, then you can run the following command.

rsync –a my_file.txt /new_directory/my_file_backup.txt

Copy command in Linux is another commonly used command that will help in creating a copy of the existing file to another specific location. In the copy command, the files will remain in the source folder along with moving to the destination folder. 

In this article, you will be able to understand the concept of the CP Linux command and how it works differently with the various options.

People Are Also Reading:

Antoniy Yushkevych

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com