Skip to main content

cp (Copy)

Explanation

The cp command is used to copy files and directories from one location to another. It can copy single files, multiple files into a directory, or entire directory trees.

Basic Syntax

cp [OPTION]... SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY

Common Flags and Options

FlagDescription
-r, -R, --recursiveCopy directories recursively.
-i, --interactivePrompt before overwrite.
-v, --verboseExplain what is being done.
-u, --updateCopy only when the SOURCE file is newer than the destination file or when the destination file is missing.
-pPreserve mode, ownership, and timestamps.

Real-world Examples

  1. Copy a file to a new name:

    cp file.txt file_backup.txt
  2. Copy multiple files to a directory:

    cp file1.txt file2.txt /path/to/backup/
  3. Copy a directory recursively:

    cp -r mydir mydir_backup