Copying files is one of the most common tasks when handling files on any operating system. It is even very common to use the desktop shortcut CTRL + c for copying and CTRL + v for pasting. However, in the Linux terminal, it is also a fairly simple task to do and for that, we use the cp command.
The cp command just copies one or more files or folders from one location to another. However, as any Linux command, its initial behavior can be modified thanks to the use of options.
Similarly, the cp command comes installed and ready in all Linux distributions as it is part of the GNU utils. So just open a terminal emulator and start working.
CP Command: basic syntax and options
The cp command is quite simple to use because its function is also simple. Like all commands in the UNIX family, it has a basic syntax for use.
In the case of the cp command, the basic syntax is as follows:
cp [option]... source... directory
Where source refers to the file or folder that will be copied to a destination called directory.
The options and some basic information about the command can be viewed using the following command:
:~$ cp --help
There you will see that there are several different options. The most important ones are the following:
Option | Description according to GNU utils |
-a | Preserve as much as possible of the structure and attributes of the original files in the copy |
-d | Copy symbolic links as symbolic links rather than copying the files that they point to, and preserve hard links between source files in the copies. |
-f or –force | This option forces the copy even if the destination folder is not available for writing. |
-i or –interactive | Displays a message each time a file is to be overwritten. |
-l | Make hard links instead of copies of non-directories. |
-R or -r or –recursive | Copy directories recursively. By default, do not follow symbolic links in the source. |
-s or –symbolic-link | In this case, the command will make symbolic links of all files that are not folders. This replaces a copy. |
-u or –update | This option does not copy files that have the same or newer modification timestamp in the destination folder. It is an update of a copy. |
-v or –verbose | Print the name of each file before copying it. |
Now with some examples, we will demonstrate the common use of the cp command on a Linux system.
Using the cp command
Copy one or more files to a folder
Let’s start with the basics. To copy a file to a certain folder, the command to be run is the following:
:~$ cp [file] [destination]
For example:
:~$ cp file.txt /home/user/Documents/
The above command copies a file called file.txt located at the same location as the prompt in the Documents folder.
You can also copy a file that is at a different location than the prompt, using an absolute path.
:~$ cp /home/user/Pictures/image.jpg /home/user/Documents
So, if you want to copy several files at once, just separate them with a blank space.
:~$ cp file1.txt file2.txt /home/user/Downloads
You can still do it with the absolute routes.
:~$ cp /home/user/file1.txt /home/user/Pictures/file2.jpg /home/user/Downloads
That is how fast you can copy several files.
Copy all files in a directory to a folder
It is also possible to copy all files from one folder to another. To do this, use the asterisk (*) operator. For example:
:~$ cp * [destination]
Or
:~$ cp * /home/users/Documents/folder/
But if you want to copy all the files that have the same extension, you can do it in the following way:
:~$ cp *.txt /home/users/Documents/folder/
That is, all .txt files are copied to the folder.
Copying directories with the cp command
So far we have taught you how to copy files, but what if you wanted to copy a folder? Well, it causes an error.
Suppose you want to copy a folder named “example” to /home/angelo/Documents this would cause a cp omitting directory error
similar to this one:
To copy folders with the cp command, add the -r option. For example:
:~$ cp -r example/ /home/angelo/Documents/
This will copy the entire folder to the destination folder.
Back up the files to be copied
This is a very useful option. When copying files it is common that you will modify it over time, but you can always keep a copy of the original.
To create the backup, just use the following command:
:~$ cp -ab [file] [destination]
Of course, you can specify the file and folder you want. For example:
:~$ cp -ab file2.txt /home/angelo/Documents/example/
In this case, we are using two options simultaneously. The first one is -a. it explicitly treats the file as what it is, a file. And the second one which is -b is to perform the backup.
What the command does, is to create a backup and add a ~ at the end if we make the copy a second time. This will be the original file of the copy.
:~$ ll /home/angelo/Documents/example
Request confirmation before overwriting a file
To avoid possible errors in file writing, you can use the -i option.
With this option, you will be asked to confirm before overwriting files for a copy. To do this, just add the -i option to the command:
:~$ cp -i file2.txt /home/angelo/Documents/example/
Remember, the warning will only come out when it is overwriting. If you copy many files but none are called the same as another in the destination, nothing will be shown.
Avoid overwriting files with the cp command
The cp command has an option that prevents the files in the destination folder from being overwritten during copying.
To do this, simply add the -n option to the cp command:
:~$ cp -n file.txt [destination]
This option is useful if you already know that there are files that are named the same way and you want to avoid overwriting them. On the other hand, it is also useful if you have no idea if similar files exist.
Make copies while maintaining file attributes
On Unix-based systems, files have specific attributes. So, if a user other than the one who created it copies or moves it, there is a risk of modifying the values of these attributes. Some of them are modification date and file permissions.
To avoid this then add the -p option to the cp command.
:~$ cp -p [file] [destination]
That is how simple it is to preserve the attributes during copying.
Updating a backup with the cp command
It is possible to use the cp command to back up our files to another location such as an external memory. Suppose you want to back up a folder with music to another location. To do this you would use the following command:
:~$ cp -r [music_folder] [destination]
But, as it turns out, you are adding new files to that folder and you want to back them up again. You can prevent the cp command from copying back the files you have already backed up and just copy the ones that are not in the destination folder.
Sounds good, doesn’t it? Well, to do that we use the -u or –update option.
:~$ cp -ru [music_folder] [destination]
In short, the command will copy the folder, verify which files from the source folder are in the destination. And finally, it will copy only the ones that are not.
To see the whole process, you can also add the -v option.
:~$ cp -ruv [music_folder] [destination]
So, you can update a backup with a simple command.
Final thoughts
The cp command is an easy to use command but you have seen that you can do many interesting things with it. It is part of GNU utils which makes it available for all Linux distributions.
On the other hand, in this post, you have learned a part of what can be done with the command. This is very useful in server environments but also to go deeper and deeper into the operation of the operating system.