Connect with us

Website

How to Remove a Directory in Linux

Published

on

How to Remove a Directory in Linux

Unlike in Windows, where you can use the “rmdir” command to remove a directory, the same Linux command only removes empty directories. It’s true that, by default, the Windows rmdir command also only removes empty directories, but you can use the “/S” flag to force Windows to remove any directory. In Linux, you’ll have to use the “rm” command instead.

To remove a directory in Linux, you need to use “rm” with a special flag, and I’ll also show you how to write the commands in such a way that it doesn’t ask you repeatedly for permission to delete files. But be careful – these commands are like a nuclear option, and there’s no going back if you make a mistake!

Because Linux often doesn’t clean up the files that applications leave behind after you uninstall them, you might find your working space getting cluttered with files and directories you don’t use. Removing them all at once is an efficient way to organize your workspace, and for that purpose, the commands below will help you.

Setting the Directory Structure

For the purposes of this article, I’ve set the directory structure on my Linux machine as below:

Directory Structure in Linux
Directory Structure in Linux

Now let’s see how to remove all of this easily.

Using the “rm” Command with -r and -rf

So I have a directory with files and a subdirectory with more files. If I just use the “rm” command to remove the test_directory, it won’t allow me as shown here:

Advertisement
Not Allowing me to Remove with rm
Not Allowing me to Remove with rm

I get the “Is a directory” error message, showing that, by default, the “rm” command is only used for files.

To remove the entire directory along with the files and subfolders, the command to use is:

rm -r [directoryname]

The -r stands for “recursive”, and the system will go subdirectory by subdirectory, empty out all the files in each, one by one, and then when each folder is empty, it will remove the directory itself, all the way to the top. It’s an easy way to remove an entire directory, root, and branch.

Removing Directories without Confirmation Prompts -rf

If your directory contains write-protected files, the “rm -r” command can be inconvenient. Many programs leave behind write-protected files, and using the above command as-is will generate a prompt that looks like this:

Prompt for Removing Write Protected Files
Prompt for Removing Write Protected Files

Usually, this is a good thing. After all, a file is usually write-protected for a reason. The system is working as intended, if it asks you for a confirmation for removing such a file. The problem, however, is that large directories can often contain thousands of write-protected files, and it’s not feasible to manually confirm the deletion of each one.

To overcome this problem, we use the “-rf” flag. So our command now becomes:

rm -rf [directoryname]

Here’s a screenshot of how it works on a copy of the structure that I created above:

Advertisement
Rm -rf Without Confirmation
Rm -rf Without Confirmation

As you can see, Linux executes the command without a hitch. And we’re done!

No “Recycle Bin” for Deleted Linux Files and Folders

Unlike with Windows, Linux doesn’t have a “oh crap” recovery system. Even in Windows, though, you can delete files permanently by using “Shift+Delete”, but at least there’s a temporary way to get rid of files and recover them in case something goes wrong.

Linux, being primarily a server OS, doesn’t have similar systems to recover deleted files by default. These days, desktop systems have more than enough space, and you rarely run out. However, space is at a premium on a server, and we can’t have deleted files just hanging around wasting it. So by default, deleted files in Linux stay deleted.

There are, however, some packages that help you recover deleted files by scanning the hard drive for files that have been marked for deletion, but whose handles still exist on the file system. One such package is “extundelete”, but it’s far from trivial to use, and the best practice is to unmount the drive on which your deleted data exists to prevent it from being overwritten. But this is only a last resort, it’s a pain to get it to work, and there’s a good chance that it won’t work after all.

Bottom line: Make sure that you’re absolutely sure that you want to remove an entire directory! At least from the command line, there’s no way to do it accidentally.

Using the rmdir Command – But only for Empty Directories

Thus far, I’ve assumed you want to remove non-empty directories in Linux. But if your folder and subfolders are empty, there’s a simple command that does the job – rmdir. The syntax is simple:

Advertisement
rmdir [directory name]

However, if your folder has stuff inside it, the command will return an error as shown here:

Rmdir to Remove Empty Directories
Rmdir to Remove Empty Directories

It says “Failed to remove ‘test_directory’: Directory not empty. And, as mentioned at the beginning, there’s no option or flag to force rmdir to remove non-empty directories in Linux.

Using the “find” Command in Conjunction with rm in Linux

Using rm or rmdir might be fine for individual directories, and most of the time you don’t need anything more. But sometimes, your needs are more complicated. For example, you may want to remove only those directories that meet certain criteria in a certain location. For example, let’s say that your applications have generated a lot of temporary files, all of which are located in “tmp” folders scattered throughout your hard drive. Now you want to get rid of all these tmp directories in one shot without wanting to search for each of them individually inside the subfolders.

Using the regular “rm” command, you can’t do this. But using it in conjunction with “find”, makes the whole process simple.

Finding Directories with “find”

Let’s say I want to find all directories in a certain location that start with the sequence “test”. I can use the “find” command like this:

find /home/bhagwad -type d -name "test*"

With my existing folder structure, this gives me the following output:

Advertisement
Using the find Command in Linux
Using the find Command in Linux

As you can see, the output of the “find” command is a list of entries. I’ve used pattern matching with a wildcard (*) to find all directories and sub-directories that start with “test”. The “-type d” option indicates that I want to restrict my search to directories, and “-name” specifies what I want to find.

Merging the “find” Command with rm

Once you’ve crafted the “find” command that shows you all the directories you’re looking for, run it and make sure that it’s what you want. This way, you get to see that everything’s working fine before you remove them en masse!

To process the list of directories returned by “find” and delete them one by one, we modify the “find” command like this:

find /home/bhagwad -type d -name "test*" -exec rm -rf {} \;

This takes each line returned by “find”, and applies the “rm” command to it with the flags as explained earlier. Running the above command on my test directory structure gives me this:

Using Find in Conjunction with rm
Using Find in Conjunction with rm

You can see that once I run the command, my directory is empty – both while using “ls” and “tree”. As you can imagine this is a very powerful combination, and must be used very carefully! It can save you hours of work, or completely wipe out your server if used incorrectly.

Conclusion

Removing a directory in Linux can be as simple or as complicated as you want. On a basic level, the “rmdir” command exists only to remove empty folders, and you can scale this up to automatically remove thousands of folders matching specific criteria without requiring any confirmation. Use this power wisely!

Advertisement

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.