Table of Contents
Introduction
To copy a file from one directory to another in Linux, use the cp command.
The cp command really has a lot of parameters which makes it really useful in all situations.
This post will show you how you can use the cp command to copy a file using only the Linux command line.
Furthermore, it will also show how to use the main (and most useful) parameters.
As usual you will find many examples and clear explanations.
How to copy a file in Linux
On a Linux system, the cp command is used to copy a file from one directory to another using the command line.
In this post we will give examples about copying files and directoryies.
If you want to replicate them and don’t know how to create a file/directory from the Linux command line, check out this posts:
In the next sections you will be introduced to the cp
command and the most common and most useful options.
Cp command
The Linux command line cp command allows you to copy a file from a starting directory to a destination directory.
This command also allows you to copy more than one file from the same source directory to a destination one.
The syntax of the cp command is as follows:
cp [OPTIONS]...ORIGIN DESTINATION
Suppose we want to copy the test.txt
file located in our working directory to the /my/tmp/folder/
directory.
You just need to use this command to execute what we described in the previous paragraph:
cp test.txt /my/tmp/folder/
Of course, it is possible to copy a file that is not in the working directory.
To do this, just specify the path to the file you want to copy.
For better understanding, assume that the test.txt
file is located in the amazing/folder/
directory and that we always want to copy it to /my/tmp/folder/.
The command you will need to use is the following:
cp amazing/folder/test.txt /my/tmp/folder/
The cp command also allows you to copy more than one file in the same command, see how in the next section.
Cp command – copy several files
In this section we see how you can copy more than one file using the same command.
It is important to keep in mind that we copy multiple files that are in the same directory and with the same destination.
Also in this case let’s take an example to better understand.
Suppose we want to copy two files, test1.txt
and test2.txt
located in the working directory to /my/tmp/folder/
.
Using this command we achieve our goal:
cp test1.txt test2.txt /my/tmp/folder/
Copy file in Linux doing a backup
In previous chapters we have seen how to copy a file using the Linux cp
command.
One parameter that I think is very useful is --backup
.
This parameter allows you to backup any existing destination files.
Its use is very simple, just add --backup
after the cp command as shown in the example below:
cp --backup original_file destination
After copying the file, another file with the suffix ~
will be created. This is backup.
Let us also take an example in this case.
Suppose we copy test.txt
to my/folder/
with the --backup
parameter.
cp --backup test.txt my/folder/
In my/folder/
we will find two files namely:
- test.txt
- text.txt~
If you don’t know how to view the contents of a directory using the command line check out this post on ls command.
Copy a file in Linux without overwriting existing files
When copying files using the Linux command line it is possible to make mistakes and run the same command several times.
If we talk about copies, this could overwrite a file and cause loss of valuable information.
To avoid this unfortunate error, the cp
command gives us an option called -n
or --no-clobber
.
Its use is very simple, just add it in the command.
Suppose you want to copy test.txt
to my/folder/
without overwriting any files.
This is the command you will need to use:
cp -n test.txt file to my/folder/
Copy file in Linux interactively
To copy a file using the Linux command line there is another option that I think may come in handy.
I’m talking about the -i
or --interactive
option.
Its purpose is to ask you whether or not you want to overwrite a file if it already exists.
Suppose we have already copied the test.txt
file to my/folder/
.
Now let’s run this command:
cp -i test.txt file to my/folder/
By doing so we will be asked if we want to overwrite the file or not.
Just reply with Y to overwrite and n to not overwrite.
IMPORTANT:
Beware that your decision always overrides the -n
parameter (if present) presented in the previous chapter.
How to copy a directory in Linux
The Linux cp
command allows us to copy not only files but also entire directories.
This is done with the -R
/-r
or --recursive
parameter.
The syntax of the command is always the same, but the source and destination are no longer files but directories.
Suppose we want to copy my/folder/
to another/folder/
.
To do this, just use this command:
cp -r my/folder/ another/folder/
Keep in mind that all the contents of the folder will be copied.
Another important thing is that if the destination folder does not exist, it will be created automatically.
Conclusion
Here we are at the end of this post!
We’ve seen how you can use the Linux command line to copy a file to another directory.
We also discovered what are, and how to use, the main parameters that the cp command makes available.
As always, if something is not clear to you, do not hesitate to contact me or write a comment below.
If, on the other hand, everything is clear to you, continue learning by moving on to the next post!