Phase 1Discover the terminal

#4 Copy, move, delete

cp, mv, rm

cp — Copy files

The cp command (short for copy) copies a file to a new destination. The original file remains intact.

Copy a file
$ cp file.txt copy.txt
$ ls
copy.txt  file.txt  notes.txt

To copy an entire folder with all its contents, add the -r option (short for recursive):

Copy a folder
$ cp -r projects projects-backup

mv — Move or rename

mv (short for move) serves two purposes: moving a file to another location, or renaming it. In fact, renaming and moving are the same operation in Linux.

Rename a file
$ mv copy.txt renamed.txt
$ ls
file.txt  renamed.txt  notes.txt
Move into a folder
$ mv renamed.txt Documents/
$ ls Documents
renamed.txt  notes.txt

rm — Delete files

rm (short for remove) permanently deletes a file. Warning: there is no recycle bin in the terminal! A file deleted with rm is gone forever.

Delete a file
$ rm renamed.txt

To delete a folder and all its contents, use the -r option:

Delete a folder
$ rm -r my-folder

rmdir — Delete an empty folder

rmdir only deletes empty folders. It's a safer alternative to rm -r: it refuses to delete a folder that still contains files.

Create then delete an empty folder
$ mkdir empty
$ rmdir empty

Summary

The four commands from this lesson form the file management toolkit:

Summary
cp source destination    # Copy
mv source destination    # Move or rename
rm file                  # Delete a file
rmdir folder             # Delete an empty folder
rm -r folder             # Delete a folder and its contents

Remember: rm is irreversible. When in doubt, check with ls before deleting.

Your turn

Try the commands cp, mv, rm and rmdir in the terminal below. Use ls to verify the result of your actions.

terminal — bash
user@stemlegacy:~$