How to Use cp, mv, and rm - Linux File Operations Guide

How to Use cp, mv, and rm - Linux File Operations Guide

What you'll be able to do

  • Copy, move, and delete files with cp, mv, and rm
  • Prevent overwrite and deletion accidents with the -i option
  • Explain why rm has no recycle bin

Prerequisites (read these first)

What you will learn

  • How to use cp (copy), mv (move/rename), and rm (delete) safely
  • How the -i option prevents overwrite and deletion accidents
  • Why rm needs extra care — Linux has no recycle bin

Before you start: the rm command is not the same as dragging a file to the Trash. A file removed with rm never lands in the Trash. It is gone at once. That is why we build a practice directoryA container that organizes files. Same idea as a "folder" on Windows or macOS. first. The virtual terminalAn interactive program that reads the commands you type and runs them. on this site is for learning, so your own machine cannot break. Try things freely.

A note on words: > is called a redirect. It sends text that would appear on screen into a file instead. Writing echo "text" > filename creates a file whose contents are that text.

What happens if you get it wrong: > empties the file before it writes. Point it at a file that already has contents and those contents are gone. They do not land in the Trash either.

How to try this safely: in this article we stay inside ~/practice/file-ops and only write to new filenames. We never point > at an existing file.

Introduction

Conclusion: A file overwrite incident kicks off the lesson on safe cp, mv, and rm patterns.

Lina: Linny-senpai, I tried to copy a file and I think I accidentally overwrote something...
Linny-senpai: Oh no, that can be stressful. Linux file operations are convenient, but a small mistake can delete important files.
Lina: That's scary... How can I operate files safely?
Linny-senpai: Don't worry. Once you know the key points, you can use them safely. Today let's learn cp, mv, and rm, together with the habits that prevent accidents.

Setting Up a Practice Environment

Conclusion: Create a practice directory first to avoid touching important files by mistake.

Linny-senpai: First, let's create a practice directory. Practicing in a place of its own keeps your real files out of reach.
$ mkdir -p ~/practice/file-ops
$ cd ~/practice/file-ops
$ pwd
/home/user/practice/file-ops
Lina: What does the -p in mkdir -p mean?
Linny-senpai: It creates the parent directories too, if they are missing. Even with no practice directory yet, it builds practice/file-ops in one step.

cp: Copy Files

Conclusion: cp copies source to destination; -i confirms overwrites; -r copies directories.

Linny-senpai: cp stands for "copy" and is the command to copy files.

Basic Usage

$ echo "Hello Linux" > original.txt
$ cp original.txt copy.txt
$ ls
copy.txt  original.txt
Lina: Oh, copy.txt was created!
Linny-senpai: The order is cp source destination. The original file stays as it was.

Preventing Overwrite Accidents: -i Option

Linny-senpai: Here's the important part. cp will overwrite a file of the same name without asking.
Lina: Oh, so that's why my file...!
Linny-senpai: Exactly. So make the -i option a habit. It stands for "interactive", and it asks before it overwrites.
$ cp -i original.txt copy.txt
cp: overwrite 'copy.txt'?
Lina: Oh, it asked! y to overwrite, n to cancel, right?
Linny-senpai: That's right. Always use cp -i for safety.

Copying Directories: -r Option

Linny-senpai: When copying directories, you need the -r option. It stands for "recursive", and it copies everything inside.
Linny-senpai: Two more from the next example: touch creates an empty file, and echo "text" prints text.
$ mkdir mydir
$ touch mydir/file1.txt mydir/file2.txt
$ cp -r mydir mydir-backup
$ ls mydir-backup
file1.txt  file2.txt

Key Points for cp Command

  • cp source destination - Basic form.
  • cp -i - Confirmation before overwrite. Always use it.
  • cp -r - Copy directories.

mv: Move and Rename Files

Conclusion: mv both moves and renames; -i prevents overwrites; the source file disappears.

Linny-senpai: mv stands for "move". It can be used for both moving and renaming files.

Renaming Files

$ mv copy.txt renamed.txt
$ ls
mydir  mydir-backup  original.txt  renamed.txt
Lina: copy.txt became renamed.txt!
Linny-senpai: If the destination is a filename, mv renames. If it is a directory, mv moves.

Moving Files

$ mv renamed.txt mydir/
$ ls mydir
file1.txt  file2.txt  renamed.txt
Lina: renamed.txt moved into mydir.

Preventing Overwrite Accidents: -i Option

Linny-senpai: Just like cp, mv overwrites a file of the same name. So use the -i option.
$ mv -i original.txt mydir/

Important Note about mv

Since mv moves files, they leave the original location. Before you panic that a file is gone, look in the destination.

Key Points for mv Command

  • mv source destination - Move or rename.
  • mv -i - Confirmation before overwrite. Always use it.
  • A directory destination moves the file. A filename destination renames it.

rm: Delete Files

Conclusion: rm has no recycle bin; always use -i to confirm before deleting any file.

Linny-senpai: rm stands for "remove" and is the command to delete files. This one requires the most caution.
Lina: Why is that?
Linny-senpai: Linux has no Recycle Bin. In a GUI you can pull a file back out of the Trash, but a file removed with rm cannot be recovered.
Lina: Yikes... I'll be careful.

Basic Usage

The example below shows what happens without -i. The file goes with no confirmation. When you type it yourself, use the -i form from the next section.

$ touch delete-me.txt
$ ls
delete-me.txt  mydir  mydir-backup
$ rm delete-me.txt
$ ls
mydir  mydir-backup

Confirmation Before Deletion: -i Option

Linny-senpai: The -i option matters most with rm. It asks before it deletes.
$ touch test.txt
$ rm -i test.txt
rm: remove regular empty file 'test.txt'?
Lina: y to delete, n to cancel, right?
Linny-senpai: Yes. Beginners should always use rm -i.

Deleting Directories: -r Option

$ rm -r mydir-backup
$ ls
mydir
Lina: The directory is gone too.
Linny-senpai: The -r option deletes everything inside, so check the contents with ls before you use it.

Key Points for rm Command

  • rm filename - Delete a file.
  • rm -i - Confirmation before deletion. Always use it.
  • rm -r - Delete a directory with all of its contents.
  • Don't use rm -rf.

Mini Exercises

Conclusion: Practice cp -i, mv -i, and rm -i with mini exercises to build the safe habit.

Linny-senpai: Now let's actually try what we learned today. Think it through before you open the answer.

Exercise 1: Copy a File Safely

Task: Create a file called memo.txt and copy it to memo-backup.txt. Use the overwrite confirmation option.

Show hint 1 (direction)

First make one file with something in it. Then copy it, adding the option that asks you before it overwrites.

Show hint 2 (command names)

Create the file with echo and the symbol that names a destination, >. Copy it with cp. The option that asks first is -i.

Show the answer
$ echo "Important memo" > memo.txt
$ cp -i memo.txt memo-backup.txt
$ ls
memo-backup.txt  memo.txt  mydir

Exercise 2: Move and Rename a File

Task: Move memo.txt into the mydir directory, then rename it to important.txt.

Show hint 1 (direction)

Moving and renaming are the same single command. You can do it in two steps or in one.

Show hint 2 (command names)

The command is mv. If you write a new filename in the destination, it moves and renames at the same time.

Show the answer
$ mv -i memo.txt mydir/
$ mv -i mydir/memo.txt mydir/important.txt
$ ls mydir
file1.txt  file2.txt  important.txt  original.txt  renamed.txt

Or in one command:

$ mv -i memo.txt mydir/important.txt

Exercise 3: Delete a File Safely

Task: Delete memo-backup.txt. Make sure confirmation appears before deletion.

Show hint 1 (direction)

Add the option that asks you before it acts. It is the same letter you used for copying and moving.

Show hint 2 (command names)

The command is rm. Add the -i option.

Show the answer
$ rm -i memo-backup.txt
rm: remove regular file 'memo-backup.txt'? y
$ ls
mydir

Review

Conclusion: Using -i on cp, mv, and rm consistently prevents most accidental file loss.

Lina: I see! If I use the -i option, it asks for confirmation before overwriting or deleting!
Linny-senpai: That's the most important point. Make cp -i, mv -i, and rm -i a habit, and you prevent most accidents.
Lina: And rm needs extra caution because there's no recycle bin, right?
Linny-senpai: Exactly. Checking with ls before you act makes it safer still.

Today's 3-Line Summary

Conclusion: cp, mv, rm, and -i in a 3-line recap — lock in the safe file workflow.

  1. cp -i to copy files, -r to copy directories
  2. mv -i to move or rename files
  3. rm -i to delete (don't use rm -rf)

Next Reading

Conclusion: Next: build on permissions and advanced file ops — chmod, head, tail, and more.

Share this article

Next steps