The Basic Command to Copy a File

To copy a file in Linux, use the cp command followed by the file you want to copy and where you want it to go. The simplest form is cp original.txt copy.txt, which creates a duplicate of original.txt named copy.txt in the same folder.

The cp command works the same way whether you're copying a text file, an image, a document, or any other file type. Linux treats all files the same — the command doesn't care what's inside.

If you want to copy the file to a different folder, include the path: cp original.txt /home/username/Documents/copy.txt. The file ends up in Documents with the new name you gave it.

Key Takeaways

  • The cp command copies a single file with the syntax cp source destination, where destination can be a new filename or a folder path.
  • Use cp -r to copy entire folders and everything inside them, not just single files.
  • Add -i to the command to get a warning before overwriting a file that already exists.
  • The -v flag shows you each file as it copies, which is useful when copying many files at once.

Copying to a Different Folder

When you copy a file to a folder that already exists, you don't need to specify a new filename. The command cp myfile.txt /home/username/Documents/ copies myfile.txt into Documents and keeps the same name.

If the destination folder doesn't exist yet, cp will fail and tell you the folder is not found. You need to create the folder first using mkdir, or include the full path to a folder that does exist.

You can also use a dot (.) to mean "the current folder". The command cp /home/username/file.txt . copies file.txt from anywhere on your system into whatever folder you're currently in.

Copying Entire Folders and Their Contents

To copy a folder and everything inside it, add the -r flag: cp -r original_folder new_folder. The -r stands for "recursive," which means cp goes into the folder, copies all the files, and repeats the process for any subfolders inside.

Without the -r flag, cp refuses to copy a folder and returns an error message saying "omitting directory." This is a safety feature — it prevents you from accidentally trying to copy a folder the wrong way.

If new_folder already exists, cp -r original_folder new_folder places original_folder inside new_folder as a subfolder. If new_folder doesn't exist, cp creates it and puts the contents of original_folder inside.

Protecting Against Accidental Overwrites

If you copy a file to a location where a file with that name already exists, cp silently overwrites the old file without asking. To get a warning first, use the -i flag: cp -i original.txt copy.txt.

When you use -i and the destination file exists, cp stops and asks "overwrite copy.txt?" You can type y to proceed or n to cancel. This gives you a chance to catch mistakes before they happen.

Some Linux systems set -i as the default for the cp command, so you might get the warning automatically. Others don't, so it's worth typing -i explicitly when you're copying important files.

Seeing What cp Is Doing

When you copy many files at once, the command runs silently and you have no way to know if it's still working or if something went wrong. Add the -v flag to see each file as it copies: cp -v file1.txt file2.txt file3.txt /home/username/backup/.

The -v stands for "verbose." Each line of output shows one file being copied, so you can watch the progress and spot any errors that happen along the way.

You can combine flags together: cp -riv original_folder new_folder copies a folder recursively, asks before overwriting, and shows you each file as it goes. The order of the flags doesn't matter.

Copying Files Across Your System

Linux file paths can be absolute (starting from the root of your system) or relative (starting from where you are now). The command cp /etc/hostname /home/username/ uses an absolute path to copy a system file from /etc to your home folder.

Relative paths use shortcuts: ~ means your home folder, and .. means the folder above the current one. The command cp myfile.txt ~/Documents/ copies myfile.txt from wherever you are into your Documents folder.

If you're not sure what folder you're in, type pwd (print working directory) to see the full path. This helps you write the correct cp command when you're using relative paths.

Common Mistakes and How to Fix Them

The most common error is forgetting the destination. The command cp myfile.txt with nothing after it fails because cp doesn't know where to put the copy. You always need both a source and a destination.

Another mistake is trying to copy a folder without -r. If you type cp myfolder newfolder, cp returns "omitting directory" and does nothing. Add -r and it works.

If you get "permission denied," you don't have permission to read the source file or write to the destination folder. Check the folder's permissions with ls -l, or ask your system administrator for access. Copying files owned by other users usually requires sudo, which runs the command with higher privileges.

Frequently Asked Questions

Can I copy a file and keep the original name in a different folder?

Yes. The command cp myfile.txt /home/username/Documents/ copies myfile.txt into Documents with the same name. You only need to specify a new name if you want the copy to have a different name than the original.

What's the difference between cp and mv?

The cp command copies a file, leaving the original in place. The mv command moves a file, which removes it from the original location. Use cp when you want two copies; use mv when you want to relocate or rename something.

Can I copy multiple files at once?

Yes. The command cp file1.txt file2.txt file3.txt /home/username/backup/ copies all three files into the backup folder. You can list as many source files as you need, as long as the last argument is the destination folder.

What happens if I copy a file to a folder with the same name already inside?

The old file gets overwritten without warning (unless you use -i). If you copy file.txt to a folder where file.txt already exists, the new version replaces the old one and the original is lost.

Do I need sudo to copy files?

Only if you're copying files you don't own or copying into a folder you don't have write permission for. Most files in your home folder don't need sudo. If you get "permission denied," try adding sudo at the start: sudo cp original.txt destination.