Removing unimportant directories from Linux helps in memory and space optimization. In this article, we will guide you on how to delete a full directory including files on Linux. So, let’s get started!
How to Delete a Full Directory Including Files on Linux
In Linux, it is simple to remove an empty directory, but if you try to delete a full directory, you will face the “rmdir: ‘dir’: Directory not empty” error. Hence, we will look at ways in which you can delete a non-empty directory.
Delete a Full Directory Using the rm Command
The most common command used to delete the directory is rm. The synopsis of rm looks something like this:
sudo rm [OPTION] DIRECTORY
However, if you run this command on the full directory or a directory that has even one file, you will encounter the following error:

To fix this, use the -r
option. This flag recursively deletes all the content inside the directory, and once the directory is empty, it will also delete it. Open the terminal by pressing “Ctrl + Alt + T” and type:
sudo rm -r /path/to/dir/
Make sure to replace the /path/to/dir/
with the directory, you wish to delete. In addition, rmdir performs a delete operation without a confirmation prompt. Therefore, you should always have a backup of the directories before performing the delete operation. If you want the confirmation prompt before the deletion operation, use the -i
flag. For example:
sudo rm -i -r /path/to/dir/

Furthermore, the rm command does not show any message if the operation is successful. To view the verbose output, use the -v
option. Input:
sudo rm -v -r /path/to/dir/
If you want to delete multiple directories, write the names separated by a space. For instance, type:
sudo rm -r dir1 dir2 dir3
To delete a directory with no write permissions, use the -f
flag. It will forcefully delete the directories. Type:
sudo rm -r -f /path/to/dir/
Delete Files From Directory Using the rm Command
Alternatively, you can also delete files inside the directory first. Then delete the empty directory. Suppose you have a directory called /home/sidrah/data
. To check if the directory has files, use the ls
command. Specifically, type:
ls ~/data/
To delete all the files in this directory, execute:
rm /home/sidrah/data/*
To view the deletion operation, run:
rm -v /home/vivek/data/*
Verify that the files have been deleted using the ls
command:
ls -l /home/vivek/data/
Alternatively, if you want to recursively delete with verbose output, type:
rm -v -r -i /home/vivek/data/*
Delete Hidden Files Using the rm Command
At times, there are hidden files in the directory which you must delete before deleting the directory. To check the hidden files in the directory, type:
ls -a /path/to/directory
To remove all files except hidden files, execute:
rm /path/to/dir/*
rm -rf /path/to/dir/*
rm *
Alternatively, to delete all hidden files, type:
rm -rf /path/to/dir1/{*,.*}
rm -rfv /path/to/dir1/{*,.*}
Delete a Full Directory Using the Unlink Command on Linux
The third option to delete the full directory is using the unlink
command. However, the unlink command only deletes a single file at a time. Hence, we will first delete the files and then delete the directory. Navigate to the directory you want to delete using the cd command. For instance, type:
cd /path/to/directory
Once you are inside the directory, delete the files using the unlink
command as shown below:
unlink filename
Make sure to replace the filename with the appropriate name of the file.

You must note that the unlink command only allows the deletion of a single file. So, if there are multiple files, delete them using the rm
command. Once the files are deleted, you can now delete the directory using the rm
or the rmdir
command. Specifically, type:
cd ..
sudo rm /path/to/directory
sudo rmdir /path/to/directory
Remove Directories Using the Find Command
The find is another command-line utility that searches for files and directories depending on the options. Then it brings the matched file or a directory. It is most commonly used to delete directories. The synopsis for the command is:
find [-H] [-L] [-P] [-D debugopts] [-O level] [starting-point...] [expression]
Let’s suppose you want to delete all directories that end with _cache
name, so you will run:
find. -type d -name '*_cache' -exec rm -r {} +
Let’s look at the explanation of each option we’ve used:
- (
.
) – recursively searches in the current working directory -type d
– exclusively searches for the directories only-name '*_cache'
– performs the search by name and searches for directories that end with “_cache”-exec
– executes an external command. In other words, the operation you want to perform on the search result from the find command. In this case, that is rm -r.{} +
– appends the search result to the end of the rm command.
Example for Empty Directory Using Find Command
Now we will consider another example. For instance, if you want to delete empty directories, you would execute:
find . -type d -empty -delete
- (
.
) – recursively searches in the current working directory -type d
– exclusively searches for the directories only-empty
– exclusively searches for the empty directory-delete
– deletes all found empty directories in the subtree.
Make sure you add the -delete
option at the end of the find command. Otherwise, it will delete all the content below the starting point.
You can check whether the directory has been deleted or not using the ls
command as shown below:

Sometimes, you will get an error if the number of files is huge in a directory you are trying to delete. For this scenario, use the find command as shown below:
find . -type f -delete && rm -r /dir
And that’s a wrap! We have covered several commands you can use to delete a full directory including files on Linux. Check out the manuals on rm, rmdir, unlink and find command to browse more information. Are you interested in learning more about directory operations? Read about navigating a directory, partitioning the directory, and renaming directories.
If this guide helped you, please share it.