🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

cp Command

Beginner File Management man(1)

Copy files and directories

👁 18 views 📅 Updated: Mar 15, 2026
SYNTAX
cp [OPTION]... SOURCE... DESTINATION

What Does cp Do?

The cp command copies files and directories from one location to another. It is one of the fundamental file management commands in Linux, used daily by system administrators and developers.

cp can copy single files, multiple files, or entire directory trees. It supports preserving file attributes like permissions, timestamps, and ownership during the copy operation. The command can also create hard or symbolic links instead of actual copies.

When copying to an existing file, cp overwrites the destination by default. Use the -i flag for interactive mode to get a confirmation prompt before overwriting.

Options & Flags

OptionDescriptionExample
-r, -R Copy directories recursively (required for directories) cp -r /src /dest
-i Interactive mode - prompt before overwriting cp -i file.txt /backup/
-v Verbose - show files being copied cp -rv dir1/ dir2/
-p Preserve permissions, ownership, and timestamps cp -p config.yml /backup/
-a Archive mode - preserve all attributes and copy recursively cp -a /var/www/ /backup/www/
-u Copy only when source is newer than destination cp -u *.txt /backup/
-n Do not overwrite existing files cp -n file.txt /dest/
-l Create hard links instead of copying cp -l file.txt link.txt
-s Create symbolic links instead of copying cp -s /etc/nginx/nginx.conf nginx.conf

Practical Examples

#1 Copy a single file

Copies report.pdf to the Documents directory.
$ cp report.pdf /home/user/Documents/

#2 Copy and rename a file

Creates a backup copy with a different name in the same directory.
$ cp config.yml config.yml.bak

#3 Copy a directory recursively

Copies the entire directory tree including all subdirectories and files.
$ cp -r /var/www/html /backup/www-backup

#4 Copy preserving all attributes

Archive mode preserves permissions, ownership, timestamps, and symlinks.
$ cp -a /etc/nginx/ /backup/nginx-config/

#5 Copy multiple files to a directory

Copies multiple named files into the /backup/ directory.
$ cp file1.txt file2.txt file3.txt /backup/

#6 Copy with verbose output

Shows each file as it is copied, useful for monitoring large copy operations.
$ cp -rv /project/ /backup/project/
Output: '/project/index.html' -> '/backup/project/index.html' '/project/style.css' -> '/backup/project/style.css' '/project/app.js' -> '/backup/project/app.js'

#7 Copy only newer files

Only copies files that are newer than the destination or don't exist yet.
$ cp -u /source/*.log /archive/

#8 Interactive copy (confirm overwrite)

Prompts for confirmation before overwriting any existing file.
$ cp -i important.conf /etc/
Output: cp: overwrite '/etc/important.conf'? y

Tips & Best Practices

Overwrite danger: cp overwrites destination files without warning by default. Always use -i for important files, or add "alias cp='cp -i'" to .bashrc.
Backup before copy: Use --backup=numbered to automatically create numbered backups: cp --backup=numbered file.txt /dest/
cp vs rsync: For large directory copies or network transfers, rsync is more efficient as it only transfers changed data.

Frequently Asked Questions

How do I copy a directory in Linux?
Use cp -r (recursive) to copy a directory and all its contents: cp -r source_dir/ destination_dir/. Without -r, cp will skip directories.
How do I preserve file permissions when copying?
Use cp -p to preserve mode, ownership, and timestamps, or cp -a for archive mode which also handles symlinks and recursive copying.
What is the difference between cp -r and cp -a?
cp -r copies recursively but may not preserve all attributes. cp -a (archive) is equivalent to -dR --preserve=all — it preserves permissions, timestamps, symlinks, and copies recursively.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →