The “cp” Command in Linux [6 Practical Examples]

The cp command in Linux is used for copying files or directories from one location to another. The word cp is used as a short form of Copy. This command is very useful for renaming files or directories. In this article, I will explain in detail how to use the cp command. And I will also show you some practical examples.

A. Description

As the command word cp stands for Copy, it is no brainer that this command will copy files or directories. The cp command will take a source file and copy it to a destination file or directory.

B. Syntax

The syntax for the cp command is very simple. But there are multiple syntaxes for the cp command. One for only copying files, one for copying directories, and one for copying multiple files. All the different syntaxes are given below.

  • The syntax for Copying Files
cp [OPTION]... [-T] SOURCE DEST
  • The syntax for Copying Files to a Directory
cp [OPTION]... SOURCE… DIRECTORY
  • The syntax for Copying Directory
cp [OPTION]... SOURCE DIRECTORY DESTINATION DIRECTORY

Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time. Anything that has 3 dots after it, means it can take multiple inputs. And anything inside the square bracket is not mandatory. Then the DIRECTORY/DESTINATION you want to copy your file or directory to.

C. Options

Many useful options are available for the cp command. I have listed some of the most used ones here. However, if you want to know more about options for the cp command you can always check the man page for the cp command.

man cp

Useful Options

  • -T: No target directory. Treats DEST as a normal File.
  • -u, – -update: Copy only when the SOURCE file is newer than the DEST file.
  • -t: Copy all source arguments into the directory.
  • -s, – -symbolic: Create a symbolic link instead of copying.
  • -R, -r, – -recursive: Copies directory recursively.
  • – -attributes-only: Doesn’t copy the file data attributes only.
  • -i, – -interactive: Prompts before overwriting files at DEST.
  • -l, – -link: Hard links files instead of copying.
  • -L: Follows symbolic links in SOURCE.
NB: The options in Linux CLI (Command Line Interface) are all case-sensitive. So, be cautious while using them. Options starting with (- -) are long forms of options. Long Forms have the same meaning as Short Forms.

Practical Examples of the “cp” Command in Linux

The cp command is very easy to use. It is one of the most basic commands of the Linux CLI. Here I have given some basic examples on the cp command. I have used some Files and Directories that I have created on my machine. You can download these contents if you want to work with the same Files and directories.

Example 1: Copy Files in the Same Directory Using the “cp” Command in Linux

If you want to copy a certain File in your current working Directory, you have to use the cp command. I have a file named File1.txt in my Home directory. I will copy File1.txt to File.txt. Now follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cp File1.txt File2.txt

➌ Now, press the ENTER button.

➍ Type the following command in the command prompt:

cat File1.txt File2.txt

➎ Finally, press the ENTER button.

Output >

In the picture below you can see that there was a file named File1.txt. Then I used the cp command which created a Copy of the existing file with a new name. Then I used the cat command to ensure that the file did indeed get copied.

Copy File using cp command

NB: The ls command is used for viewing the contents of a directory.

Similar Readings


Example 2: Copy Multiple Files in a Directory Using the “cp” Command in Linux

To copy multiple files at the same time you have to use the cp command with both the File names and a destination directory. I will copy both File1.txt and File2.txt inside a directory named Destination, which I have created before. Now follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cp File1.txt File2.txt Destination/

➌ Now, press the ENTER button.

Output >

As you can see there is a directory Destination. Where I copied both File1.txt and file2.txt. And then used cd command to jump into Destination to check my copied files.

copying files into directory using cp command

NB: The ls command is used for viewing the contents of a directory. The cd command is used for Changing Directory.

Example 3: Copying Directory Using the “cp” Command in Linux

When it comes to copying the Directory, the command syntax is different. For copying the Directory, you have to give an Option(-r) to the cp command. Now I will copy the Sports directory to the Destination directory. Now follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cp -r Sports Destination/

➌ Now, press the ENTER button.

Output >

As you can see Sports directory is copied to the Destination directory. Which you can confirm using the ls command.

copying Directory

NB: The ls command is used for viewing the contents of a directory. The cd command is used for Changing Directory.

Example 4: Copy Interactively Using the “cp” Command in Linux

Normally when SOURCE is copied to DESTINATION, it overwrites DESTINATION without asking the user. To prevent that you have to use Option (-i) with the cp command. For this example, I have two files File1.txt, and File2.txt. Now follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cat File1.txt

➌ Now, press the ENTER button.

➍ Type the following command in the command prompt:

cat File2.txt

➎ Now, press the ENTER button.

➏ Type the following command in the command prompt:

cp -i File1.txt File2.txt

➐ Now, press the ENTER button.

➑ Now press the Y button to give permission to overwrite.

➒ Type the following command in the command prompt and press ENTER.

cat File2.txt

Output >

Here first I viewed the File1.txt, and File2.txt contents using the cat command. Then I used the cp command. As you can see it asks for permission to overwrite.

copying file interactively

NB: The ls command is used for viewing the contents of a directory.

Example 5: Copying While Keeping a Backup of Destination File

As the previous example has shown the Destination is always overwritten. To prevent that you need to keep a backup of the Destination file. And for that purpose, I will show you how to use Option (-b) to keep backup files. Now I will use the files that I used in the previous example. So follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cat File1.txt

➌ Now, press the ENTER button.

➍ Type the following command in the command prompt:

cat File2.txt

➎ Now, press the ENTER button.

➏ Type the following command in the command prompt:

cp -b File1.txt File2.txt

➐ Now, press the ENTER button.

➑  Type the following command in the command prompt:

ls

➒ Type the following command in the command prompt and press ENTER.

cat File2.txt~

Output >

As you can see from the picture I used the cp command to copy. But instead of overwriting, I used Option (-b). Which created a backup file File2.txt~. Then I used the cat command to view the backup file.

Copying file without overwriting destination

NB: The ls command is used for viewing the contents of a directory.

Similar Readings


Example 6: Copy Using the Wildcard Character Asterisk(*)

As you know in CLI, the Asterisk(*) means all the values. So if I write F* my terminal will match every value that starts with F and ends with anything else. So if I want to Copy both File1.txt, File2.txt, and anything that starts with F without writing their full name, I will have to use Wildcard characters. Now follow the steps below to know how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cp F* Destination

➌ Now, press the ENTER button.

➍ Type the following command in the command prompt:

cd Destination

➎ Now, press the ENTER button.

Output >

You can see in the below picture that I have used the cp command combined with the Wildcard character to copy multiple files with ease. Then used the cd command to jump into the Destination directory to see if our operation was successful.

Using Wildcard to copy with cp command

NB: The ls command is used for viewing the contents of a directory. cd is used for Changing Directory.

Conclusion

In this article, I have tried to show you how the cp command works. And I have also included some useful examples. If you go through this article properly, you should be able to use the command cp properly on your own.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Md. Rafsan Zani

Hello, I’m Md. Rafsan Zani. I have recently completed my Undergraduate from the Bangladesh University of Engineering and Technology (BUET). Currently, I’m pursuing higher studies abroad. I’m really interested in computer science and would like to learn a lot about the wonderful world of computers. Currently, I’m working as a Linux Content Developer Executive and find Linux really interesting. I certainly would like to learn more about Linux and implement them in my future studies. Read Full Bio

Leave a Comment