Often you may want to use Bash to delete all directories older than a specific number of days.
You can use the following syntax to do so:
find /c/users/bobbi/data2/* -maxdepth 0 -type d -mtime +5 -exec rm -rf {} \;
This particular example deletes all directories located at the base path c:/users/bobbi/data2 that are older than 5 days old.
Note that you can change -mtime +5 to use a different number to instead delete all directories that are older than a different number of days.
Note: We specify -type d to only delete directories and not files located at the base path.
The following example shows how to use this syntax in practice.
Example: How to Delete Directories Older than X Days in Bash
Suppose that we would like to delete all directories in the folder located at c:/users/bobbi/data2 that are more than 5 days old.
We can first use the ls command with the following syntax to first view all of the files in this folder along with their last modified times:
ls c:/users/bobbi/data2 -l
The following screenshot shows how to use this syntax in practice:

This article is currently being written on 8/1/2024.
We can use the following syntax to delete all directories in this folder that have been modified more than 5 days ago:
find /c/users/bobbi/data2/* -maxdepth 0 -type d -mtime +5 -exec rm -rf {} \;
The following screenshot shows how to use this syntax in practice:

Notice that when we use the ls command again to view the updated items in the folder we can see that all directories that were modified more than 5 days ago have been deleted.
Feel free to use similar syntax to delete directories older than a specific number of days in your own folder.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Delete Files Older than X Days
Bash: How to Delete Files Older than X Hours
Bash: How to Find All Files that Were Created Today
Bash: How to Find Files that Have Been Changed in Last 24 Hours
