cp
Quick Reference
Command Name:
cp
Category:
file management
Platform:
Linux/Unix
Basic Usage:
Common Use Cases
Syntax
cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE...
Options
Option | Description |
---|---|
-a, --archive | Same as -dR --preserve=all (preserves structure and attributes) |
-b, --backup | Make a backup of each existing destination file |
--backup=CONTROL | Control backup method (none, numbered, existing, simple) |
-d | Same as --no-dereference --preserve=links |
-f, --force | Remove existing destination files if needed |
-i, --interactive | Prompt before overwrite |
-l, --link | Hard link files instead of copying |
-n, --no-clobber | Do not overwrite an existing file |
-P, --no-dereference | Never follow symbolic links in SOURCE |
-p, --preserve | Preserve mode, ownership, timestamps |
--preserve=ATTR_LIST | Preserve specified attributes (mode, ownership, timestamps, links, context, xattr, all) |
-r, -R, --recursive | Copy directories recursively |
-s, --symbolic-link | Make symbolic links instead of copying |
-S, --suffix=SUFFIX | Override the usual backup suffix |
-t, --target-directory=DIRECTORY | Copy all SOURCE arguments into DIRECTORY |
-u, --update | Copy only when the SOURCE file is newer than the destination file |
-v, --verbose | Explain what is being done |
-x, --one-file-system | Stay on this file system |
Examples
How to Use These Examples
The examples below show common ways to use the cp
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
Basic Examples:
cp file.txt backup.txt
Copy file.txt to backup.txt in the current directory.
cp file.txt /path/to/directory/
Copy file.txt to another directory.
cp -r dir1/ dir2/
Copy directory dir1 and all its contents to dir2.
Advanced Examples:
cp -a source_dir/ destination_dir/
Archive mode: preserve all file attributes and copy recursively.
cp -i file.txt destination/
Interactive mode: prompt before overwriting files.
cp -u *.txt destination/
Update mode: copy only when source is newer than destination.