LeVence
LeVence

Reputation: 61

Linux - Delete directories which are older than x days

I'd like to know how I could delete all directories which are older than 14 days, without deleting their sub-folders.

I had been using the following command, but this will also check / delete all located sub-folders which are bound to their main directories:

find /path/ -mtime +14 -type d | xargs rm -f -r;

So it basically should only check if directories in /path/ are older than 14 days and delete them if so. My current command above does also check their sub-folders and delete those if older than 14 days, but it shouldn't check them - only the "main" folders in /path/.

Chris

Upvotes: 1

Views: 2487

Answers (1)

LeVence
LeVence

Reputation: 61

Could solve it by using a pattern:

find /path/ -name "FOLDER_*-*-*_*" -mtime +14 -type d | xargs rm -f -r;

This command will delete all directories which are located at /path/, "without checking" their sub-folders, after 14 days. Directories names at /path/ must patch the following pattern, e.g. FOLDER_08-25-16_8:00.

It'll basically check sub-folders as well but those must match the pattern above otherwise they won't be checked. That's not a complete solution but it's definitely better than nothing and it does what I had been looking for, right? ^^

Chris

Upvotes: 2

Related Questions