How to Search and Remove Directories Recursively on Linux
In one of our previous articles, we explained how to find out top directories and files consuming the most disk space on file system in Linux. If you notice that such directories no longer contain important files and subdirectories (such as old backups, downloads etc..), then you can delete them to free up space on your disk.
This short tutorial describes how to find and delete directories recursively in the Linux file system.
To achieve the above purpose, you can employ the find command together with rm command using the syntax below. Here, the + sign at the end enables multiple directories to be read simultaneously.
Attention: You must use rm command carefully because it is one of the most dangerous commands to use in Linux: you may accidentally delete critical system directories, thus resulting to system failure.
In the example below, we will search for a directory called files_2008 and delete it recursively:
You can also use find and xargs; in the following syntax, -print0 action enables printing of the full directory path on the standard output, followed by a null character:
Using the same example above, we have:
Last but not least, if you are concerned about the security of your data, then you may want to learn 3 ways of permanently and securely deleting ‘Files and Directories’ in Linux.
Do not forget to read more useful articles about file and directory management in Linux:
In this article, we showed you how to find and remove directories recursively on Linux. If you have any question or extra ideas you want to add to this topic, use the comment section below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Recursively remove a directory using C
I want to implement this myself and I come up something like this:
I am guessing I have to start delete things from the deepest level and then go up, but now it seems readdir does not give me that order.
Please help, thanks!
3 Answers 3
You have to recurse all the way down to the lower levels first, as in the following pseudo code:
That’s it really, this guarantees that all lower directories and files are deleted before you attempt to remove the parent.
Since item is likely to be just the current component of the directory, you may need to construct a full name using, for example, strcpy/strcat :
You put all this in a function, accepting a string (which is the top path to be removed.) Then check dit->d_type for DT_DIR , and call the function recursively with the new path (old path plus the name).
I suggest working in three passes: a first (recursive) pass determine which files should be removed and remember their directories, and a second pass actually delete the files (with unlink or remove ), the last pass delete the empty directories (with rmdir or remove )
The nftw function could be useful to you.
And there is always the possibility that some other process fill the directory you are removing.
How do I remove a full directory in Linux?
When attempting to remove a directory using a command, such as rmdir, you may receive a prompt similar to «rmdir: ‘dir’: Directory not empty» and be unable to delete the directory.
To remove a directory that contains other files or directories, use the following command.
In the example above, you would replace «mydir» with the name of the directory you want to delete. Executing the command would recursively delete all files and subdirectories in that directory.
By default, rm will not prompt you to confirm deletions. If rm deletes the files successfully, it displays no output, and silently return exit status 0 (success).
If you want to be prompted before deletions, use the -i option. To see a line of output for each deletion, use the -v (verbose) option. For complete options and examples, see our Linux rm command reference.
Delete / Remove a Directory Linux Command
Commands to remove a directory in Linux
There are two command to delete a folder in Linux:
- rmdir command – Deletes the specified empty directories and folders in Linux.
- rm command – Delete the file including sub-directories. You can delete non-empty directories with rmdir command in Linux.
Let us see some examples and usage in details delete the directories.
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:
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:
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):
$ 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 here:
- For more information read man pages: rm(1)
How to remove files and directories quickly via terminal (bash shell) [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 5 years ago .
From terminal window:
When I use the rm command it can only remove files.
When I use the rmdir command it only removes empty folders.
If I have a directory nested with files and folders within folders with files and so on, is there any way to delete all the files and folders without all the strenuous command typing?
If it makes a difference, I am using the mac bash shell from terminal, not Microsoft DOS or linux.
4 Answers 4
-r «recursive» -f «force» (suppress confirmation messages)
Would remove everything (folders & files) in the current directory.
But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.
Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir .
The other two options you should know are -i and -f . -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you’re absolutely sure you’re deleting the right thing. You can specify these with -r or not; it’s an independent setting.
And as usual, you can combine switches: rm -r -i is just rm -ri , and rm -r -f is rm -rf .
Also note that what you’re learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm ‘s syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.