DEV Community

LaTerral Williams
LaTerral Williams

Posted on • Edited on

📁Mastering cp and mv Commands in Linux (Plus Related cmds)

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

The cp command copies files and folders from one place to another.

Syntax:

cp [options] source destination
Enter fullscreen mode Exit fullscreen mode

Examples:

  • Copy a single file:
cp notes.txt notes_backup.txt
Enter fullscreen mode Exit fullscreen mode
  • Copy multiple files into a folder:
cp file1.txt file2.txt /home/user/backup/
Enter fullscreen mode Exit fullscreen mode
  • Copy a whole directory (needs -r):
cp -r my_folder/ /home/user/backup/
Enter fullscreen mode Exit fullscreen mode

⚙️ 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/
Enter fullscreen mode Exit fullscreen mode

🚚 mv — Move or Rename Files and Directories

The mv command moves files, folders, and can also rename them!

Syntax:

mv [options] source destination
Enter fullscreen mode Exit fullscreen mode

Examples:

  • Move a file to another folder:
mv report.docx /home/user/Documents/
Enter fullscreen mode Exit fullscreen mode
  • Rename a file:
mv oldname.txt newname.txt
Enter fullscreen mode Exit fullscreen mode
  • Move a whole directory:
mv project/ /home/user/archives/
Enter fullscreen mode Exit fullscreen mode

⚙️ 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/
Enter fullscreen mode Exit fullscreen mode
  • install — Like cp, but lets you set permissions during the copy.
install -m 755 script.sh /usr/local/bin/
Enter fullscreen mode Exit fullscreen mode
  • rename — Mass rename files based on a pattern.
rename 's/.txt/.bak/' *.txt
Enter fullscreen mode Exit fullscreen mode

(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:


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

Top comments (1)

Collapse
 
nevodavid profile image
Nevo David

love having little cheat sheets like this tbh, makes my life a lot easier - you think messing up file moves ever really goes away or you just get better at fixing it fast?