The Basic Command to Copy a Directory
To copy a directory in Linux, use the cp command with the -r flag, which tells cp to copy recursively — meaning it copies the folder itself plus everything inside it. The basic syntax is cp -r source_directory destination_directory.
For example, if you have a folder called my_project in your home directory and want to copy it to your Desktop, you would type cp -r ~/my_project ~/Desktop/my_project. The command copies the entire folder structure, all files, and all subdirectories in one operation.
Without the -r flag, cp will refuse to copy directories and return an error. The -r stands for "recursive" because it descends into each subfolder and copies everything it finds.
Key Takeaways
- Use cp -r source destination to copy a directory and everything inside it to a new location.
- The -i flag prompts you before overwriting an existing directory, which prevents accidental data loss.
- The -v flag shows each file being copied, which is useful when copying large directories to confirm progress.
- Combine flags as cp -riv to copy recursively, prompt before overwriting, and display what is being copied.
- If the destination directory does not exist, cp creates it; if it does exist, cp places the source directory inside it.
Copying to a Location Where the Destination Folder Already Exists
When you copy a directory to a location where a folder with the same name already exists, cp places the source directory inside the existing one rather than replacing it. If you have a folder called my_project and copy it to a location that already contains a my_project folder, the result is a nested structure: my_project/my_project.
To avoid this, either give the copy a different name or remove or rename the existing destination folder first. For example, cp -r my_project my_project_backup creates a copy with a new name instead of nesting it.
Prompting Before Overwriting Files
The -i flag (interactive mode) makes cp ask for confirmation before overwriting any file. This is especially useful when copying directories that might contain files you want to keep. Use it as cp -ri source_directory destination_directory.
When cp encounters a file that already exists at the destination, it stops and asks overwrite [file]? You can then type y to overwrite, n to skip that file, or a to overwrite all remaining files without asking again. This prevents accidentally losing data when the destination already contains files with the same names.
Seeing What Gets Copied With Verbose Output
The -v flag (verbose mode) prints the name of each file and subdirectory as it is copied. This is helpful when copying large directories, because you can see progress in real time and confirm that the operation is actually running. Use it as cp -rv source_directory destination_directory.
Combining -r, -i, and -v together — cp -riv source_directory destination_directory — gives you a safe, visible copy operation that stops before overwriting anything and shows you exactly what is being copied.
Copying Directories With Symbolic Links
By default, cp follows symbolic links (shortcuts to files or folders) and copies the actual content they point to, not the links themselves. If your directory contains symbolic links and you want to preserve them as links rather than copying what they point to, use the -P flag (capital P) alongside -r.
For example, cp -rP source_directory destination_directory copies the directory structure and recreates the symbolic links in the destination, rather than copying the files the links point to. This is useful when you want the copy to have the same structure and link behavior as the original.
Copying Directories Across Different Filesystems
When copying a directory to a different filesystem (such as from your internal drive to an external USB drive), cp works the same way. The command cp -r /home/user/my_project /media/usb_drive/my_project copies the directory to the external drive just as it would to any other location.
If you are copying very large directories or over a slow connection, the -v flag becomes especially useful so you can see that the operation is progressing. Some users prefer rsync for large transfers because it can resume interrupted copies, but cp works fine for most situations.
Common Mistakes and How to Avoid Them
The most common mistake is forgetting the -r flag entirely. Without it, you will see an error like cp: omitting directory. Always include -r when copying folders.
Another mistake is not realizing that if the destination directory already exists, cp will place your source directory inside it. If you want to replace the destination entirely, delete or rename it first. Also, remember that cp does not move the original — it leaves the source directory in place. If you want to move instead of copy, use the mv command.
Frequently Asked Questions
What is the difference between cp -r and cp -R?
Both cp -r and cp -R copy directories recursively. On most Linux systems they behave identically. The -R flag is a capital R, while -r is lowercase. Either one works; lowercase -r is more commonly used and is the standard across different Unix-like systems.
Does cp -r copy file permissions and ownership?
By default, cp copies files with the permissions of the user running the command, not the original file's permissions. To preserve the original permissions and ownership, use cp -rp (the -p flag preserves permissions). If you need to preserve ownership as well, you may need to run the command with sudo, though this depends on your system setup.
Can I copy a directory to itself?
No. If you try to copy a directory to a location inside itself — for example, cp -r my_project my_project/backup — cp will either fail or create an infinite loop. Always copy to a location outside the source directory or give it a completely different name.
How do I copy a directory without copying hidden files?
By default, cp copies hidden files (those starting with a dot, like .config). There is no built-in flag to exclude them. If you need to skip hidden files, use rsync instead with the --no-hidden flag, or manually copy only the visible files you need.
What if the copy operation gets interrupted?
If cp is interrupted (by pressing Ctrl+C or a network disconnect), the partially copied directory remains at the destination. You can delete it and start again, or resume by running the command again with the -i flag to skip files that were already copied. For large operations, rsync is better because it can resume interrupted transfers automatically.