cp(1)
Copy files and directories.
Synopsis
cp [OPTION]... SOURCE... DESTINATIONDescription
Copy SOURCE file(s) to DESTINATION. If multiple sources are given, DESTINATION must be a directory. By default, cp does not copy directories recursively; use -r or -R to do so.
When copying, ownership and permissions may change depending on whether -p is used and the destination location. Symbolic links are followed by default unless -P is specified.
Common options
| Flag | What it does |
|---|---|
-r, -R | copy directories recursively |
-p | preserve mode, ownership, and timestamps |
-a | archive mode; same as -dR --preserve=all |
-f | force overwrite without prompting |
-i | prompt before overwriting existing files |
-v | print names of copied files |
-d | same as --no-dereference --preserve=links |
-P | never follow symbolic links in SOURCE |
--update | copy only when source is newer than destination |
-n | do not overwrite existing files |
Examples
copy a single file in the current directory
cp file.txt file_backup.txtcopy file to a specific directory
cp file.txt /home/user/Documents/copy entire directory and its contents recursively
cp -r /home/user/mydir /home/user/mydir_backupcopy while preserving permissions and timestamps
cp -p important.conf important.conf.bakcopy all .log files verbosely to archive directory
cp -v *.log /var/log/archive/copy with confirmation prompt if destination exists
cp -i file.txt /tmp/file.txtarchive-mode copy preserving all attributes and symlinks
cp -a /home/user/project /backup/projectcopy only if source is newer than destination
cp --update old.txt new.txt