The cp command in Linux is used to copy files and directories from one location to another. It’s a fundamental command for file management in the terminal.
cp [options] source destination
cp file1.txt file2.txtcp file1.txt /home/user/documents/cp file1.txt file2.txt /backup/cp -r dir1/ dir2/-r : Copy directories recursively-i : Prompt before overwriting files-u : Copy only if the source file is newer-v : Show files as they are copied-p : Preserve file attributes (mode, ownership, timestamps)$ cp -v report.txt backup/
'report.txt' -> 'backup/report.txt'
-i to avoid accidentally overwriting files.cp -r for directories, as plain cp will fail on folders.The cp command in Linux is used to copy files and directories from one location to another. It's one of the fundamental file manipulation commands and supports a wide range of options to handle various file operations efficiently.
cp [options] source destination
Here, source is the file or directory you want to copy, and destination is the target file or directory where the copy will be placed.
cp file1.txt file2.txtcp file1.txt file2.txt backup_folder/cp -r myfolder backup_folder/cp -p file1.txt backup.txtcp -i file1.txt file2.txt-r or --recursive: Copy directories recursively-i: Prompt before overwrite-u: Copy only when the source file is newer than the destination-v: Verbose output showing files being copied-p: Preserve file attributes such as timestamp and permissions-f: Force overwrite if needed# Copying a file to a new directory
cp myfile.txt /home/user/Documents/
# Copy a folder recursively
cp -r project_folder /home/user/Backups/
# Copy with progress and details
cp -v largefile.iso /media/usbdrive/
Wildcards help copy multiple files matching a pattern:
cp *.jpg /home/user/Pictures/
cp file?.txt backup/
Use dotglob in bash or wildcard with dot:
shopt -s dotglob
cp * /destination/folder
Use cp in combination with SSH or other protocols for remote copies. For advanced cases, consider scp or rsync.
The cp command is often used in automation for backups, installations, or configuration setups:
#!/bin/bash
cp -r /var/www/html /var/backups/site_$(date +%F)
Yes, by default. Use -i to prompt before overwriting.
Use -d or --no-dereference.
Use -v for verbose mode or combine with rsync --progress for large files.
The file is copied into that directory with the same filename.
The cp command is a foundational tool in Linux file management. From simple file copies to complex directory trees, its flexibility and options make it essential for both beginners and advanced users. Understanding how to use cp efficiently can significantly enhance your productivity on the command line.
The cp command in Linux is used to copy files and directories from one location to another. It's one of the fundamental file manipulation commands and supports a wide range of options to handle various file operations efficiently.
cp [options] source destination
Here, source is the file or directory you want to copy, and destination is the target file or directory where the copy will be placed.
cp file1.txt file2.txtcp file1.txt file2.txt backup_folder/cp -r myfolder backup_folder/cp -p file1.txt backup.txtcp -i file1.txt file2.txt-r or --recursive: Copy directories recursively-i: Prompt before overwrite-u: Copy only when the source file is newer than the destination-v: Verbose output showing files being copied-p: Preserve file attributes such as timestamp and permissions-f: Force overwrite if needed# Copying a file to a new directory
cp myfile.txt /home/user/Documents/
# Copy a folder recursively
cp -r project_folder /home/user/Backups/
# Copy with progress and details
cp -v largefile.iso /media/usbdrive/
Wildcards help copy multiple files matching a pattern:
cp *.jpg /home/user/Pictures/
cp file?.txt backup/
Use dotglob in bash or wildcard with dot:
shopt -s dotglob
cp * /destination/folder
Use cp in combination with SSH or other protocols for remote copies. For advanced cases, consider scp or rsync.
The cp command is often used in automation for backups, installations, or configuration setups:
#!/bin/bash
cp -r /var/www/html /var/backups/site_$(date +%F)
-i for safety: Prevents accidental overwrites.-u in backups: Only updates outdated files, saving time and resources.find: To selectively copy files matching conditions (e.g., by size, date).cp oldname.txt newname.txtcp file.txt file_$(date +%F).txtfind . -type f -size +10M -exec cp {} /large/ \;Useful for handling large lists of files:
find . -name "*.log" | xargs -I {} cp {} /backup/logs/
sudo or check file permissions.-r if copying folders.| Command | Use Case | Network Support |
|---|---|---|
| cp | Local copy | No |
| scp | Remote copy over SSH | Yes |
| rsync | Efficient syncing and backup | Yes |
Yes, by default. Use -i to prompt before overwriting.
Use -d or --no-dereference.
Use -v for verbose mode or combine with rsync --progress for large files.
The file is copied into that directory with the same filename.
Yes, cp works across mounted filesystems seamlessly.
Use rsync -a or cp -r --parents (if supported).
The cp command in Linux is essential for copying files and directories effectively. Whether you're backing up data, organizing your system, or writing shell scripts, knowing the different options and behaviors of cp will make you a more efficient and confident Linux user. Master this command, and you'll have a core tool for everyday terminal operations.