Deleting files & directories: rm

Objectives
  • Demonstrate how to remove files and folders.
  • Describe the difference between the rm and rmdir commands.
  • Describe the danger associated with rm.
Prerequisites

None.

Overview

Use rm to remove / delete files or directories.

Deleting is not safe

Deleting files and directories via the command-line is permanent. There is no undo!

To be precise: 
deleted files might be recoverable by advanced users using specialized skills and software, but generally speaking, once deleted via the command-line, a file or directory is gone forever!

Safe-deleting with trash

Linux users may want to install the excellent trash-cli tool, available for all the major Linux distributions. It introduces the trash command, which moves files to ~/.trash, where you can recover them until you empty that directory.

Deleting files

rm PATH/TO/FILE
 

Deleting a file in the current directory

sarah@laptop ~/documents/$ rm myfile.pdf
sarah@laptop ~/documents$

→ The above command deletes the file ~/documents/myfile.pdf.

Deleting a file in a different directory

sarah@laptop ~/documents/$ rm reports/weather-august.pdf
sarah@laptop ~/documents$

→ The above command deletes the file ~/documents/reports/weather-august.pdf.

Activity

In your home directory:

  • Create a file with touch or nano.
  • Delete that file.
  • List your home directory contents to verify that the file is indeed gone.
Hide hints
show hints

Deleting directories

To delete directories, you must provide the -r (recursive) option, otherwise the command will refuse to remove them.

rm -r PATH/TO/DIR
 

Deleting a directory

sarah@laptop ~/documents/$ rm -r drafts
sarah@laptop ~/documents$

→ The above command deletes the directory ~/documents/drafts and all of its contents (subdirectories and files)

Deleting empty directories

rmdir lets you delete directories as well, but only empty ones, which limits this command's usefulness.

Nevertheless, we're adding it here for completeness.

Deleting an empty directory

sarah@laptop ~/documents$ rmdir drafts
sarah@laptop ~/documents$

→ The above command will delete the directory ~/documents/drafts if it is empty.

Activity

In your home directory:

  • Create a directory.
  • Create a file inside that directory.
  • Attempt to delete that directory with rmdir.
  • Delete that directory with rm.
  • List your home directory contents to verify that the directory is indeed gone.

If you were unable to delete your directory with rm, did you use the correct option?

mkdir mydirectory
touch mydirectory/testfile.txt
rmdir mydirectory
rm -r mydirectory
Hide hints
show hints

Force-deleting

WARNING!

With rm you can use the option -f (--force) to ignore all warnings, even if the file or directory is locked or protected by another application. This is a dangerous operation.

You should be especially careful with the command rm -rf /, which will recursively delete everything in your filesystem, starting at the top, without warning. 

If you run this as a privileged user (root), you will permanently remove your entire filesystem, and will have to reinstall your operating system. Needless to say this is a bad thing.

Summary

  • Use rm [PATH/TO/FILE] to delete a file.
  • Use rm -f [PATH/TO/FILE] to force-delete a file.
  • Use rm -r [PATH/TO/DIRECTORY] to delete a directory.
  • Use rm -rf [PATH/TO/DIRECTORY] to force-delete a directory.