CIS120 Linux Fundamentals by Scott Shaper

cp, mv and rm Commands

Think of these commands as your file management toolkit in Linux. They're like the basic operations you do with files on your computer - copying, moving, and deleting - but with more power and flexibility.

The examples below use the course setup. Work in ~/playground/chapter2 so you have practice files and directories to use. Run cd ~/playground/chapter2 before trying the commands.

Quick Reference

Command Description Common Use
cp Copy files and directories Make backups, duplicate files
mv Move or rename files Organize files, change names
rm Remove files and directories Delete unwanted files

Why Learn These Commands?

These commands are essential because:

The cp Command (Copy)

The cp command is like making a photocopy of a file. It creates an exact duplicate of your file in a new location.

cp [options] source destination

Common Options for cp

Option What It Does When to Use It
-i Asks before overwriting files When you want to be safe
-r Copies directories and their contents When copying folders
-v Shows what's being copied When you want to see progress
-p Preserves file attributes (permissions, timestamps) When you need exact copies
-a Archive mode (preserves everything, recursive) When making complete backups
-u Updates only newer files When syncing directories
-l Creates hard links instead of copying When you want to save space

Practical Examples

Copying a Single File

From ~/playground/chapter2, make a backup of notes.txt:

cd ~/playground/chapter2
cp notes.txt notes_backup.txt
Copying to a Directory

To copy a file into the documents folder (created by setup):

cd ~/playground/chapter2
cp notes.txt documents/
Copying Multiple Files

You can copy several files at once into a directory:

cd ~/playground/chapter2
cp report1.txt report2.txt documents/
Copying a Directory

To copy an entire folder and its contents, use cp -r. What you get depends on whether the destination already exists:

  • Destination does not exist (e.g. backup): cp -r documents backup creates a new directory named backup and copies the contents of documents into it. You get backup/readme.txt, not backup/documents/readme.txt. The new folder is named after the destination, not the source.
  • Destination exists (e.g. backup/ is already a directory): mkdir backup then cp -r documents backup/ copies the documents folder into backup, so you get backup/documents/ with its contents inside.
cd ~/playground/chapter2
# If you want backup/documents/ (folder and its contents inside backup), create backup first:
mkdir -p backup
cp -r documents backup/

Result: backup/documents/readme.txt (the folder documents is inside backup).

Copying Directory Contents Only

To copy only the contents of a directory (not the directory itself) into another folder:

cd ~/playground/chapter2
mkdir -p backup2
cp -r documents/* backup2/

Result: backup2/readme.txt (the files appear directly in backup2; there is no documents folder inside backup2). The * (asterisk) is a wildcard that means "all files and directories" here. You'll learn more about wildcards below.

Difference in one line: With cp -r documents backup/ (and backup existing), you get backup/documents/. With cp -r documents/* backup2/, you get the same files but directly in backup2/, with no documents subfolder.

The mv Command (Move/Rename)

The mv command does two things: it moves files to new locations and renames them. Think of it like picking up a file and putting it somewhere else.

mv [options] source destination

Common Options for mv

Option What It Does When to Use It
-i Asks before overwriting When you want to be safe
-v Shows what's being moved When you want to see progress
-n Never overwrite existing files When you want to protect existing files
-b Makes backup of existing files When you want to keep old versions
-u Updates only newer files When syncing directories
-f Force move (overwrites without asking) When you're sure about overwriting

Practical Examples

Moving a File

To move a file into the documents folder:

cd ~/playground/chapter2
mv data_old.txt documents/
Renaming a File

To rename a file (moving it to the same location with a new name):

cd ~/playground/chapter2
cp notes.txt oldname.txt
mv oldname.txt newname.txt
Moving a Directory

To move an entire folder (create archive first if you like, then move backup into it):

cd ~/playground/chapter2
mkdir -p archive
mv backup archive/

The rm Command (Remove)

The rm command deletes files and directories. Be careful with this one - deleted files can't be recovered from the trash like in Windows or macOS!

rm [options] file

Common Options for rm

Option What It Does When to Use It
-i Asks before deleting Always use this when learning!
-r Removes directories and their contents When deleting folders
-v Shows what's being deleted When you want to see progress
-f Force delete (no confirmation) Use with extreme caution!
-d Removes empty directories When cleaning up empty folders
-I Asks once before deleting many files When deleting multiple files
--preserve-root Prevents deleting the root directory Safety feature (default in modern systems)

Practical Examples

Deleting a File

To delete a single file (with confirmation). Use a file you created during practice, e.g. notes_backup.txt:

cd ~/playground/chapter2
rm -i notes_backup.txt
Deleting Multiple Files

To delete several files at once:

cd ~/playground/chapter2
rm -i report1.txt report2.txt
Deleting a Directory

To delete an entire folder and its contents (e.g. a backup folder you created):

cd ~/playground/chapter2
rm -ri backup/

Using Wildcards with cp, mv, and rm

Wildcards (also called glob patterns) let you specify many files at once using special characters. The shell expands the pattern into a list of matching file names before the command runs. They work with cp, mv, rm, ls, and many other commands.

Common Wildcard Characters

Character Meaning Example
* Matches any number of characters (including zero) report*.txt matches report1.txt, report2.txt, report_final.txt
? Matches exactly one character report?.txt matches report1.txt, report2.txt but not report10.txt
[ ] Matches one character from a set data_[on]*.txt matches data_old.txt, data_new.txt

Wildcard Examples (using ~/playground/chapter2)

The setup creates report1.txt, report2.txt, report3.txt, data_old.txt, and data_new.txt. From ~/playground/chapter2 you can try:

Copy all report files
cd ~/playground/chapter2
mkdir -p reports_backup
cp report*.txt reports_backup/

report*.txt expands to report1.txt, report2.txt, report3.txt (any filename that starts with "report" and ends with ".txt").

Copy files matching a single character
cd ~/playground/chapter2
cp report?.txt documents/

report?.txt matches report1.txt, report2.txt, report3.txt (one character where ? is). It would not match report10.txt because ? is only one character.

Copy with a character set
cd ~/playground/chapter2
cp data_[on]*.txt documents/

data_[on]*.txt matches data_old.txt and data_new.txt (one character that is either o or n, then any characters, then .txt).

Remove multiple files with a pattern
cd ~/playground/chapter2
rm -i data_*.txt

Always use -i with rm and wildcards so you don't delete more than you intend. The shell will list each matching file and ask for confirmation.

Tip: Use ls with the same pattern first to see what will match before running cp, mv, or rm. For example: ls report*.txt.

Tips for Success

Common Mistakes to Avoid