Learn by reading through in order

File Operations — cp / mv / rm

This article is part of the Linux Course, where you master practical Linux skills from scratch, from basic commands through to shell scripting.
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.

UsageMeaning
cp src dstDuplicate a file (the original stays)
cp -r src dstCopy a directory and its contents
cp -i src dstAsk before overwriting when the destination already exists
How cp works
cp a.txt b.txtBoth a.txt and b.txtremaincp a.txt sub/Copy into sub/(original also stays)cp -r dir copyCopy the whole directory
cp keeps the original and makes a copy. Add -r to copy a directory and its contents.
echo 'report' > report.txt   # create a sample file
cp report.txt backup.txt     # duplicate the file (original stays)
mkdir docs                   # prepare a directory
cp -r docs docs_copy         # copy the whole directory

① Create a sample file with echo 'hello' > a.txt.

② Use cp to duplicate a.txt to b.txt.

③ Make a directory with mkdir box, then use cp -r to copy box to box_copy directory and all.

④ Confirm with ls that both b.txt and box_copy exist. (Run it correctly and an explanation will appear.)

Linux console
0 / 5 completed
Loading Linux Terminal...

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.

UsageMeaning
mv file directory/Move a file (the original is gone)
mv oldname newnameRename in the same place
mv -i src dstAsk when it would overwrite
mv -f src dstOverwrite forcibly without asking
How mv works
mv a.txt sub/a.txt moves into sub/(original is gone)mv a.txt new.txtName changes in place(rename)mv -i a.txt b.txtConfirm y/n beforeoverwriting
mv moves or renames, and the original name doesn't remain. Add -i to confirm before overwriting.
echo 'draft' > draft.txt    # create a sample file
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

① Create a sample file with echo 'data' > old.txt.

② Use mv to rename old.txt to new.txt.

③ Make another file with echo 'memo' > note.txt.

④ Run mv -i note.txt new.txt (since new.txt already exists, an overwrite confirmation appears — type y to proceed).

⑤ Confirm with ls that note.txt is gone and only new.txt remains.

Linux console
0 / 5 completed
Loading Linux Terminal...

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.

UsageMeaning
rm fileDelete a file (can't undo)
rm -i fileDelete with a confirmation for each one
rm -f fileDelete forcibly with no prompts or warnings
How rm works
rm file.txtDeletes the fileon the spotNo trashCan't be restoredrm -i file.txtConfirm y/n beforedeleting
rm deletes instantly without a trash. Add -i to confirm before deleting.
echo 'log' > old.log    # create a sample file
rm old.log              # delete the file
echo 'tmp' > tmp.log    # create one more
rm -i tmp.log           # confirm y/n before deleting

① Create an unneeded file with echo 'temp' > junk.txt.

② Delete junk.txt with rm.

③ Make one more with echo 'temp' > junk2.txt.

④ Run rm -i junk2.txt (a confirmation appears before deleting, so type y).

⑤ Confirm with ls that both are gone.

Linux console
0 / 5 completed
Loading Linux Terminal...

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.

UsageMeaning
rmdir directoryDelete an empty directory
rmdir -p a/b/cIf empty, delete c → b → a, following up through the parents
How rmdir works
mkdir boxAn empty directoryis createdrmdir box (empty)Empty, so it canbe deletedrmdir box (has contents)Not deleted,stays (error)
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

① Create an empty directory with mkdir emptydir, then delete it with rmdir.

② Create an empty nested directory with mkdir -p a/b/c.

③ Use rmdir -p a/b/c to delete the empty parents from c up to a together.

④ Confirm with ls that emptydir and a are gone.

Linux console
0 / 5 completed
Loading Linux Terminal...

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.

UsageMeaning
rm -r directoryDelete a directory and its contents recursively
rm -rf directoryForce-delete contents and all with no confirmation (most dangerous)
How rm -r works
rmdir dir (has contents)Can't delete (error)rm -r dirDelete the directoryand contents entirelyCan't undoDouble-check the pathbefore running
A directory with contents is deleted entirely with rm -r. It can't be undone, so confirm the path before running.

① Create a throwaway nested directory with mkdir -p trash/inner.

② Delete trash directory and all with rm -r.

③ Confirm with ls that trash is gone.

Linux console
0 / 3 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1Which is the correct difference between cp and mv?

Q2Which option do you add to copy a directory and its contents?

Q3What happens when you run rm -r trash?