📝 Command: cp (Copy)
The cp command is used to create a duplicate of a file or a directory. In a graphical interface, this is the equivalent of "Copy" and "Paste."
1. The "Why"
In development and system administration, you rarely want to edit a critical file without having a backup. cp allows you to:
- Create a "safety copy" of a configuration file before you change it.
- Duplicate a project template to start a new app.
- Move files from your downloads to your project folders while keeping the original.
2. Basic Syntax
cp [OPTIONS] SOURCE DESTINATION
- Source: The file or folder you want to copy.
- Destination: Where you want the copy to go (and what you want to name it).
3. Most Useful Flags
| Flag | Name | What it does |
|---|---|---|
-r |
Recursive | Required to copy a directory and everything inside it. |
-i |
Interactive | Warns you before overwriting an existing file at the destination. |
-v |
Verbose | Shows you exactly which files are being copied (useful for large folders). |
-p |
Preserve | Keeps the original file's attributes (date, time, and permissions). |
-u |
Update | Only copies if the source is newer than the destination or if the destination is missing. |
4. Practical Examples
A. Making a Simple Backup
Before editing your .bashrc or a LaTeX file, make a copy:
cp script.sh script.sh.bak
B. Copying a File to Another Folder
To copy a file into a directory while keeping the same name:
cp image.png ~/Pictures/
C. Copying an Entire Folder (The -r Flag)
If you try to copy a folder without -r, it will fail. To duplicate your entire "Preader" project:
cp -r Preader Preader_Backup
D. Copying Multiple Files to One Spot
You can list several files and finish with the destination folder:
cp file1.txt file2.txt file3.txt ~/Desktop/
5. Pro-Tips
- The Dot Shortcut: To copy a file from somewhere else into your current folder, use a dot
.as the destination:cp /etc/fstab . - Be Careful with Overwriting: By default,
cpwill overwrite a file at the destination without asking. If you want to be safe, usecp -i. - Arch Linux Context: When installing a new theme or icon pack, you’ll often use
sudo cp -r [theme_folder] /usr/share/themes/. - Using Wildcards: To copy all Java files to a backup folder:
cp *.java ./backup/
6. Summary Reference
| Goal | Command |
|---|---|
| Copy a file | cp source.txt dest.txt |
| Copy a folder | cp -r folder_source folder_dest |
| Backup with original info | cp -p file.txt file_backup.txt |
| Copy only newer files | cp -u source.txt dest.txt |