Script to remove folders by date created


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to remove folders by date created
# 1  
Old 12-19-2011
Script to remove folders by date created

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

Last edited by vbe; 12-19-2011 at 10:53 AM..
# 2  
Old 12-19-2011
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
Code:
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.

Last edited by jim mcnamara; 12-19-2011 at 11:22 AM.. Reason: can't type
# 3  
Old 12-19-2011
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:
Code:
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" !!

Last edited by methyl; 12-19-2011 at 12:06 PM..
# 4  
Old 01-03-2012
Hi
still have no luck with this if i run the command
Code:
/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
Code:
/usr/bin/find /gpfs/SERVER_ADMIN/OUTSOURCE_DONE -type d -mtime +5 -exec rm -r {} \;

I get the following error on all folders
Code:
/usr/bin/find: /gpfs/SERVER_ADMIN/OUTSOURCE_DONE/43056566/43056566_/JPEG_FILE: No such file or directory

Any help appreciated

Treds


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-03-2012 at 09:06 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 01-03-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check current date file is created and with >0 kb or not for multiple directories

Hi All, I am new in scripting and working in a project where we have RSyslog servers over CentOS v7 and more than 200 network devices are sending logs to each RSyslog servers. For each network devices individual folders create on the name of the each network devices IP addresses.The main... (7 Replies)
Discussion started by: Pinaki
7 Replies

2. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

3. Shell Programming and Scripting

Please help, need to create script to remove lines by date in file

Please Help (novice to PERL and SHELL scripting)…. Need to create a script which removes all lines in $filename = "cycle_calendar_ftp_out" older than current date – a variable which will be a number of days passed to script. For Ex it will look at the end date which is the last field (4) and... (2 Replies)
Discussion started by: m3pwr
2 Replies

4. Shell Programming and Scripting

How do I take out(remove) the date part in the file name in a script?

Hi All, I need to create links between two directories. have multiple files in a specified location. Source Location ex: /opt/xdm/input/ Target Location ex: /opt/xdm input file names: 1. abc_app.aus.apac.yyyymmdd.dtd 2. abcd_app.aus.apac.yyyymmdd.dtd I need to build a code that reads... (1 Reply)
Discussion started by: kthri_82
1 Replies

5. Shell Programming and Scripting

Removal of files/folders created during a period of time

Hi, I have some folders (containing files) which gets created daily and names start with 'TT_' . I would like to remove these folders every month end. Could you please provide me the commands and also the syntax to schedule through crontab. Thanks, Archana (3 Replies)
Discussion started by: archana.n
3 Replies

6. UNIX for Dummies Questions & Answers

Remove file and folders Script

Hi I am trying to modify a script that will remove files and folders under a set directory on my server. I am using the following find /home/test/HM/ -type d -exec rm -rf {} \; 2>/dev/null This removes the files in the HM folder but it also removes the HM folder which i do not want to... (3 Replies)
Discussion started by: treds
3 Replies

7. UNIX for Dummies Questions & Answers

Remove Occurrences created with Uniq -c

Hello, I have "mastered" the counting of occurrences with uniq -c. :D Now I need to keep that file neat and nice and remove only the occurrence number and the space after it. Example: 1286456 aaa 164758 aab aaa 112345 aac aaf should be turned into: aaa aab aaa aac aaf I... (6 Replies)
Discussion started by: hobbiecat
6 Replies

8. Shell Programming and Scripting

How remove the file which was created by another user?

When I try to remove the file which was created by another user through super user, I am getting the "override protection 644 " meesage. Could you please anyone help me how will I delete the file without prmpting the override protection. I have also given the permission (rwx) to the group as... (3 Replies)
Discussion started by: kandi.reddy
3 Replies

9. Shell Programming and Scripting

Remove files which created date before 10 days on HP-UX

Hi All, Could you please let me know if there is any one can help to create a shell script to remove some files which is the created date for them greate than 10 days (sysdate-10) Please try to email me on email removed Thanks in advance, Murad (1 Reply)
Discussion started by: murad_fayez
1 Replies

10. Shell Programming and Scripting

List Files & Folders created/modified

Hello people, I want to list the files & folders created/modified since a particular date say June 2006. I know I can list recursively thru the folders and use awk to extract the date column to get the desired output. Just wanted to check whether there is an easier way to do this. Please... (2 Replies)
Discussion started by: tipsy
2 Replies
Login or Register to Ask a Question