cp – Copy Files and Directories in Linux
The cp command is used to copy files and directories from one location to another.
Syntax
cp [OPTIONS] SOURCE DEST
SOURCE: The file or directory you want to copy.DEST: The destination path where the file or directory will be copied to.
Options
| Option | Description |
|---|---|
-r or -R |
Copy directories recursively (required for folders) |
-v |
Verbose mode (shows what is being copied) |
-i |
Interactive mode (asks before overwriting) |
-u |
Copy only if source is newer than destination or destination is missing |
-f |
Force overwrite without confirmation |
-p |
Preserve file attributes (timestamps, mode, ownership) |
Practical Examples
1. Copy a file to another directory
cp file.txt /home/user/Documents/
2. Copy and rename a file
cp file.txt backup.txt
3. Copy multiple files into a directory
cp file1.txt file2.txt /home/user/backup/
4. Copy a directory and its contents
cp -r my_folder /home/user/Documents/
5. Copy a file with confirmation before overwrite
cp -i file.txt /home/user/backup/
6. Copy with detailed output (what is being copied)
cp -v file.txt /home/user/
Tip
When copying folders, always use the -r option:
cp -r source_folder/ destination_folder/
Without -r, it won’t work for directories.
More info:
man cp