In this quick guide, we will be showing you how to copy files on the Ubuntu operating system while using the terminal.
Knowing how to copy a file on Ubuntu is a super important skill to learn, especially if you plan on using the terminal.
On the desktop variant of Ubuntu, this is as simple as opening up the file explorer, clicking a file, and pressing CTRL + C to copy and then CTRL + V to paste.
Copying and pasting using the terminal on Ubuntu isn’t any more complicated than on the desktop. In fact, copying is all done with a single simple but powerful command.
This command we are talking about is the cp command. Over the following sections, we will show you how to utilize this command to copy files. You will quickly see that it is easy to remember and easy to use.
As a bonus, if you learn the copy command for one Linux operating system, you can use it on basically any operating system.
Using the Terminal to Copy files on Ubuntu
By the end of the following sections, you should have a good idea of how you can copy files on Ubuntu. We will cover a few different ways to utilize the cp command, from copying a single file to multiple ones.
Syntax of the cp command on Ubuntu
Let us start this guide with a quick look at the syntax of the cp command on the Ubuntu operating system.
cp [OPTIONS] SOURCE... DESTINATION
[OPTIONS]
– The options parameter is entirely optional but allows you to control how this command copies files on your Ubuntu system.
For example, you can get it to create backups of files it overwrites, or you can get it to prompt you when it tries to replace files.SOURCE...
– This command allows you to specify as many files as you want to copy. You can even use wildcards (*
) to select multiple files that start or end with a certain string.DESTINATION
– The copy command has one weak point: it can only copy files to a single destination. This destination must be the path to which you want the files copied.- When copying a single file on Ubuntu, you can end the destination with a filename to rename it while copying.
Copying a Single File on Ubuntu using the Terminal
At its most basic usage, the cp command on Ubuntu allows you to copy a single file to a single destination.
All you need to do to copy a file this way is to specify the “cp
” command, followed by the name of the file or the path to the file, and finally, a path to the destination.
The destination path does not need to contain the file’s name, but it will be renamed during copying using that name if it does.
cp FILE DESTINATION
Example of Copying a Single File
For example, we would use the following command if we wanted to copy a file named “example.txt
” to the “/home/pimyubu/
” directory.
cp example1.txt /home/piymubu/
If we want to copy this file on Ubuntu and change the resulting name, we simply need to specify it at the end of the directory.
For example, if we wanted to copy and rename this file to “helloworld.txt
” we would use the command below.
cp example1.txt /home/pimyubu/helloworld.txt
How to Copy Multiple Files
To copy multiple files at once on Ubuntu, you simply need to specify multiple files when using the cp command.
You can copy as many files as you would like and even use wildcards (*
) to select files starting, and/or ending with specified characters. If you are unsure what we mean, we will give you an example in the next section.
Just note that while you can copy multiple files at once, you cannot have multiple destinations. Additionally, you can’t rename a file when copying multiple files. The destination must be a path.
cp FILE1 FILE2... DESTINATION
Example of Copying Multiple Files on Ubuntu
At its most basic usage, you can specify the name of each file that you want to copy to your new directory.
In the following example, we copy “example1.txt
” and “example2.txt
” to the “/home/pimyubu
” directory.
cp example1.txt example2.txt /home/pimyubu
The cp command on Ubuntu also supports wildcards (*
), which allows you to copy multiple files with a single command.
For example, if we wanted to copy all files that started with “example
” and ended with “.txt
” we would use the following command.
cp example*.txt /home/pimyubu
Ask Before Replacing when Copying a File on Ubuntu
When copying files on Ubuntu, you will notice that it can replace any existing files without warning.
Luckily, this command has a built-in feature that allows it to ask you before overwriting files. You will want to use the “-i
” option to activate the interactive mode.
When using this option, you will have to confirm every time the cp command attempts to overwrite a file.
cp -i FILE... DESTINATION
You can also invoke the interactive option using the long-form version: “--interactive
“.
cp --interactive FILE... DESTINATION
Example of Asking Before Replacing a File on Ubuntu
To showcase what the interactive option does, let us copy a file that we already have saved within our directory.
We will copy the “example1.txt
” file into the “/home/pimyubu
” directory by using following command.
cp -i example1.txt /home/pimyubu
Using the “-i
” option, if the file already exists, you will be prompted with a questions asking if you want to replace the file. To overwrite this file, simply type “y
” and then press the ENTER key.
cp: overwrite './home/pimyubu/example1.txt'? y
Create a Backup When using Ubuntu to Copy Files
On Ubuntu, it is also possible to copy a file and create a backup of any file the tool goes to overwrite. This is useful when you are unsure if the original file is worth keeping or if you want to revert to the original.
To get the cp command to make a backup of any file it replaces, you should use the “-b
” option. Backups of files will end with the tilde symbol (~
).
cp -b FILE... DESTINATION
Like the interactive option, you can also write this as “--backup
“. It achieves the same thing as the “-b
” option.
cp --backup FILE... DESTINATION
Example of Creating a Backup of Files When Copying Them on Ubuntu
To give you an example of how this works. Copy the file “example1.txt
” into the “/home/pimyubu
” directory. This directory is where we copied the text file earlier.
By using the “-b
” option, the cp command will create a backup of every file that already exists on your Ubuntu system.
cp -b example1.txt /home/pimyubu
We can now use the ls command to list the files in our directory.
ls -l /home/pimyubu
Within this directory, we can see the newly copied file (example1.txt
), and a backup of the file that got overwritten (example1.txt~
).
-rw-r--r-- 1 pi pi 0 Jan 15 11:59 example1.txt
-rw-r--r-- 1 pi pi 0 Jan 15 11:41 example1.txt~
Conclusion
Hopefully, at this stage in the guide, you will have a good idea of how to copy files on the Ubuntu system.
The cp command makes copying files on a Linux system a straightforward process.
Please feel free to comment below if you have any issues copying files on your system.
If you found this tutorial to be helpful, we recommend you check out our many other Ubuntu tutorials.