#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.
$ cp file.txt copy.txt
$ ls
copy.txt file.txt notes.txtTo copy an entire folder with all its contents, add the -r option (short for recursive):
$ cp -r projects projects-backupmv — 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.
$ mv copy.txt renamed.txt
$ ls
file.txt renamed.txt notes.txt$ mv renamed.txt Documents/
$ ls Documents
renamed.txt notes.txtrm — 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.
$ rm renamed.txtTo delete a folder and all its contents, use the -r option:
$ rm -r my-folderrmdir — 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.
$ mkdir empty
$ rmdir emptySummary
The four commands from this lesson form the file management toolkit:
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 contentsRemember: 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.