File Operations
12 min
cp source dest copies a file (add -r to copy a directory and
everything inside it); mv source dest renames or moves a file — there's
no separate "rename" command, moving to a new name in the same directory
IS a rename. rm deletes a file; deleting a directory requires rm -r
explicitly, since removing a whole tree of files is destructive enough
that the shell won't do it by accident. rm -f suppresses errors for
files that don't exist, useful in scripts that shouldn't fail just because
something was already cleaned up.
echo draft content > draft.txt
cp draft.txt backup.txt
mv draft.txt final.txt
rm backup.txt
ls
# final.txt
The first rm on a directory fails on purpose — it's a safety rail. rm -r is the explicit, harder-to-typo-into-by-accident way to confirm you really do want to delete the whole tree.
Create a file `draft.txt` containing the text `hello`, copy it to `draft-backup.txt`, rename `draft.txt` to `final.txt`, and finally delete `draft-backup.txt`.
You run `rm important-dir` on a directory (not a file) without any flags. What happens?
You can copy, move/rename, and delete files and directories, and you understand why deleting a directory needs an explicit flag.