How to Copy a File in Ubuntu Linux Terminal Using the cp Command

In a graphical desktop, copying a file is as simple as right-clicking it, selecting “Copy,” and then pasting it into a new folder. When managing an Ubuntu Linux server or working entirely within the terminal, you must use the cp command (short for “copy”). It is a fast, powerful tool that allows you to duplicate files and directories exactly where you need them.

The Basic Syntax of the cp Command

The cp command requires two pieces of information: the source (what you want to copy) and the destination (where you want the copy to go).

cp [source] [destination]

Copying a File into the Same Directory

Often, you need to make a quick backup of a configuration file before you edit it. You can use cp to duplicate the file in the exact same folder by simply giving the copy a new name.

cp config.txt config_backup.txt

After running this command, you will have two identical files in your current directory: the original config.txt and the new config_backup.txt.

Copying a File to a Different Directory

To copy a file to an entirely new folder, you specify the path of the destination folder. Let’s say you have a file named report.pdf in your current directory, and you want to copy it into your Documents folder.

cp report.pdf /home/username/Documents/

The original file stays where it is, and a perfect copy is placed inside the Documents folder.

Copying and Renaming at the Same Time

You can perform both actions—moving the file to a new folder and giving it a new name—in a single command. You simply specify the new filename at the end of the destination path.

cp report.pdf /home/username/Documents/final_report.pdf

Copying Multiple Files at Once

If you need to copy several files into a folder, you do not need to run the command multiple times. You can list all the source files first, followed by the destination folder at the very end.

cp file1.txt file2.txt file3.txt /home/username/Documents/

You can also use wildcards (like the asterisk *) to copy all files of a specific type. To copy all JPEG images in the current folder to your Pictures folder:

cp *.jpg /home/username/Pictures/

Important Warning: Overwriting Files

By default, the cp command is silent and ruthless. If the destination folder already contains a file with the exact same name as the copy you are making, the cp command will overwrite the existing file immediately without asking for permission. The old data will be permanently destroyed.

To protect yourself, you should use the interactive flag (-i). This forces the system to pause and ask you “yes or no” before it overwrites an existing file.

cp -i report.pdf /home/username/Documents/

If a file named report.pdf already exists there, the terminal will ask: cp: overwrite '/home/username/Documents/report.pdf'?. You must type y (yes) or n (no) to proceed.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.