0% found this document useful (0 votes)
17 views

How To Remove A Directory in Linux - Nixcraft

The document discusses how to delete or remove directories in Linux using the rmdir and rm commands. It provides examples and explanations of using these commands to delete both empty and non-empty directories, with and without options like -r and -f. It also discusses using find, sudo, and wildcards to delete multiple directories at once.

Uploaded by

NedkoKadrev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

How To Remove A Directory in Linux - Nixcraft

The document discusses how to delete or remove directories in Linux using the rmdir and rm commands. It provides examples and explanations of using these commands to delete both empty and non-empty directories, with and without options like -r and -f. It also discusses using find, sudo, and wildcards to delete multiple directories at once.

Uploaded by

NedkoKadrev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

nixCraft → Howto → Linux → How To Delete / Remove a Directory Linux Command

🔎 To search, type & hit enter...


How To Delete / Remove a Directory Linux
Command
Author: Vivek Gite • Last updated: June 7, 2022 • 106 comments

I am a new Linux user. How do I delete or remove a directory using


command line option? How do I delete a folder in Linux?

Everything is a file in Linux including the directory. A directory is nothing but a


group of files. You can use the following commands to delete the directory in
Linux. Directory is also known as a folder in the Macintosh OS X/macOS and
Microsoft Windows operating system.

Tutorial details

Difficulty level Easy

Root privileges No

Requirements Linux terminal

Category File Management

Prerequisites rmdir or rm command

AIX • Alma • Alpine • Arch • BSD • Debian • Fedora • FreeBSD • HP-UX • Linux •

OS compatibility macOS • Mint • NetBSD • OpenBSD • openSUSE • Pop!_OS • RHEL • Rocky •


Stream • SUSE • Ubuntu • Unix • WSL

Est. reading time 4 minutes


nixCraft: Privacy First, Reader Supported

➔ nixCraft is a one-person operation. I create all the content myself, with no help
from AI or ML. I keep the content accurate and up-to-date.

➔ Your privacy is my top priority. I don’t track you, show you ads, or spam you with
emails. Just pure content in the true spirit of Linux and FLOSS.

➔ Fast and clean browsing experience. nixCraft is designed to be fast and easy to
use. You won’t have to deal with pop-ups, ads, cookie banners, or other distractions.

➔ Support independent content creators. nixCraft is a labor of love, and it’s only
possible thanks to the support of our readers. If you enjoy the content, please support
us on Patreon or share this page on social media or your blog. Every bit helps.

Join Patreon ➔

Commands to remove a directory in Linux


There are two command to delete a folder in Linux:

1. rmdir command – Deletes the specified empty directories and folders in Linux.

2. rm command – Remove the file including sub-directories. You can delete non-empty
directories with rm command in Linux.

Let us see some examples and usage in details to delete the directories or folders under Linux
operating system using the CLI.

rmdir command syntax to delete directory in Linux


The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:

$ rmdir directory-name
$ rmdir [option] directory-name
Open the terminal application and run command to delete given directory. For example, delete
a folder named dir1:

$ rmdir dir1

Delete directory Linux Command


Open a command line terminal (select Applications > Accessories > Terminal), and then type
the following command to remove a directory called /tmp/docs:

$ rmdir /tmp/docs

If a directory is not empty you will get an error message that read as follows:

$ rmdir letters

Sample outputs:

rmdir: letters: Directory not empty

You can cd to the directory to find out and list all files:

$ cd letters
$ ls

Delete those files or directories. In this next example, remove data, foo and bar if bar were
empty, foo only contained bar and data only contained foo directories:

$ cd /home/nixcraft
$ rmdir -p data/foo/bar

Where,

-p : Each directory argument is treated as a pathname of which all components will be


removed, if they are empty, starting with the last most component.
How to see a diagnostic message for every directory
processed
Pass the -v option to the rmdir command:

$ rmdir -v dir1

Sample outputs:

rmdir: removing directory, 'dir1'

Removing directories with rmdir and wildcards

We can use wildcards such as ‘*’ and ‘?’ to match and delete multiple directories. For example:

$ ls -l dir*

We have three dirs named dir1, dir2, and dir3. To delete all directories starting with ‘dir’ in the
current, you would use the following command:

$ rmdir -v dir*
Linux remove entire directory including all files and sub-
directories command
To remove all directories and subdirectories use the rm command. For example, remove
*.doc files and all sub-directories and files inside letters directory, type the following
command:

Warning: All files including subdirectories will be deleted permanently when executed the
following commands.

$ rm -rf letters/

Sample session:

Where,
-r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively
remove subdirectories and files from the specified directory.

-f : Attempt to remove the files without prompting for confirmation, regardless of the file’s
permissions

Are you getting permission denied error message while


removing directories?
Only owners can delete their directories. However, a sysadmin can delete any directories
created by anyone on the system. The syntax is:

$ sudo rmdir /path/to/dir/


$ sudo rm -rf dir2

When prompted, you need to provide root user or sudo user password.

Use find command to delete unwanted directories


Say you want to find out all directories named ‘session’ and delete them in the current
directory, run:

$ find . -type d -iname 'session' -delete

How to find and remove all empty directories


Run:

$ find . -type d -iname 'session' -empty -delete

Where,

-type d : Only search for directories and ignore all other files.

-iname 'session' : Search directory named ‘session’. You can use wildcards here too.
For example, -iname 'dir*' .
-empty : Only match empty directories

-delete : Deletes all found empty directories only

To delete all ‘.DS_store’ directories stored in /var/www/html, run:

$ sudo find /var/www/html/ -type d -name .DS_Store -exec rm {} \;

OR

$ sudo find /var/www/html/ -type d -name .DS_Store -exec rm {} +

The -exec option to the find command run an external command named rm to delete all files.
The “ rm {} +/ ” is a better option as it uses one rm command to delete all .DS_Store
directories.

Conclusion
This page showed how to delete a directory when it is empty. Further, it showed, how to
remove folders using the rm and rmdir commands. See rm help page page for more info using
the man command or rm command:

$ man rmdir
$ man rm
$ rm --help
$ rmdir --help

Did you notice? 🧐


nixCraft is ad-free to protect your privacy and security. We rely on reader support to keep
the site running. Please consider subscribing to us on Patreon or supporting us with a
one-time support through PayPal. Your support will help us cover the costs of hosting,
CDN, DNS, and tutorial creation.

Join Patreon ➔ PayPal ➔


About the author: Vivek Gite is the founder of nixCraft, the oldest running blog about Linux
and open source. He wrote more than 7k+ posts and helped numerous readers to master IT
topics. Join the nixCraft community via RSS Feed or Email Newsletter.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.
🔎 To search, type & hit enter...
Related Tutorials
UNIX Delete / Remove Directory Command

Delete a non-empty directory when you get directory…

Ubuntu Linux Remove / Delete PPAs apt-get command

Linux Delete / Remove User Account Using userdel command

Unix / Linux: Tar Command Remove a Directory From a Tar Ball

Unix / Linux: Remove Nonempty Directory Command

How to remove directory in Linux using rmdir and rm command

106 comments… add one ↓

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy