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
| Flag | Description |
|---|---|
-r, -R, --recursive | Copy directories recursively. |
-i, --interactive | Prompt before overwrite. |
-v, --verbose | Explain what is being done. |
-u, --update | Copy only when the SOURCE file is newer than the destination file or when the destination file is missing. |
-p | Preserve mode, ownership, and timestamps. |
Real-world Examples
-
Copy a file to a new name:
cp file.txt file_backup.txt -
Copy multiple files to a directory:
cp file1.txt file2.txt /path/to/backup/ -
Copy a directory recursively:
cp -r mydir mydir_backup