When you start working with Linux daily, you’ll quickly find yourself moving, copying, and organizing files.
A couple of commands you'll absolutely want to get comfortable with are cp
(copy) and mv
(move). They're simple, powerful, and likely to pop up in almost every Linux project.
👋 Hello again: Just like my previous posts Linux Directories Guide and RHEL 9 VM Setup, I'm learning by doing and sharing the notes with you! Think of this article as both a guide and a cheat sheet you can bookmark.
Let’s dig into it!
📚 Table of Contents
- cp — Copy Files and Directories
- Common cp Options
- mv — Move or Rename Files and Directories
- Common mv Options
- Related Commands to Research
- Key Takeaways
- Related Reading
📋 cp
— Copy Files and Directories
The cp
command copies files and folders from one place to another.
Syntax:
cp [options] source destination
Examples:
- Copy a single file:
cp notes.txt notes_backup.txt
- Copy multiple files into a folder:
cp file1.txt file2.txt /home/user/backup/
- Copy a whole directory (needs
-r
):
cp -r my_folder/ /home/user/backup/
⚙️ Common cp
Options
Option | What it Does |
---|---|
-r or --recursive
|
Copy directories and their contents |
-u |
Only copy if the source file is newer |
-i |
Ask before overwriting files |
-v |
Show progress (verbose) |
-p |
Preserve file attributes like timestamps and permissions |
Example with multiple options:
cp -ruv source_folder/ destination_folder/
🚚 mv
— Move or Rename Files and Directories
The mv
command moves files, folders, and can also rename them!
Syntax:
mv [options] source destination
Examples:
- Move a file to another folder:
mv report.docx /home/user/Documents/
- Rename a file:
mv oldname.txt newname.txt
- Move a whole directory:
mv project/ /home/user/archives/
⚙️ Common mv
Options
Option | What it Does |
---|---|
-i |
Ask before overwriting |
-u |
Only move if the source is newer |
-v |
Show each move step (verbose) |
-n |
Do not overwrite any existing files |
🔗 Related Commands to Research
Sometimes cp
and mv
aren't enough. Here are a few closely related tools:
-
rsync
— Advanced syncing and copying.
rsync -avh source/ destination/
-
install
— Likecp
, but lets you set permissions during the copy.
install -m 755 script.sh /usr/local/bin/
-
rename
— Mass rename files based on a pattern.
rename 's/.txt/.bak/' *.txt
(This renames all .txt
files to .bak
.)
🧠 Key Takeaways
- Use
cp
when you want a copy of a file or folder. - Use
mv
when you want to move or rename something. - Add
-i
or-n
to protect yourself from overwriting files. - For larger or smarter transfers, check out
rsync
. - Always double-check your source and destination paths!
📚 Related Reading
If you enjoyed this, you might also like:
- A Beginner’s Guide to Linux File Hierarchy System (with Windows Comparisons)
- Setting Up a RHEL 9 VM on AWS
Thanks for reading! 🚀
If you're learning Linux too, feel free to share your tips or questions in the comments below. Let's grow together! 🌱
💬 Let's Connect!
LinkedIn