How to Copy a Folder in Linux: Commands, Options, and When to Use Each
Copying folders in Linux is one of those tasks that looks simple on the surface but opens up into real complexity once you factor in file permissions, hidden files, symbolic links, and remote destinations. Whether you're moving data between drives, backing up a project directory, or duplicating a configuration folder, understanding how the copy works — not just that it works — saves you from unpleasant surprises.
The Core Command: cp and Why -r Matters
The standard tool for copying files and folders in Linux is cp. On its own, cp handles individual files. To copy a directory and everything inside it, you need the -r flag (recursive):
cp -r /source/folder /destination/folder The -r flag tells cp to descend into subdirectories and copy their contents too. Without it, Linux will refuse to copy a directory and return an error.
A key behavioral detail: whether the destination folder already exists changes the outcome.
- If
/destination/folderdoes not exist, Linux creates it and copies the source contents into it. - If
/destination/folderalready exists, Linux copies the entire source folder inside the destination, creating/destination/folder/folder.
This trips up a lot of users. Always check whether your destination path exists before running the command.
Useful Flags to Know
The basic -r flag gets the job done, but several other options change behavior in meaningful ways:
| Flag | What It Does |
|---|---|
-r or -R | Copies directories recursively |
-p | Preserves file permissions, timestamps, and ownership |
-a | Archive mode — combines -r, -p, and copies symbolic links as links |
-v | Verbose output — shows each file as it's copied |
-u | Only copies files that are newer than the destination version |
-n | No-clobber — skips files that already exist in the destination |
-a (archive mode) is worth highlighting. It's the flag most system administrators reach for when they want a copy that behaves like the original — same permissions, same ownership, symlinks intact. A plain cp -r can silently flatten symlinks into actual file copies, which matters if your project depends on them.
cp -a /source/folder /destination/folder 📁 Copying Folders with rsync
For larger folders, folders on remote machines, or situations where you need more control, rsync is often the better tool — even locally.
rsync -av /source/folder/ /destination/folder/ Notice the trailing slash on the source path. With rsync:
/source/folder/— copies the contents of the folder into the destination/source/folder— copies the folder itself into the destination
This is the opposite of where beginners expect the gotcha to be. Trailing slashes matter enormously with rsync.
rsync also supports incremental copying — if the destination already has most of the files, it only transfers what's changed. This makes it significantly faster for large directories or repeated backups.
For copying to a remote server over SSH:
rsync -av /source/folder/ user@remotehost:/destination/folder/ When Permissions and Ownership Matter
On a single-user home machine, copying a folder from one place to another usually works without issues. On shared systems, servers, or when dealing with system directories, file permissions and ownership become critical.
A plain cp -r runs as your current user. Files in the copy will be owned by you — not the original owner. If you're copying configuration files that a service needs to read with specific ownership, that can break things.
cp -a or sudo cp -a preserves original ownership — but only if you have the privileges to do so. Running as a regular user, -a will preserve permissions and timestamps, but ownership may still shift.
If you need to copy system folders or preserve exact ownership across users, you'll likely need sudo:
sudo cp -a /etc/nginx /etc/nginx-backup Hidden Files and Directories
Linux treats files and folders starting with a dot (.) as hidden. The good news: cp -r copies them by default. Unlike some graphical file managers, the command line doesn't skip dotfiles unless you explicitly tell it to.
This matters when copying home directories or project folders that contain configuration files like .env, .git, or .bashrc.
🗂️ What Determines the Right Approach for You
Several variables shape which method fits your situation:
- File volume and size — for thousands of files or gigabytes of data,
rsynchandles interruptions and resumes better thancp - Local vs. remote —
rsyncover SSH is the standard for remote copies;cpis local-only - Need for exact replication — if permissions, symlinks, and ownership must be preserved exactly,
-aflag orrsync -ais the starting point - Frequency — one-off copy vs. repeated sync changes the tool calculus entirely
- User privilege level — copying system directories or preserving root-owned file metadata requires elevated permissions
A developer copying a project folder on their own laptop has different needs than a sysadmin replicating a web server's document root to a backup machine. The commands overlap, but the flags, destination syntax, and error-handling considerations diverge considerably.
Understanding which of those scenarios matches your actual setup — and what your destination environment looks like in terms of existing files, permissions, and remote access — is what determines which combination of flags and tools will produce the result you actually want.