How to Remove Directory in Linux

Remove Directory in Linux

Quick Steps and Scenarios Overview

What You Need to DoQuick Jump
CautionJump to Step 1
rm commandJump to Step 2
ExampleJump to Step 3
rmdir commandJump to Step 4
ExampleJump to Step 5
Remove directory with findJump to Step 6

How to Remove or Delete Directory in Linux

To delete a directory in Linux remove command to use is rm or rmdir. Both work fine but there is an important difference between how rm and rmdir remove a directory.

Caution

If you delete a directory from the GUI even in Linux, the folder is moved to the Trash bin. It is not really deleted right away.

If you remove a file using command line it skips the Trash bin – the directory gets deleted now not later!

Use ls /directory-path/your directory/ to be sure this is the actual directory you want the be deleted.

rm command

The rm command can be used to delete directories but also file and also everything if you are not careful.

There are many jokes and memes about the command “rm -rf /” but you should know what it does. It deletes everything, no questions asked -f and recursive in sub-directories -r in the root of your system / so it really deletes everything.

rm -r directory/

-r is for recursive – subfolders and files

rm -rf directory/

–rf is recursive and force – no questions – deletes all it can in the target directory you specify.

Example

[dragos@localhost ~]$ mkdir test-diy
[dragos@localhost ~]$ ls | grep diy
test-diy
[dragos@localhost ~]$ rm test-diy/
rm: cannot remove 'test-diy/': Is a directory
[dragos@localhost ~]$ rm -r test-diy/
[dragos@localhost ~]$ mkdir -p test-diy/test1/test2/test3
[dragos@localhost ~]$ tree | grep test
├── printtest
│ │ │ │ │ └── testfile1
│ │ │ │ └── testfile2
│ │ └── testfile3
│ └── test4
├── test
├── test1
├── test3
├── test-diy
│ └── test1
│ └── test2
│ └── test3
└── test.html
[dragos@localhost ~]$ rm -r test-diy/
[dragos@localhost ~]$ tree | grep test
├── printtest
│ │ │ │ │ └── testfile1
│ │ │ │ └── testfile2
│ │ └── testfile3
│ └── test4
├── test
├── test1
├── test3
└── test.html
[dragos@localhost ~]$
[dragos@localhost ~]$ rm -r rex.red/
[dragos@localhost ~]$ tree.
├── 8
│ └── nine
├── file
├── filed
├── fileee
├── logfiles
├── printtest
├── test
├── test1
├── test3
└── test.html

2 directories, 9 files
[dragos@localhost ~]$
[dragos@localhost ~]$ rm * -r
[dragos@localhost ~]$ tree.

0 directories, 0 files
[dragos@localhost ~]$
[dragos@localhost ~]$ mkdir -p test1/test2/test3/test4
[dragos@localhost ~]$ tree.
└── test1
 └── test2
 └── test3
 └── test4

4 directories, 0 files
[dragos@localhost ~]$

rmdir command

It deletes a directory without having to specify extra parameters.

In a way it is easier and safer than rm with the -rf options.

rmdir /directory-path-to/your-directory

or

cd /directory-path-to
rmdir your-directory

or just

rmdir your-directory/

If the directory is not empty you will receive the error

[dragos@localhost ~]$ rmdir rex.red/
rmdir: failed to remove 'rex.red/': Directory not empty

To delete a non-empty directory use rm -r directory/ instead.

Example

[dragos@localhost ~]$ mkdir test-diy.rednumberone.com
[dragos@localhost ~]$ ls
8 file filed fileee logfiles printtest rex.red test test1 test3 test-diy.rednumberone.com test.html
[dragos@localhost ~]$ ls | grep diy
test-diy.rednumberone.com
[dragos@localhost ~]$ rmdir test-diy.rednumberone.com
[dragos@localhost ~]$ ls | grep diy
[dragos@localhost ~]$ ls
8 file filed fileee logfiles printtest rex.red test test1 test3 test.html
[dragos@localhost ~]$

Remove directory with find

Find is a command line utility that can list files or directories but with the exec command and {} + option it executes the new command and passes it’s output directory path as an argument.

If you want to delete one or multiple directories it’s better to run the find command without exec to see if this is really what you want to remove in the first place.

find /home/dragos/test1 -type d -name '*-testlogs'

If you would like to find and delete all *-testlogs folders / directories you can use the bellow command.

find /home/dragos/test1 -type d -name '*-testlogs' -exec rm -r {} +

Helpful Tips

Check all labels, connections, and power ratings before starting so everything fits safely and securely. If any detail looks slightly different on your model, pause the embedded video above for a clear closeup view.

Disclosure: This article contains Amazon affiliate links. As an Amazon Associate, we earn from qualifying purchases at no extra cost to you.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.