The cp command

copy files and directories 15 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.

Options in the examples

-R, -r, --recursive
copy directories recursively
-i, --interactive
prompt before overwrite (overrides a previous -n option)
-p
same as --preserve=mode,ownership,timestamps
-u
equivalent to --update[=older].
-v, --verbose
explain what is being done

Examples

cp source.txt destination.txt
cp documents/hello.txt hello-backup.txt
cp file.txt documents/
cp file.txt documents/newname.txt

Copy and rename

cp file1.txt file2.txt directory/
cp -r documents/ documents-backup/
cp -i file.txt dest/

Interactive: ask before overwriting

cp -v file.txt dest/

Verbose: show what's being copied

cp -p file.txt dest/

Preserve timestamps and permissions

cp -u file.txt dest/

Only copy if source is newer

cp *.txt documents/

Copy all .txt files

cp file?.txt backup/

? matches any single character

cp -r "$src" "$dest"
cp "$SRC" "$DST"
sudo cp /etc/resolv.conf.bak /etc/resolv.conf

Lessons that use cp