1
votes

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

1
How do you delete any folder while preserving it's sub-folders? That seems contradictory. Is there a bigger problem you're trying to solve?beeflobill
My current command checks all sub-folders as well but it should only check the modification date of their main folders which are located at /path/ and not e.g. /path/main_directory_1/sub-folder/.LeVence
yes, we read your headline and and problem description. Are you asking to delete only files and leave subdirs in place? What about files that are in subdirs? Please rephrase your question so someone can help. Good luck.shellter
Sorry. I updated the thread.LeVence
Maybe see the find man page? Possibly the -maxdepth option is of interest.larsks

1 Answers

2
votes

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