How to Remove Directory and It’s Contents in Linux

If you’re working at the Linux command line and you need to remove an empty directory that you don’t use any longer, then all you have to do is type rmdir followed by the name of the directory. For instance, typing rmdir Test will remove the Test directory if it’s empty. It will give you an error message that reads “rmdir: failed to remove ‘Test’: Directory not empty” if there are any files at all in it.

There are several ways you can strip out this entire directory and all its contents with a single Linux remove directory command, but before proceeding keep in mind that you need to be careful. The Linux command line assumes that you know what you’re doing, and you can remove whole directories of stuff you need by mistake if you don’t phrase your commands just right. Since this makes use of the standard rm command, this should work with most other Unix implementations like FreeBSD and macOS.

Method 1: Forcing rm to Remove Populated Directories

We’ll assume that you’ve already opened up a command line interface. You can hold down Ctrl, Alt and T to do so or search for terminal on the Dash. KDE, Cinnamon, LXDE and Xfce4 users can click on the Applications menu and then select Terminal from the System Tools submenu. You could technically be working from a virtual terminal as well.

Before proceeding, keep in mind that the following commands will delete everything in a directory. Let’s assume that you wanted to delete a directory full of files that live inside of the Documents directory inside of your home directory. If you’re used to calling that your Documents folder, then keep in mind that in this case directory and folder mean the same thing. Navigate to your Documents directory by typing cd ~/Documents, or follow cd with wherever the directory you want to remove is. For instance, if you wanted to remove a subdirectory inside of your Downloads directory, then you could use cd ~/Downloads instead.

If you type rmdir Test and push enter, then you’ll receive a message that complains the directory isn’t empty. To remove the directory plus all the files in it, you can type rm -r Test and push enter, but keep in mind that this will remove everything inside of it. You can use ls Test, or ls followed by the name of whatever directory you have, to see whatever files are in there beforehand.

Method 2: Forcing rm to Remove Directories Under All Circumstances

You can force it to remove the directory even if there are read-only files or other problems with the files inside of the directory by typing rm -rf Test, or by replacing Test with whatever directory name you have. This will totally destroy the directory and is similar to the DOS/Windows deltree command. It’s extremely destructive, so use it with care.

You might see people try to get you to run sudo rm -rf / or something like it, but don’t pay them any attention because doing so will remove your entire Linux installation and everything with it! If you’re a regular user of Linux or any other Unix implementation, then you will more than likely come across these sorts of pranks but remember just how dangerous they are.

Method 3: Making rm Prompt You to Remove Files

You should always make sure to use the cd and ls commands to inspect a directory’s contents before removing it, but sometimes it can be a good idea to make the rm command prompt you for each file as well so you know what you’re deleting for sure and don’t remove something you need by accident. Using our test directory from the first method, you could type rm -ri Test and push the enter key to require rm to ask you each time it’s going to remove a file. Type y and push enter or return each time to remove the file. You can also type n instead to keep the file in question.

You may notice that the first question is actually whether or not you should “descend into directory ‘test’?” If you responded with n, then this would keep rm from doing anything else.

Adding the i option can be rather intrusive at times, so some users would instead prefer rm -rI Test as the command. The upper case I option prompts you only when you’re going to delete more than three files, when you start a recursive delete operation or when you’re removing write-protected files. This keeps it much quieter when it comes to the number and types of prompts the command pesters you with.


Kevin Arrows

Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.