I have directories named as:
2012-12-12
2012-10-12
2012-08-08
How would I delete the directories that are older than 10 days with a bash shell script?
Join Stack Overflow to learn, share knowledge, and build your career.
|
This will do it recursively for you:
Explanation:
Alternatively, use:
Which is a bit more efficient, because it amounts to:
as opposed to:
as in the Note: Also see @MarkReed's comment below regarding preferred usage with modern version of |
|||||||||||||||||||||
|
|
If you want to delete all subdirectories under
but you don't want to delete the root
and so forth. So , to delete all the sub-directories under
|
||||
|
|
|
I think there's a catch that the files need to be 10+ days older too. Haven't tried, someone may confirm in comments. The most voted solution here is missing
The |
||||
|
|
|
I was struggling to get this right using the scripts provided above and some other scripts especially when files and folder names had newline or spaces. Finally stumbled on tmpreaper and it has been worked pretty well for us so far.
Original Source link Has features like test, which checks the directories recursively and lists them. Ability to delete symlinks, files or directories and also the protection mode for a certain pattern while deleting |
|||
|
|
|
OR
Updated, faster version of it:
|
|||||||||||||||||
|
findcould do it without looking at the name then... – Wrikken Dec 13 '12 at 21:33ctimeis the inode change time. For a directory, it changes when files are added or removed from the directory. – ajk Feb 20 '14 at 11:36