Delete multiple folders in a directory which are two weeks old

I need help.

I have to delete multiple directories inside a directory that are two weeks old.

Example: Today is July 09, 2012

Folder1 > folder1 (created June 4, 2012) -- should be deleted
> folder2 (created June 2, 2012) -- should be deleted
> folder3 (created July 1, 2012) -- should be retained as of July 9, 2012 but will be deleted after two weeks
> folder4 (created July 3, 2012) -- -- should be retained retained as of July 9, 2012 but will be deleted after two weeks

Thanks for the help.

---------- Post updated at 03:55 AM ---------- Previous update was at 03:46 AM ----------

I think, the following script is closely related to the problem:

( for d in *// do ksh -c "rm ${d}" & done wait )

Thanks to DGPickett for the above script, however, I need to identify the age of the file
which should be 2 weeks old.

Thanks

Search for FIND command in man pages. It will be really helpful :slight_smile:

Please post what Operating System and version you have and what Shell you use.

Is the "last modified" times stamp which you see in ls -la usable to identify which directories to delete?
Are there any subdirectories below the directories concerned? Especially any subdirectories which should not be deleted?
Do the directories have a naming convention which makes them easy to identify by name? (Using * in deletion scripts can be dangerous).
Does the creation date appear in the directory name?

Please post what Operating System and version you have and what Shell you use.

OS version is Red Hat 4.5-6, Bash

Is the "last modified" times stamp which you see in ls -la usable to identify which directories to delete? Yes, actually, the files to be deleted is only created once and will not be modified.

Are there any subdirectories below the directories concerned? Especially any subdirectories which should not be deleted?There are only two directories, the main directory have subdirectories which contains the files.

The main directory remains and the subdirectories including all files will be deleted.

Do the directories have a naming convention which makes them easy to identify by name? (Using * in deletion scripts can be dangerous).

The main directory doesn't have any naming convention, only pattern (e.g. ####_####_#### or 192_168_252_101).

The subdirectories, however are based on date when the file is created, like 20120709 for 2012 July 09.

Does the creation date appear in the directory name?

Yes, it appears, but for the subdirectories which are for deletion.

Thanks for the help.

Does this command find the directories you want to delete?

find /parent_tree/* -type d -name '????????' -mtime +24 -exec ls -lad {} \;
1 Like