How to Delete a Directory in Linux

When you purchase through links on my site, I may earn an affiliate commission. Here’s how it works.

    Table of Contents   Show

    So, let's say you've just taken the plunge and you're exploring Linux for the very first time. You're creating files, organizing things, and suddenly you realize you need to delete a folder.

    Only, in Linux, we don't call them folders – we call them directories. Think of them as the same thing: containers that hold your files and other directories. Pretty straightforward, right?

    Now, why would you want to delete a directory? Well, it's the same reason you'd delete a folder on any other OS. Maybe you're doing some digital cleaning and getting rid of old files you don't need anymore. Maybe you finished a project and want to remove the entire directory to free up space. Or maybe you just messed something up and need a fresh start. Whatever the reason, deleting directories is a basic task you'll need to know in Linux.

    Now, here's the thing about Linux: it gives you options. Unlike some operating systems where you just drag a folder to the trash, Linux has different commands for deleting directories depending on whether they're empty or not. This might seem a little extra at first, but trust me, it gives you more control and flexibility in the long run.

    I'll break down the commands, show you some examples, and even give you some pro tips to level up your skills. Let's get into it!

    How to Delete an Empty Directory in Linux

    Okay, so you've got an empty directory just chilling there in your Linux file system, taking up space or maybe just cluttering your view. Time to get rid of it. This is where rmdir comes in. Think of it as the "remove directory" command, but with a catch: it only works on empty directories.

    Now, let's break down the syntax. In Linux, commands are all about efficiency, so it's super simple:

    rmdir directory_name

    That's it! Just type rmdir, followed by a space, and then the exact name of the directory you want to delete.

    Let's say you have an empty directory called "test_dir". To delete it, you'd open your terminal and type:

    rmdir test_dir

    Hit enter, and poof – "test_dir" vanishes. No fuss, no muss.

    But here's where things get interesting. What happens if you try to use rmdir on a directory that's not empty? Let's say you have a directory called "my_project" with some files inside. If you try:

    rmdir my_project

    Linux is going to hit you with an error message that looks something like this:

    rmdir: failed to remove 'my_project': Directory not empty

    Basically, Linux is saying, "Hold up! This directory isn't empty. I'm not going to delete it with rmdir because you might lose important stuff." And that's a good thing! Linux is looking out for you, making sure you don't accidentally delete something you need.

    So, what do you do if you do want to delete a directory with files in it? Well, that's where the rm command comes into play, and we'll talk about that next. But for now, just remember: rmdir is your go-to for quickly and cleanly deleting those empty directories.

    How to Delete an Non-Empty Directory in Linux

    Alright, so what if you need to delete a directory that actually has stuff in it? Files, subdirectories, the whole shebang? That's where rmdir taps out, and we are going for rm instead.

    The key to deleting non-empty directories with rm is the -r option, which stands for "recursive." Think of it like this: rm on its own just wants to delete individual files. But when you add -r, you're basically telling it, "Hey, I want you to go into this directory, delete everything inside, and then delete the directory itself."

    So, the basic syntax is:

    rm -r directory_name

    For example, if you have a directory called "downloads" filled with files you don't need anymore, you'd type:

    rm -r downloads

    And voilà, the entire "downloads" directory, along with all its contents, is gone.

    But hold on a second! This is where I need to give you a serious warning. Unlike rmdir, which only works on empty directories, rm -r is permanent. There's no recycle bin, no "undo" button. Once you hit enter, those files are gone for good.

    Now, to make things even more intense, there's another option you might come across: -f, which stands for "force." When you combine this with -r, you get rm -rf, and this is where things get really dangerous. rm -rf will delete a directory and everything inside it without asking for confirmation. No warnings, no prompts, just straight-up deletion.

    One wrong typo and you could accidentally wipe out critical system files or your entire project. Trust me, you don't want to go there.

    So, what's the safer way to use rm? I highly recommend adding the -i option for "interactive" deletion. This gives you rm -ri, and it's a lifesaver. With this command, Linux will ask you to confirm the deletion of every single file and subdirectory within the target directory. It might seem a bit tedious, but it's way better than accidentally deleting something important.

    Let's recap with a couple of examples:

    • Deleting a Directory with Files

      rm -ri my_documents (this will prompt you for confirmation before deleting each file within "my_documents")

    • Deleting a Directory with Subdirectories

      rm -ri project_x (this will prompt you for confirmation before deleting each file and subdirectory within "project_x")

    Please be careful guys – take your time, double-check your commands, and always opt for the interactive option whenever possible. You'll thank me later.

    What You Should Also Keep in Mind

    Okay, so we've covered the main commands for deleting directories in Linux. But before you go on a deleting spree, there are a few more things you need to keep in mind.

    1. Permissions

    In Linux, everything has an owner and associated permissions. Think of it like a bouncer at a club. If you don't have the right permissions, you're not getting in – or in this case, you're not deleting that directory.

    Usually, you can delete directories you created in your own user space. But if you're trying to delete something in a system folder, you might need root privileges (think of it as the admin password for your Linux system).

    We won't go deep into user permissions and sudo here, but just know that sometimes you might need extra authorization to delete certain directories.

    2. Recovery

    Now, this is a big one. In Linux, when you delete a directory, it's usually gone for good. There's no Recycle Bin like in Windows or Trash like in macOS. Once you hit enter on that rm -r command, those files are pretty much toast.

    There are some advanced recovery tools and techniques, but they're not always reliable, and they definitely aren't as simple as restoring from the Recycle Bin. So, the bottom line is: think twice before you delete anything.

    3. File Manager

    Finally, I know we've been focusing on the command line here, and that might seem intimidating if you're coming from Windows or macOS. But don't worry, Linux also has graphical ways to delete directories.

    Most Linux distributions come with a desktop environment that includes a file manager, like Nautilus (in GNOME) or Dolphin (in KDE). These file managers work a lot like File Explorer in Windows or Finder in macOS. You can browse your files, create folders (directories), and yes, delete them too. Just right-click on the directory you want to delete and select "Move to Trash" or "Delete."

    So, why even bother with the command line then? Well, once you get comfortable with the terminal, you can delete directories much faster, especially if you need to delete multiple directories or use specific criteria.

    Plus, the command line gives you more control and options, like the interactive deletion we talked about earlier.

    Ultimately, whether you use the command line or a GUI is up to you. Both methods get the job done.

    How to Delete a Directory in Linux with Wildcards & Find (Advanced Tips)

    You've got the basics down. You can delete empty directories with rmdir, and you can tackle those packed directories with rm -r (with the -i option for safety, of course!).

    But now, let's take it to the next level. If you really want to become a Linux power user, you should master some advanced techniques for deleting directories: wildcards.

    These are special characters that let you select multiple files or directories at once. Think of them as shortcuts for when you don't want to type out every single directory name. One of the most common wildcards is the asterisk (*), which matches any string of characters.

    For example, let's say you have a bunch of directories named "project_1", "project_2", "project_3", and so on. Instead of deleting them one by one, you can use a wildcard to delete them all at once:

    rm -ri project_*

    This command will delete all directories that start with "project_". Pretty efficient, right?

    But wildcards can be even more powerful when combined with other commands. This is where find comes in. find is a command-line tool that lets you search for files and directories based on various criteria, like name, size, modification date, and more. And you can combine find with rm to delete directories that match specific conditions.

    For example, let's say you want to delete all directories in your "Downloads" folder that are older than 30 days. You could use the following command:

    find Downloads -type d -mtime +30 -exec rm -ri {} \\;

    Let's break this down:

    • find Downloads: This tells find to search within the "Downloads" directory.

    • type d: This specifies that we're looking for directories only.

    • mtime +30: This means we want to find directories that were modified more than 30 days ago.

    • exec rm -ri {} \\;: This tells find to execute the rm -ri command on each directory it finds (the {}represents the found directory).

    Now, I know this command looks a bit complicated, but trust me, it's incredibly powerful. With find, you can create all sorts of complex searches to locate and delete exactly the directories you want.

    Just remember that, as Stan Lee would say, with great power comes great responsibility. Always double-check your find commands before you run them, especially when combined with rm. You don't want to accidentally delete something important!

    Wrap Up

    Alright guys, that pretty much wraps up our deep dive into deleting directories in Linux. We've gone from the basics of rmdir for those empty directories all the way to advanced techniques with wildcards and the find command.

    Hopefully, you're feeling a lot more confident navigating the Linux file system and getting rid of those unwanted directories.

    Let's do a quick recap of the key commands we covered:

    rmdir directory_name

    Your go-to for deleting empty directories. Simple and efficient.

    rm -r directory_name

    This is how you delete directories with files inside. Keep in mind that r stands for "recursive," meaning it deletes everything within the directory.

    rm -ri directory_name

    This is the safe way to use rm -r. The i option makes it interactive, so Linux will ask for confirmation before deleting each file and subdirectory.

    rm -rf directory_name

    The danger zone! This command deletes everything without any warnings. Use with extreme caution, or better yet, avoid it altogether.

    And of course, we can't forget about those wildcards and the find command for those pro-level deletion moves.

    Now, I can't stress this enough: be careful when deleting directories in Linux. Unlike other operating systems, there's no easy "undo" button. Once you delete something, it's usually gone for good. So, take your time, double-check your commands, and always opt for the interactive option whenever possible.

    If you want to dive even deeper into these commands, I highly recommend checking out the man pages. Think of these as the official instruction manuals for Linux commands. You can access them right from your terminal by typing man rm or man rmdir.

    And if you have any questions or want to share your own tips for deleting directories in Linux, drop a comment down below! I always love hearing from you guys.

    Oh, and don't forget to subscribe to my tech newsletter for more awesome tech tips and tutorials delivered straight to your inbox.

    Thank you very much for reading, I’ll hopefully see you around. :)


    FAQ

    • Think of rmdir as the lightweight option. It's specifically designed for deleting empty directories. rm, on the other hand, is more versatile. With the -r option, it can delete directories with files and even other directories inside. Just remember to use rm -ri for that safety net of interactive deletion.

    • Unfortunately, in most cases, no. Linux doesn't have a Recycle Bin or Trash like you might be used to. When you delete a directory, it's usually gone for good.

      There are some advanced recovery tools out there, but they're not always reliable. The best way to avoid this is to be cautious and double-check your commands before you hit enter.

    • This means you don't have the necessary permissions to delete that directory. You might need to use sudo to gain root privileges.

      But be extra careful when using sudo, as it gives you a lot of power to make changes to your system.

    • Absolutely! Most Linux distributions come with a file manager that lets you delete directories graphically.

      Just right-click on the directory and select "Move to Trash" or "Delete." It's a more user-friendly option if you're not comfortable with the command line.

    • Oh man, there are so many! Here are a few of my favorites:

      • ls: Lists the contents of a directory.

      • cd: Changes your current directory.

      • mkdir: Creates a new directory.

      • mv: Moves or renames files and directories.

      • cp: Copies files and directories.



    MOST POPULAR

    LATEST ARTICLES


    Tobias Holm

    Hey everyone, Tobias here, taking you on a unique journey through the tech landscape with a perspective you won't find just anywhere.

    Alongside my tech enthusiasm, I bring insights from my study of psychology and am on the brink of completing my law studies, providing a unique backdrop to how I view technology – not just as a collection of gadgets and software, but as an integral part of our daily lives and various professions.

    My versatility doesn't stop there – as a freelancer in writing, proofreading, and translating, I ensure each blog post is crafted with precision and clarity, making complex topics accessible to everyone.

    Plus, for those of you who love music as much as I do, check out my YouTube channel where I share my journey as a seasoned pianist.

    Thank you so much for reading – enjoy! :)

    https://www.tobiasholm.com
    Previous
    Previous

    How to Find a File in Linux

    Next
    Next

    How to Restart a Google Pixel 8 & Pixel 8 Pro