The three ways to copy a file, and when to use each one

The cp command copies a file from one location to another. The basic shape is cp original-file new-file. If you want to copy a file called budget.txt from your home directory to your Documents folder, you would type cp budget.txt Documents/budget.txt. The original stays where it was; you now have two copies.

Linux also gives you two other ways to move or link files that look similar but work differently. Understanding which one you need prevents you from accidentally overwriting something or creating a file that breaks when you move the original. The choice matters most when you are copying files that other programs depend on, or when you are working with files that contain sensitive information.

Key Takeaways

  • The cp command creates a separate copy of a file, so changes to one do not affect the other.
  • Use cp -r to copy an entire folder and everything inside it, not just a single file.
  • The mv command moves a file instead of copying it, leaving only one version behind.
  • A symbolic link created with ln -s points to the original file rather than copying it, so deleting the link does not delete the original.
  • Copying a file does not erase it from its original location; you must delete it separately if you want it gone.

Copying a single file with cp

Type cp, then a space, then the name of the file you want to copy, then a space, then the name you want the copy to have. If both files are in the same folder, you can use just the filenames: cp report.pdf report-backup.pdf. If the original is in a different folder, include the path: cp Downloads/report.pdf Documents/report.pdf.

The new file is a complete, independent copy. If you edit the copy later, the original does not change. If you delete the copy, the original remains. This is the safest choice when you want to keep both versions and work on one without affecting the other.

If a file with the new name already exists, cp will overwrite it without asking. To be prompted before overwriting, use cp -i instead: cp -i report.pdf report-backup.pdf. The -i flag stands for "interactive" and gives you a yes-or-no choice before the old file disappears.

Copying an entire folder with cp -r

To copy a folder and all the files inside it, use cp -r (the -r stands for "recursive"). For example, cp -r Projects Projects-backup copies the entire Projects folder, including every file and subfolder inside it, and creates a new folder called Projects-backup with everything duplicated.

Without the -r flag, cp will refuse to copy a folder and show you an error message. This is a safety feature — it prevents you from accidentally trying to copy a folder as if it were a single file. If you forget the -r, the terminal will tell you what went wrong, and you can run the command again with the flag added.

Moving a file instead of copying it with mv

The mv command moves a file from one location to another, leaving only one version behind. Type mv original-file new-location/new-name. For example, mv budget.txt Documents/budget.txt moves the file from your home directory into Documents. The original location is now empty; there is only one copy.

You can also use mv to rename a file without moving it: mv old-name.txt new-name.txt. The file stays in the same folder but gets a new name. Like cp, mv will overwrite an existing file without asking unless you use the -i flag: mv -i old-name.txt new-name.txt.

The difference between cp and mv matters when you are working with files that other programs are using. If a program expects a file to be in a specific location and you copy it elsewhere, the program still looks for the original. If you move it, the program will not find it. If you want the program to use the new location, moving is the right choice.

Creating a link instead of copying with ln -s

A symbolic link is a shortcut that points to another file. It looks and acts like the original file, but it is not a copy — it is a pointer. Create one with ln -s original-file link-name. For example, ln -s Documents/budget.txt budget-shortcut creates a link called budget-shortcut that points to the real file in Documents.

When you open the link, you see the contents of the original file. If you edit the file through the link, the original changes. If you delete the link, the original file stays intact. If you delete the original file, the link breaks — it points to something that no longer exists.

Symbolic links are useful when a program expects a file to be in one location but you want to keep it somewhere else. Instead of copying the file (which wastes space and creates a version that does not stay in sync), you create a link in the location the program expects. The program follows the link to the real file, and everything works as if the file were actually there.

Copying files with sensitive information

When you copy a file that contains passwords, financial records, or other sensitive data, the original file remains on your disk even after you delete the copy. Deleting a file in Linux does not erase the data — it only removes the filename from the directory. The data can sometimes be recovered with specialized tools.

If you want to may support a file is truly gone, use shred instead of regular deletion: shred -u sensitive-file.txt. The -u flag overwrites the file with random data before deleting it, making recovery much harder. This takes longer than a normal delete, but it is worth the time for files that contain information you do not want anyone to retrieve.

When you copy a sensitive file, both the original and the copy exist on your disk. If you only need one version, delete the other using shred rather than the normal rm command. This applies to any copy — whether you made it with cp, moved it with mv, or created a link with ln -s.

What happens if something goes wrong

If you copy a file and the command seems to hang or freeze, press Ctrl+C to stop it. The copy may be incomplete, so check whether the new file exists and whether it has the size you expect. You can see the size of a file with ls -lh filename, which shows the file size in human-readable format.

If you get a "permission denied" error, you do not have permission to read the original file or write to the destination folder. Ask the person who owns the file or folder to change the permissions, or use a folder where you have write access. If you get a "no such file or directory" error, you typed the path wrong or the file does not exist where you think it does.

If you accidentally overwrite a file you needed, there is usually no way to undo it from the command line. This is why the -i flag is worth using: cp -i and mv -i ask before overwriting, giving you a chance to stop and check that you are overwriting the right thing.

Frequently Asked Questions

Can I copy a file to a location that does not exist yet?

You can copy a file to a folder that already exists, but not to a folder that does not exist. If you want to copy a file to a new folder, create the folder first with mkdir new-folder, then copy the file into it: cp myfile.txt new-folder/myfile.txt.

What is the difference between cp and cp -r?

cp copies a single file. cp -r copies a folder and everything inside it. If you try to use cp on a folder, you will get an error message telling you to use -r instead.

If I copy a file, does the copy take up extra space on my disk?

Yes. A copy is a complete, separate file that uses its own space. If the original is 10 MB, the copy is also 10 MB, and you now have 20 MB of data on your disk. A symbolic link created with ln -s takes almost no space because it is just a pointer to the original.

Can I copy a file while another program is using it?

Yes, you can copy a file while it is open in another program. The copy will contain whatever data was written to the file at the moment you ran the copy command. If the program is still writing to the file, the copy may not include the most recent changes.

What does the -i flag do?

The -i flag makes cp and mv ask for confirmation before overwriting an existing file. Instead of silently replacing the old file, the terminal shows a prompt asking whether you want to proceed. This prevents accidental data loss.