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.
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.
Similar Readings
- The āpwdā Command in Linux [4 Practical Examples]
- The ācdā Command in Linux [6 Practical Examples]
- The ālsā Command in Linux [7+ Practical Examples]
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.
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.
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.
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.
Similar Readings
- The āmvā Command in Linux [8 Practical Examples]
- The ārmā Command in Linux [7 Practical Examples]
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.
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
- The ātouchā Command in Linux [8 Practical Examples]
- The āmkdirā Command in Linux [6+ Practical Examples]
- The ālocateā Command in Linux [7 Practical Examples]
- The āfindā Command in Linux [10+ Practical Examples]
- The āchmodā Command in Linux [6 Practical Examples]
- The “chown” Command in Linux [8 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners