The cp command copies files and folders in Linux

To copy a file in Linux, you use the cp command followed by the name of the file you want to copy and where you want it to go. The basic structure is cp original-file new-file. If you are in the same folder as the file, you can type the command and press Enter. If the file is in a different folder, you need to include the path to that folder.

The cp command works the same way whether you are copying a single file or multiple files at once. You can also copy entire folders by adding a flag that tells Linux to copy everything inside. Most mistakes happen when people forget to include the destination path or accidentally overwrite a file they meant to keep, so this guide walks you through the real steps and shows you how to avoid those problems.

Key Takeaways

  • The basic copy command is cp filename destination, and you press Enter to run it.
  • Use cp -r foldername destination to copy an entire folder and everything inside it.
  • Add the -i flag to make Linux ask for confirmation before overwriting an existing file.
  • If you do not include a full path, Linux assumes you mean the current folder, so check where you are first with the pwd command.
  • The original file stays in place after you copy it — cp does not move or delete anything.

Copy a single file to the same folder

Open your terminal and navigate to the folder that contains the file you want to copy. If you are not sure where you are, type pwd and press Enter — this shows your current location. Then type the copy command with the original filename and the new filename you want to create.

For example, if you have a file called report.txt and you want to copy it as report-backup.txt, type this:

cp report.txt report-backup.txt

Press Enter. Linux creates a new file with the name you specified, and the original file stays where it is. You now have two identical files in the same folder. If you want to see both files, type ls and press Enter to list everything in the folder.

Copy a file to a different folder

To copy a file to another folder, include the path to that folder after the filename. The path can be a full path (starting from the root of your system) or a relative path (starting from where you are now).

If you want to copy report.txt to a folder called backups that is inside your current folder, type:

cp report.txt backups/report.txt

If the backups folder is in your home directory but you are somewhere else, use the full path:

cp report.txt /home/username/backups/report.txt

Replace username with your actual username. You can also use ~ as a shortcut for your home directory, so ~/backups/report.txt works the same way. If you just want to copy the file into the folder without changing its name, you can type the folder path alone:

cp report.txt backups/

Linux keeps the original filename when you do this.

Copy multiple files at once

To copy several files to the same destination, list all the filenames you want to copy, then add the destination folder at the end. For example:

cp file1.txt file2.txt file3.txt backups/

This copies all three files into the backups folder and keeps their original names. You can also use a wildcard to copy files that match a pattern. If you want to copy all files that end in .txt, type:

cp *.txt backups/

The asterisk means "any characters", so *.txt matches report.txt, notes.txt, and any other file ending in .txt. This saves time when you have many files to copy at once.

Copy entire folders with the -r flag

To copy a folder and everything inside it, add the -r flag to the cp command. The r stands for "recursive", which means Linux copies the folder, all the files in it, and all the subfolders inside it.

If you have a folder called project and you want to copy it as project-backup, type:

cp -r project project-backup

This creates a new folder called project-backup with copies of everything that was in project. If you want to copy the folder into a different location, add the destination path:

cp -r project /home/username/backups/

Without the -r flag, Linux will not copy folders — it will show an error message instead. This is a safety feature to prevent accidental mistakes.

Prevent accidental overwrites with the -i flag

If you copy a file to a location where a file with the same name already exists, Linux overwrites the old file without asking. To make Linux ask for confirmation first, add the -i flag:

cp -i report.txt backups/report.txt

If a file called report.txt already exists in the backups folder, Linux will ask: "overwrite 'backups/report.txt'?" Type y for yes or n for no, then press Enter. You can combine flags if you need both — for example, cp -ri copies a folder recursively and asks before overwriting anything.

This flag is especially useful when you are copying files to a folder you have not used in a while and you are not sure what is already there.

Common mistakes and how to fix them

The most common mistake is forgetting to include the destination. If you type cp report.txt without saying where to put it, Linux shows an error because it does not know what you want to do. Always include both the source file and the destination.

Another mistake is trying to copy a folder without the -r flag. Linux will refuse and show a message like "omitting directory 'project'". Add the -r flag and try again.

If you copy a file to a folder that does not exist yet, Linux will not create the folder for you — it will show an error. Create the folder first with the mkdir command, then copy the file. For example, type mkdir backups to create a new folder called backups, then copy your files into it.

If you are not sure whether a file exists in the destination folder, use the -i flag to get a warning before anything is overwritten.

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 removes it from the original location. Use cp when you want to keep the original, and mv when you want to move it somewhere else.

Can I copy a file with a new name to a different folder at the same time?

Yes. Type the full path with the new filename as the destination. For example, cp report.txt /home/username/backups/report-2024.txt copies the file to the backups folder and renames it in one step.

What does the -v flag do?

The -v flag stands for "verbose" and makes Linux show you each file as it copies. This is useful when you are copying many files and want to see progress. You can combine it with other flags: cp -rv project backups/ copies a folder recursively and shows each file as it goes.

How do I copy a file to my current folder with a different name?

Type cp original-filename new-filename without any path. Since you do not include a destination folder, Linux assumes you mean the current folder. For example, cp report.txt report-copy.txt creates a copy in the same folder with a new name.

What happens if I copy a file to a folder that does not have enough space?

Linux will start copying and stop when the disk is full, leaving an incomplete file in the destination folder. Check how much space is available with the df -h command before copying very large files.