The Basic Command to Copy a Directory

To copy a directory in Linux, use the cp command with the -r flag, which tells the system to copy recursively — meaning it copies the folder and everything inside it. The command looks like this:

cp -r source_directory destination_directory

Replace "source_directory" with the name of the folder you want to copy, and "destination_directory" with where you want it to go. If you want the copy to have a different name, put that new name in the destination spot. For example, cp -r Documents Documents_backup creates a copy of your Documents folder called Documents_backup in the same location.

The -r flag is essential — without it, cp will refuse to copy directories at all. You can also use -R (capital R), which does the same thing.

Key Takeaways

  • Use cp -r source destination to copy a directory and all its contents in one command.
  • The -r flag tells cp to work recursively, copying folders and subfolders together.
  • Add -v to the command to see each file as it copies, which helps you track progress on large directories.
  • Use cp -r source destination/ with a trailing slash on the destination to copy the directory inside an existing folder rather than replacing it.
  • The -p flag preserves file permissions and timestamps, useful when you need the copy to match the original exactly.

Copying to a Different Location

If you want to copy a directory to a different folder, specify the full path. For example, cp -r ~/Documents ~/Backups/Documents copies your Documents folder into the Backups folder. The system creates the copy with the same name unless you give it a new one at the end.

A trailing slash on the destination changes how the copy lands. cp -r Documents Backups/ puts the Documents folder inside Backups. Without the trailing slash, cp -r Documents Backups replaces Backups with a copy of Documents if Backups already exists — or creates Backups as a copy of Documents if it does not. The trailing slash is safer when you want to preserve an existing destination folder.

Watching the Copy Happen with Verbose Mode

When you copy a large directory, the command runs silently and you have no way to know if it is still working or stuck. Add the -v flag to see each file as it copies:

cp -r -v Documents Documents_backup

This prints the name of every file and subfolder as it is copied. On a directory with thousands of files, the output scrolls quickly, but you can see the process is moving. If the output stops for a long time, the copy may have encountered a problem — usually a permission error on a file the system cannot read.

Preserving Permissions and Timestamps

By default, cp copies the files but gives them new timestamps (the date and time the copy was made) and may change permissions if your system has different defaults. If you need the copy to match the original exactly — same permissions, same modification dates — add the -p flag:

cp -r -p Documents Documents_backup

This is especially useful when you are backing up a directory for archival purposes or when you need to preserve the original file metadata for legal or compliance reasons. You can combine -p with -v to both preserve metadata and watch the progress: cp -r -p -v Documents Documents_backup.

Combining Flags for Common Tasks

Most of the time, you will use cp with multiple flags together. Here are the combinations that handle the most common situations:

  • cp -r source destination — basic copy, fastest, good for most backups.
  • cp -r -v source destination — copy with progress visible, useful for large directories.
  • cp -r -p source destination — copy preserving all original metadata, needed for archives.
  • cp -r -p -v source destination — copy with metadata preserved and progress shown, the safest choice when you want to see what is happening.

You can write these flags in any order — cp -rpv is the same as cp -r -p -v. The short form is faster to type once you know what each letter means.

What Happens If the Destination Already Exists

If you try to copy a directory to a location where a directory with the same name already exists, cp behaves differently depending on whether you use a trailing slash. Without a trailing slash, cp -r Documents Documents (copying to the same name in the same place) will fail with an error — the system will not let you overwrite a directory with itself.

If the destination is a different directory that already exists, cp copies your source directory inside it. For example, cp -r Documents Backups when Backups already exists will create Backups/Documents. This is usually what you want, but if you intended to replace Backups entirely, you would need to delete it first with rm -r Backups and then run the cp command.

Handling Permission Errors During Copy

If cp encounters a file it cannot read — usually because you do not have permission to access it — the copy stops and prints an error message. This is a safety feature; the system will not silently skip files. To copy a directory where you own most files but not all, you have two options.

The first is to use sudo to run the command as the system administrator: sudo cp -r -v source destination. This gives cp permission to read everything, but it also means the copied files will be owned by root, which can cause problems later. The second option is to copy what you can and accept that some files will not transfer. Run the command normally, note which files failed, and decide whether you need those files in the copy. In most cases, the files that fail are system files you do not actually need to back up.

Frequently Asked Questions

What is the difference between cp -r and cp -R?

Both copy directories recursively and work identically in most situations. The -R flag is slightly more aggressive — it follows symbolic links (shortcuts to other files) and copies the target instead of the link itself. For everyday copying, either flag works fine. Use -r because it is more common and easier to remember.

Can I copy a directory to a USB drive or external hard drive?

Yes, the command is the same. Plug in the drive, find its mount point (usually something like /media/username/drivename), and use that as your destination: cp -r Documents /media/username/drivename/. Make sure the drive has enough free space first — you can check with the df command.

How do I copy a directory over the network to another computer?

Use scp (find copy) instead of cp. The command is scp -r source username@remote_computer:/destination/path. You need SSH access to the remote computer, and the transfer happens over an encrypted connection. This is slower than local copying but find for sending directories over the internet.

What if I want to copy only certain files from a directory, not the whole thing?

Use cp with a wildcard pattern instead of copying the whole directory. For example, cp -r Documents/*.txt backup/ copies only text files from Documents to backup. You can also use find with cp for more complex filtering, but that requires combining multiple commands.

Does copying a directory take up twice as much disk space?

Yes, temporarily. While the copy is happening, both the original and the copy exist on your drive, so you need enough free space for the entire directory size. Once the copy finishes, you have two identical copies taking up twice the space. If you want to move the directory instead of copying it, use mv, which does not create a duplicate.