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), andrm(delete) safely - How the
-ioption prevents overwrite and deletion accidents - Why
rmneeds 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.
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.
$ mkdir -p ~/practice/file-ops $ cd ~/practice/file-ops $ pwd
/home/user/practice/file-ops
-p in mkdir -p mean?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.
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
copy.txt was created!cp source destination. The original file stays as it was.Preventing Overwrite Accidents: -i Option
cp will overwrite a file of the same name without asking.-i option a habit. It stands for "interactive", and it asks before it overwrites.$ cp -i original.txt copy.txt cp: overwrite 'copy.txt'?
y to overwrite, n to cancel, right?cp -i for safety.Copying Directories: -r Option
-r option. It stands for "recursive", and it copies everything inside.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.
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
copy.txt became renamed.txt!mv renames. If it is a directory, mv moves.Moving Files
$ mv renamed.txt mydir/ $ ls mydir file1.txt file2.txt renamed.txt
renamed.txt moved into mydir.Preventing Overwrite Accidents: -i Option
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.
rm stands for "remove" and is the command to delete files. This one requires the most caution.rm cannot be recovered.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
-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'?
y to delete, n to cancel, right?rm -i.Deleting Directories: -r Option
$ rm -r mydir-backup $ ls mydir
-r option deletes everything inside, so check the contents with ls before you use it.Danger: Don't Use rm -rf
rm -rf is a command that forcibly deletes without confirmation. Beginners should never use it. If you name the wrong directory, all of your important files are gone.
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.
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.
-i option, it asks for confirmation before overwriting or deleting!cp -i, mv -i, and rm -i a habit, and you prevent most accidents.rm needs extra caution because there's no recycle bin, right?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.
cp -ito copy files,-rto copy directoriesmv -ito move or rename filesrm -ito delete (don't userm -rf)