Skip to main content

Command Palette

Search for a command to run...

How to Delete a Folder in Linux

Updated
7 min read

Deleting folders in Linux might seem tricky if you're new to the system. But once you understand the basic commands and options, it becomes straightforward. Whether you want to remove an empty folder or a directory full of files, Linux offers flexible ways to do it.

In this article, I’ll guide you through the most common methods to delete folders in Linux. You’ll learn how to use terminal commands safely and also how to delete folders using graphical interfaces. By the end, you’ll feel confident managing your files and folders efficiently.

Understanding Folder Deletion in Linux

Deleting a folder in Linux means removing the directory and its contents from your file system. Unlike some operating systems, Linux treats folders as directories that can contain files or other directories. This means you need to be careful when deleting folders, especially if they contain important data.

There are two main ways to delete folders in Linux:

  • Using the command line (terminal)
  • Using a graphical file manager (GUI)

The command line is powerful and preferred by many Linux users because it offers more control. However, graphical tools are user-friendly and good for beginners.

Why Use the Command Line?

The terminal lets you delete folders quickly and with precision. You can specify options to delete empty folders or folders with files inside. It also allows you to automate tasks using scripts.

When to Use GUI?

If you prefer clicking over typing, the GUI method is easier. Most Linux desktop environments like GNOME, KDE, or XFCE have file managers that let you delete folders with a right-click.

How to Delete an Empty Folder in Linux

If your folder is empty, deleting it is simple. The command you use is rmdir, which stands for "remove directory."

Using the rmdir Command

Open your terminal and type:

rmdir foldername

Replace foldername with the name of the folder you want to delete. This command only works if the folder is empty. If the folder contains files or other folders, you will get an error.

Example

If you have a folder named testfolder that is empty, run:

rmdir testfolder

If successful, the folder will be deleted without any message.

Tips for Using rmdir

  • You can delete multiple empty folders at once:

    rmdir folder1 folder2 folder3
    
  • To delete nested empty folders, use the -p option:

    rmdir -p parentfolder/childfolder
    

    This deletes childfolder and then parentfolder if both are empty.

How to Delete a Folder with Contents in Linux

Most folders contain files or other directories. To delete these, you need a command that can remove everything inside the folder.

Using the rm Command with -r Option

The rm command removes files, and with the -r (recursive) option, it deletes directories and their contents.

rm -r foldername

This command deletes the folder and everything inside it.

Adding the -f Option for Force Deletion

Sometimes, you might encounter permission issues or prompts asking for confirmation. Adding the -f (force) option forces deletion without prompts:

rm -rf foldername

This is a powerful command and should be used carefully because it deletes everything without asking.

Example

To delete a folder named myfolder with all its contents:

rm -rf myfolder

Safety Tips When Using rm -rf

  • Double-check the folder name before running the command.
  • Avoid running rm -rf / or similar commands that can delete system files.
  • Use tab completion to avoid typos.
  • Consider backing up important data before deletion.

Deleting Folders Using Graphical File Managers

If you prefer not to use the terminal, you can delete folders using your Linux desktop’s file manager.

Steps to Delete a Folder in GUI

  1. Open your file manager (Nautilus, Dolphin, Thunar, etc.).
  2. Navigate to the folder you want to delete.
  3. Right-click the folder.
  4. Select “Delete” or “Move to Trash.”
  5. Confirm the deletion if prompted.

Difference Between “Delete” and “Move to Trash”

  • Delete: Permanently removes the folder immediately.
  • Move to Trash: Sends the folder to the Trash or Recycle Bin, allowing recovery later.

Most file managers use the Trash system by default, which is safer.

Emptying the Trash

To permanently remove deleted folders from Trash:

  • Open the Trash folder.
  • Click “Empty Trash” or “Delete Permanently.”

Deleting Folders with Special Permissions

Sometimes, you might not have permission to delete a folder. This happens if the folder is owned by another user or requires administrative rights.

Using sudo to Delete Protected Folders

If you get a “Permission denied” error, try running the command with sudo:

sudo rm -rf foldername

You will be prompted to enter your password. This runs the command with root privileges.

Checking Folder Permissions

To see who owns a folder and its permissions, use:

ls -ld foldername

This shows the owner, group, and permission settings.

Changing Permissions or Ownership

If needed, you can change permissions or ownership before deleting:

  • Change ownership:

    sudo chown yourusername foldername
    
  • Change permissions:

    sudo chmod u+w foldername
    

Then try deleting again.

Using Wildcards to Delete Multiple Folders

If you want to delete multiple folders matching a pattern, you can use wildcards.

Example: Delete All Folders Starting with “temp”

rm -rf temp*

This deletes all folders and files starting with “temp” in the current directory.

Caution with Wildcards

  • Always run ls temp* first to see what will be deleted.
  • Wildcards can delete more than intended if not used carefully.

Recovering Deleted Folders in Linux

Once you delete a folder using rm, it’s usually gone permanently. Linux does not have a built-in recycle bin for terminal deletions.

Using Trash Instead of rm

To avoid accidental permanent deletion, use GUI file managers or tools like trash-cli that move files to Trash.

Data Recovery Tools

If you accidentally delete important data, recovery tools like TestDisk or Photorec might help, but success is not guaranteed.

Summary Table of Commands to Delete Folders in Linux

CommandDescriptionUse Case
rmdir foldernameDelete empty folderFolder has no files or subfolders
rm -r foldernameDelete folder and contentsFolder contains files or subfolders
rm -rf foldernameForce delete folder and contentsDelete without prompts, use carefully
sudo rm -rf foldernameDelete folder with admin rightsFolder requires root permissions
rm -rf temp*Delete multiple folders by patternBulk deletion using wildcards

Conclusion

Now you know how to delete folders in Linux using both the command line and graphical interfaces. The rmdir command is perfect for empty folders, while rm -r and rm -rf handle folders with contents. Always be cautious with powerful commands like rm -rf to avoid accidental data loss.

If you prefer a safer approach, use your file manager’s Trash feature to delete folders. Remember, permissions can affect your ability to delete folders, so using sudo might be necessary. With these tips, you can manage your Linux folders confidently and efficiently.

FAQs

How do I delete a folder that is not empty in Linux?

Use the command rm -r foldername to delete a folder and all its contents recursively. Add -f to force deletion without prompts.

Can I recover a folder deleted with rm?

Folders deleted with rm are usually permanently removed. Recovery is difficult but possible with specialized tools like TestDisk.

What is the difference between rmdir and rm -r?

rmdir deletes only empty folders. rm -r deletes folders and all files and subfolders inside them.

How do I delete a folder I don’t have permission to remove?

Use sudo rm -rf foldername to delete the folder with administrative privileges after entering your password.

Is there a way to delete folders safely without permanent loss?

Yes, use your file manager’s “Move to Trash” option or tools like trash-cli to send folders to Trash before permanent deletion.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts