Select Page

Organizing More Files – rm, rmdir

  • rm (remove) – Removes files. Can be used to remove directories too
  • rmdir(remove directory) – Especially for removing empty directories

Till now, you’ve created bunch of files and directories to practice various commands in shell. Now, to keep your files system neat and clean, you need to delete unnecessary files and directories. In this tutorial, you’ll learn about various ways of removing files and directories fast and efficiently and a command that you never should run in your terminal.

  • Warning: rm and rmdir completely remove files and directories from file system. There’s no recycle bin for this. So be sure before you remove something!

Removing Files with rm

rm is mainly used for removing files. The command structure is as follows:

$ rm file-path1 file-path2 file-path-n

As you can see, you can remove as many files as you want. If you want to delete every files inside a folder, use wildcard (*).

up1 4

Removing Directories with rm

If you want to remove a directory with rm, you’ll get an error. Create some sub-directories inside MLwiki and some files using mkdir and touch. Learn about them here, mkdir tutorial, touch tutorial. Then try to delete MLwiki entirely.

~$ rm MLwiki
rm: cannot remove 'MLwiki': Is a directory

To remove the entire MLwiki directory, you need to add an option, -r. -r or -R stands for recursive. It recursively deletes each file in each folder and then deletes the empty folder. Now to delete the entire MLwiki folder, run this from home:

~$ rm -r MLwiki
up2 3

In some OS, you may get a prompt asking for your confirmation whether you really want to delete a directory or not. If you enter ‘y’ word, then the corresponding directory will be deleted. It can be painful when deleting a huge folder. But you can add another option with -r, i.e. -f, which stands for force action. So your entire command will look like this:

~$ rm -rf MLwiki

Warning: Never run this command!

rm removes your files. -r helps rm to remove folders recursively. -f helps rm to forcefully remove the folders. Wildcard (*) stands for any one or more characters, i.e. it represents everything. On top of that, rm removes files and directories permanently. So if you run this command from the home directory:

~$ rm -rf *

Then all of your file system will be deleted one by one, recursively. You’ll lose everything in your hard disk. You may see unethical people suggesting you to do this. But it will have catastrophic consequences.

Removing Directories with rmdir

This command is a little bit safe to use. It only removes empty folders. If the folder is not empty, it returns a “Directory not empty” error. You can remove multiple directories at once. Here’s the format:

$ rmdir path1 path2 path-n
up3 5

If you want to see what directories are being deleted, you can pass an option -v or -verbose to the rmdir command like this:

$ rmdir -v path1 path2 path3

If you try to remove multiple directories, but some of them are not empty, then rmdir will return “Directory not empty” error only for those directories. The removing process won’t get hurt for it. All the empty folders will be removed perfectly. Let’s see the ultimate example.

up4 3
Mind Sharing It?
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others