The cp command copies files and directories in Linux. Unlike copying a single file, directories require the -r flag to recursively copy all contents — without it, the terminal returns an error and refuses to proceed. This guide covers every practical variation of the command.
cp Command Syntax for Copying a Directory in Linux
The basic syntax for using cp to copy a directory on Linux is straightforward:
cp [options] source_directory destination_directory
The source_directory is the folder you want to copy. The destination_directory is where the copy should go. Both accept absolute paths (starting with /) or relative paths from your current location.
If you’re new to navigating directories in the terminal, the Linux command line basics guide covers cd and ls — two commands you’ll use constantly alongside cp.
How to Copy a Directory in Linux with cp -r
To copy a directory and everything inside it — subdirectories, files, nested folders — add the -r or -R flag (both are equivalent). Without it, cp exits with: cp: omitting directory 'dirname'.
cp -r /path/to/source_directory /path/to/destination/
When the Destination Directory Does Not Exist
If the destination path doesn’t exist yet, Linux creates it and copies the entire contents of the source directory into it.
cp -r projects/ backup_projects/
Here, backup_projects/ gets created and the contents of projects/ are placed directly inside it.
When the Destination Directory Already Exists
This is where behavior changes. If backup_projects/ already exists, the source directory itself — not just its contents — gets placed inside the destination.
# Result: backup_projects/projects/ (source becomes a subdirectory)
cp -r projects/ backup_projects/
To copy only the contents and skip the parent folder, add a trailing slash to the source path:
cp -r projects/. backup_projects/
The trailing /. tells cp to copy the directory contents rather than the directory itself.
How to Preserve File Attributes When Copying a Directory
By default, cp assigns the current timestamp and the running user’s ownership to copied files. The original permissions, timestamps, and ownership are not retained unless you ask for it.
The -p flag preserves mode, ownership, and timestamps:
cp -rp /var/www/html/ /backup/html/
For a complete archive copy — including symbolic links, permissions, and all file attributes — use -a (archive), which is shorthand for -dR --preserve=all:
cp -a /var/www/html/ /backup/html/
The -a flag is the safest choice when copying directories for backup. It ensures the destination is an exact replica, which matters when you’re working on a Chromebook and need to run Linux alongside Chrome OS with consistent file states across containers.
Copying a Linux Directory Without Overwriting Existing Files
The cp command overwrites destination files silently by default. Two flags change this behavior.
Use -n to skip files that already exist at the destination:
cp -rn source_dir/ destination_dir/
Use -i (interactive) to get a prompt before each overwrite:
cp -ri source_dir/ destination_dir/
The terminal asks overwrite 'filename'? for each conflict. Press y to overwrite, anything else to skip. This is particularly useful when merging two directories with overlapping file names.
-i and -n are passed together, the last one on the command line takes precedence.
How to Copy Multiple Directories at Once with cp
You can pass multiple source directories before the destination in a single cp command:
cp -r dir1/ dir2/ dir3/ /destination/
Each of the source directories gets copied into /destination/ as separate subdirectories. The destination must already exist when copying multiple sources.
To monitor progress during large copies, the -v flag prints each file name as it’s copied:
cp -rv source_dir/ destination_dir/
This is particularly handy with large directory trees where the operation takes several seconds or longer. If you manage Linux on a Chromebook and want a smoother terminal experience, learning to customize the Linux Terminal app can make commands like these easier to work with.
cp Command Options for Directory Copying on Linux
| Flag | Full Name | What It Does |
|---|---|---|
| -r, -R | –recursive | Copies directories and all their contents recursively |
| -a | –archive | Equivalent to -dR –preserve=all; ideal for backups |
| -p | –preserve | Retains mode, ownership, and timestamps from source |
| -n | –no-clobber | Does not overwrite any existing destination files |
| -i | –interactive | Asks for confirmation before each overwrite |
| -v | –verbose | Prints each file name as it is copied |
| -f | –force | Forces the copy when destination files cannot be opened |
| -u | –update | Copies only when source is newer than destination |
FAQs
How do I copy a directory and all its contents in Linux?
Use cp -r source_dir/ destination_dir/. The -r flag enables recursive copying, which includes all subdirectories and files inside the source directory.
What is the difference between cp -r and cp -a on Linux?
cp -r recursively copies files but does not preserve permissions or timestamps. cp -a does both, making it the right choice for backups where file attributes must be retained.
Why does cp say “omitting directory” on Linux?
The cp command requires the -r or -R flag to copy directories. Without it, the command refuses to process directories and prints the “omitting directory” error.
How do I copy a folder in Linux without overwriting files?
Add the -n flag: cp -rn source/ destination/. This skips any files that already exist at the destination, leaving them untouched.
Can I copy a directory to another location with a different name in Linux?
Yes. Specify the new name in the destination path: cp -r old_name/ /path/to/new_name/. If the destination path doesn’t exist, Linux creates it with that name.
