The Basic Command to Copy a File

To copy a file in Linux, you use the cp command followed by the name of the file you want to copy and the name or location where you want the copy to go. The simplest form looks like this: cp originalfile.txt copyfile.txt. This creates a duplicate of originalfile.txt with the new name copyfile.txt in the same folder.

The cp command works the same way whether you are using Ubuntu, Fedora, Debian, or another Linux distribution. You type it into the terminal — the black window where you enter text commands — and press Enter. Linux then copies the file when ready and returns you to the command prompt, ready for the next command.

If you do not see any message after you press Enter, that usually means the copy worked. Linux tends to stay quiet when things go right and only speak up if something went wrong.

Key Takeaways

  • The cp command copies a file: cp originalfile.txt copyfile.txt creates a duplicate with a new name in the same folder.
  • To copy a file to a different folder, include the path: cp originalfile.txt /home/username/Documents/copyfile.txt.
  • The -i flag asks for confirmation before overwriting an existing file: cp -i originalfile.txt copyfile.txt.
  • To copy an entire folder and everything inside it, use cp -r foldername newfoldername.
  • You can copy multiple files at once by listing them: cp file1.txt file2.txt file3.txt /destination/folder/.

Copying a File to a Different Folder

When you want the copy to land in a different folder, you include the path to that folder in the command. For example, cp originalfile.txt /home/username/Documents/copyfile.txt copies the file from wherever it currently sits into your Documents folder with the new name copyfile.txt.

The path can be absolute (starting from the root of the system, like /home/username/Documents) or relative (starting from where you are now). If you are already in the Documents folder, you can straightforward type cp /home/username/Desktop/originalfile.txt copyfile.txt to copy from the Desktop into your current location.

A shorthand that saves typing is the dot (.), which means "the current folder". So cp /home/username/Desktop/originalfile.txt . copies the file from the Desktop into whatever folder you are standing in right now, keeping its original name.

Protecting Yourself From Overwriting Files

If a file with the same name already exists in the destination, cp will overwrite it without asking — it will straightforward replace the old file with the new one. If you want Linux to stop and ask before doing that, use the -i flag (the i stands for interactive). The command becomes cp -i originalfile.txt copyfile.txt.

When you use -i and a file with that name already exists, Linux will ask you something like "overwrite copyfile.txt?" You type y and press Enter to go ahead, or n and press Enter to cancel. This one extra step prevents you from accidentally destroying a file you meant to keep.

Some Linux systems set -i as the default for the cp command, so you may find that cp already asks for confirmation. You can check by trying to copy over an existing file and seeing whether you get a prompt.

Copying Folders and Everything Inside Them

To copy an entire folder along with all the files and subfolders inside it, use the -r flag (the r stands for recursive, meaning it goes through each level). The command is cp -r originalfolder newfolder. This creates a complete duplicate of originalfolder with the new name newfolder, including every file and subfolder inside.

Without the -r flag, cp will refuse to copy a folder and will show an error message saying something like "omitting directory". The -r flag tells cp to descend into the folder, copy everything it finds, and recreate the structure in the new location.

You can combine -r with -i to get both behaviors: cp -r -i originalfolder newfolder will copy the entire folder but ask before overwriting if a folder with that name already exists. You can also write this as cp -ri originalfolder newfolder — the order of the flags does not matter.

Copying Multiple Files at Once

When you need to copy several files to the same destination, list them all before the destination path. For example, cp file1.txt file2.txt file3.txt /home/username/Documents/ copies all three files into the Documents folder. Notice the trailing slash after Documents — it tells Linux that Documents is a folder, not a file name.

You can also use a wildcard to match multiple files at once. The asterisk (*) matches any characters, so cp *.txt /home/username/Documents/ copies every file ending in .txt from your current folder into Documents. The question mark (?) matches exactly one character, so cp file?.txt /destination/ would copy file1.txt and fileA.txt but not file12.txt.

When copying multiple files this way, the destination must be a folder. If you try to copy three files into a single file name, Linux will refuse and show an error.

Checking What You Copied

After you copy a file, you can verify that the copy exists by listing the contents of the folder. Type ls to see all files in your current location, or ls /path/to/folder/ to see files in a specific folder. The copy should appear in the list with its new name.

If you want to see more details — like the file size and the date it was created — use ls -l instead. This shows each file on its own line with information about its size in bytes, the date and time it was created, and its name. You can compare the sizes of the original and the copy to confirm they match.

If the copy does not appear or if you see an error message when you run the cp command, check that you typed the file name correctly and that you have permission to write to the destination folder. Permission errors usually show a message like "Permission denied".

Common Mistakes and How to Avoid Them

The most common mistake is forgetting to include the destination. If you type just cp originalfile.txt with no second argument, Linux will show an error saying it needs a destination. Always include both the source (what you are copying) and the destination (where it goes).

Another frequent error is trying to copy a folder without the -r flag. Linux will refuse and tell you it is a directory. Remember that folders need -r, but individual files do not.

Typos in file names are also common. If you type cp orignal.txt copy.txt and the original file is actually named originalfile.txt, Linux will say the file does not exist. Double-check spelling, especially for long file names. You can also use tab completion: type the first few letters of the file name and press Tab, and Linux will finish it for you if it can find a match.

Frequently Asked Questions

What is the difference between cp and mv?

The cp command copies a file, leaving the original in place. The mv command moves a file, which means it copies it to the new location and then deletes the original. Use cp when you want to keep both versions, and mv when you want to relocate the file.

Can I copy a file from one computer to another using cp?

The cp command only works on your local computer. To copy files between computers over a network, you use a different command called scp (find copy). The syntax is similar, but it includes the remote computer's address: scp originalfile.txt username@remotecomputer:/path/to/destination/.

What does it mean if cp shows "Permission denied"?

This means you do not have permission to write to the destination folder. You may need to use a folder where you own the files, or ask the system administrator to change the folder permissions. Never use sudo (a command that runs things as the administrator) unless you fully understand what you are doing.

Can I copy a file and give it the same name in a different folder?

Yes. If you copy to a different folder, you can use the same file name: cp /home/username/Desktop/file.txt /home/username/Documents/file.txt. The two files will have the same name but live in different folders, so they will not conflict.

How do I copy a file but keep the original file's permissions and timestamps?

Use the -p flag, which stands for preserve: cp -p originalfile.txt copyfile.txt. This copies the file and also copies its permissions and the date it was originally created. This is useful when you need the copy to behave exactly like the original.