Theory
The life of a file
A file gets created, looked at, copied, moved or renamed, and eventually deleted. Linux gives you one small command for each of these, and together they are the tools you will use to manage your work on the server every day.
The five are touch, cat, cp, mv, and rm. Most are gentle. One, rm, is genuinely dangerous, because Linux deletes for real, with no recycle bin to rescue you. This lesson introduces the family and flags exactly where to be careful.
At a glance
| Command | What it does |
|---|---|
| touch file | Create an empty file (or update an existing file's timestamp) |
| cat file | Show a file's contents on screen (also joins files together) |
| cp src dst | Copy src to dst (cp -r to copy a whole directory) |
| mv old new | Move a file, or rename it (same command does both) |
| rm file | Delete a file permanently (rm -r for a directory and its contents) |
Practical
A file-management session
$ echo "hello" > a.txt # create a.txt containing 'hello'
$ cat a.txt
hello
$ cp a.txt b.txt # copy: now a.txt AND b.txt exist
$ mv b.txt c.txt # rename b.txt to c.txt
$ ls
a.txt c.txt
$ rm a.txt # delete a.txt (permanently!)
$ ls
c.txtWatch out
rm is permanent, and rm -r is the classic disaster
There is no recycle bin in the Linux shell. When you rm a file, it is gone, not moved to trash, gone. Type carefully.
The famous trap is `rm -r`, which deletes a directory AND everything inside it, recursively. Aim it at the wrong directory and you can erase huge amounts of work in an instant. Two habits help: use `rm -i` to be prompted before each delete, and always double-check the path before you press Enter. Respect rm; it does not ask twice by default.
Quiz
You run: cp a.txt b.txt and then mv b.txt c.txt. Which files exist afterward (assuming a.txt existed to begin with)?
- Only c.txt, because copying and moving delete the originals
- a.txt and c.txt (cp left a.txt in place and made b.txt; mv then renamed b.txt to c.txt)
- a.txt, b.txt, and c.txt, all three
- Only a.txt, because the others were temporary
Show the answer
a.txt and c.txt (cp left a.txt in place and made b.txt; mv then renamed b.txt to c.txt)
Trace it: cp a.txt b.txt COPIES, so a.txt stays and a new b.txt appears (two files). Then mv b.txt c.txt RENAMES b.txt to c.txt, so b.txt becomes c.txt. You are left with a.txt and c.txt. Option A is wrong: cp does not delete the original (that is the whole point of copying), and mv only renamed b.txt, not a.txt. Option C is wrong: b.txt no longer exists after mv renamed it to c.txt. Option D is wrong: nothing here deletes a.txt or c.txt. The key distinction: cp duplicates (original remains), mv relocates or renames (original name is gone).
Think first
Why is 'move' and 'rename' the same command?
mv both moves a file to another directory AND renames it in place. Why is one command doing two jobs? Then tap.
Show the answer
Because to Linux, moving and renaming are the SAME underlying operation: changing a file's path. A file's name is really just its location in the directory tree, so 'give this file a new name' and 'put this file in a new place' are both just 'change where this entry points'. When you run mv old.txt new.txt in the same directory, you are changing the name part of the path, a rename. When you run mv report.txt /home/riya/archive/, you are changing the directory part of the path, a move. Same action, changing the path, so one command, mv, handles both, and it can do them together (mv old.txt archive/new.txt moves AND renames at once). This is more elegant than having two separate commands, and it reflects the underlying truth that a filename is just a label on a location. Note the practical caution: because mv changes the path, it can overwrite a file that already has the destination name, silently, so use mv -i to be prompted if you are unsure. One command, because moving and renaming are one idea.
Summary
Key takeaways
- touch creates an empty file (or updates its timestamp); cat shows a file's contents.
- cp src dst copies a file (original stays); cp -r copies a whole directory.
- mv old new moves OR renames a file, the same command, because both just change the file's path.
- rm deletes a file permanently; rm -r deletes a directory and everything inside it.
- There is no recycle bin: rm is irreversible, and rm -r on the wrong directory is the classic disaster.
- cp and mv can silently overwrite an existing destination; use -i to be prompted, and check paths before Enter.
- Memory hook: touch make, cat show, cp copy, mv move/rename, rm delete-forever.