Hi All
I require to generate a script that deletes folders and sub directoires from a set period after creation.
So have folder X on a daily basis i have job folders being created folder name 1234568. What i need to do is keep this folder pruned. So say 7 days after creation of the job folder its deleted
thanks
treds
Except for some special cases UNIX does not use create dates - that is purely windows.
the find command uses -ctime (for last time the file metadata changed; usually when it was created) or -mtime (last time the file was written). Pick one.
here is an mtime example
find /path/to/directory -type d -mtime +7 -exec rm -r {} \;
Any directory older than 7 days get clobbered. use either ctime or mtime.
Beware of the red part, you can run it in test mode by exclusing the red code to see what it will do. Why? You can't undelete files in UNIX. rm -r deletes the directory and all of the files and subdirectories under it.
Be very very careful. It is quite normal in unix for a directory to have the value of "-mtime" much much older than the "-mtime" of the subdirectories.
To illustrate this, have a look at some of these timestamps:
ls -lad /
ls -lad /usr
ls -lad /var
Also bear in mind that the action of removing a directory changes the timestamp of the parent directory.
This sort of cleanup needs very careful planning and design.
I don't believe that the script proposed by Jim is at all safe.
Lateral thought idea:
At the time of creating each directory involved in this process, create a timestamp file in the directory. Then use "find" with the "-newer filename" switch to make decisions about that particular directory. This will also ensure that you only look at directories you want to maintain and be prepared to not delete a directory if the subdirectories are not old enough.
Test with "echo" not "rm -r" !!
Hi
still have no luck with this if i run the command
/usr/bin/find /gpfs/SERVER_ADMIN/OUTSOURCE_DONE -type d -mtime +5 -exec "echo" {} \;
I get the list of files to delete
but if i run the delete
/usr/bin/find /gpfs/SERVER_ADMIN/OUTSOURCE_DONE -type d -mtime +5 -exec rm -r {} \;
I get the following error on all folders
/usr/bin/find: /gpfs/SERVER_ADMIN/OUTSOURCE_DONE/43056566/43056566_/JPEG_FILE: No such file or directory
Any help appreciated
Treds
Though I do not endorse the script or the method at all, the error comes from not using the "-depth" switch to "find". Directories and contents and subdirectories and contents have been deleted while they were still in the "find" unprocessed list. Using "rm -r" in directory cleanups is very very dangerous.