Q1Which is the correct difference between cp and mv?
File Operations — cp / mv / rm
Copy files with cp, move and rename with mv, delete files with rm, and remove directories with rmdir and rm -r — with their main options, hands-on in a browser terminal.
Copy — cp
cp is the command that duplicates a file.
Writing cp source destination keeps the original file and creates the destination as a new copy.
Add -r to copy a directory and all, or -i to avoid overwriting.
| Usage | Meaning |
|---|---|
cp src dst | Duplicate a file (the original stays) |
cp -r src dst | Copy a directory and its contents |
cp -i src dst | Ask before overwriting when the destination already exists |
cp keeps the original and makes a copy. Add -r to copy a directory and its contents.echo 'report' > report.txt # create material
cp report.txt backup.txt # duplicate the file (original stays)
mkdir docs # prepare a directory
cp -r docs docs_copy # copy the whole directory
Move and Rename — mv
mv is the command for both moving and renaming.
mv file directory/ moves, while mv oldname newname renames in place.
Moving onto an existing name overwrites it without asking, so add -i when you want to prevent that.
| Usage | Meaning |
|---|---|
mv file directory/ | Move a file (the original is gone) |
mv oldname newname | Rename in the same place |
mv -i src dst | Ask when it would overwrite |
mv -f src dst | Overwrite forcibly without asking |
mv moves or renames, and the original name doesn't remain. Add -i to confirm before overwriting.echo 'draft' > draft.txt # create material
mv draft.txt final.txt # rename (draft.txt is gone)
echo 'old' > final.txt # prepare an overwrite target
mv -i draft.txt final.txt # exists, so confirm y/n
Delete Files — rm
rm is the command that deletes files.
It doesn't go to a trash — the file is gone on the spot and can't be restored.
Add -i to confirm one by one, or -f to delete without errors or prompts.
| Usage | Meaning |
|---|---|
rm file | Delete a file (can't undo) |
rm -i file | Delete with a confirmation for each one |
rm -f file | Delete forcibly with no prompts or warnings |
rm deletes instantly without a trash. Add -i to confirm before deleting.echo 'log' > old.log # create material
rm old.log # delete the file
echo 'tmp' > tmp.log # create one more
rm -i tmp.log # confirm y/n before deleting
rm can't be undone
A file deleted with rm doesn't go to a trash — it's removed on the spot.
rm -rf in particular deletes a directory and everything inside it with no confirmation, so double-check the target name before running it.
Remove an Empty Directory — rmdir
rmdir deletes only empty directories.
If files or subdirectories remain inside, it isn't deleted and the directory stays.
Add -p to delete a nested path like a/b/c step by step, going up through the empty parents.
| Usage | Meaning |
|---|---|
rmdir directory | Delete an empty directory |
rmdir -p a/b/c | If empty, delete c → b → a, following up through the parents |
rmdir succeeds only when empty. Use -p to remove empty parents too.mkdir empty_box # create an empty directory
rmdir empty_box # empty, so it can be deleted
mkdir -p a/b/c # create an empty nest
rmdir -p a/b/c # delete c → b → a up to the parent
Delete a Whole Directory — rm -r
A directory that contains files or subdirectories can't be removed with rmdir.
rm -r deletes the directory and its contents together.
Adding -f too (rm -rf) removes everything with no confirmation or warning, so it's the option to treat most carefully.
| Usage | Meaning |
|---|---|
rm -r directory | Delete a directory and its contents recursively |
rm -rf directory | Force-delete contents and all with no confirmation (most dangerous) |
rm -r. It can't be undone, so confirm the path before running.Knowledge Check
Answer each question one by one.
Q2Which option do you add to copy a directory and its contents?
Q3What happens when you run rm -r trash?