The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > Linux
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
deleteing shary Shell Programming and Scripting 4 05-09-2008 06:51 AM
How to open a file that is archived Tanvirk UNIX for Advanced & Expert Users 1 03-11-2008 01:51 AM
tar :: rename the file after archived thambi Shell Programming and Scripting 1 05-31-2007 12:56 AM
deleteing .doc file agarwalniru UNIX for Dummies Questions & Answers 2 05-25-2007 09:18 AM
Showing that something is deleteing nhatch Shell Programming and Scripting 1 05-06-2003 06:16 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-2008
Registered User
 

Join Date: May 2008
Posts: 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Deleteing archived files

Hi,

I need to remove files that are in archive directory and which are old. I can make use of find command to search for files which are older by number of days.

But the problem is there are sub directories in directory 'archive' like 'sub1' 'sub2' 'sub3'.

Now all files which are in 'archive/sub1' should be deleted if they are old by 30 days.
All files which are in 'archive/sub2' should be deleted if they are old by 90 days.
Now all files which are in 'archive/sub3' should be deleted if they are old by 120 days.
If the files are in 'archive/' then they should be deleted if they are older by 365 days.

I want to create a properties file like

Code:
#archive policy
# foldername=<period-in-days>
archive/sub1=30
archive/sub2=90
archive/sub3=120
default = 365
and I want to write a shell script that reads location from above file to know the period of days.

Can any one give me a push! :-)
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-02-2008
Registered User
 

Join Date: Aug 2007
Posts: 21
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Thumbs up

Buddy,

Having a properties file is a good option. U can use a loop to work on each directory individually. Loop it until it reaches the last directory. Inside the loop, do the following things:
1. Extract the period from the properties file in some variable. This can be done using grep command to search the directory name and then using cut command.

2. Check whether the directory is period times older. This can be done using find command with the option "mtime". Find command will display the list of files if the directory is older by period no. of days.

if[! -z `find "$file" -mtime +$period`];then
rm -r $file
fi

3. Remove the files if it satisfies the "if condition" .

4. Continue the same for all the directories.
5. I think you should take care of files directly under archive separately. Follow the same metod of checking whether the files are older by default time or not. If yes then delete them.

This is just an algorithm to solve the problem. Please revert back in case of any issues.
Reply With Quote
  #3 (permalink)  
Old 05-03-2008
Registered User
 

Join Date: May 2008
Posts: 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Am able to read the lines of file and cutting the two fields using cut command.
Eg. line archive/subdirectory=30
using cut
cat filename|while read line
paths=$"(echo $line | cut -d"=" -f1)"
days=$"(echo $line | cut -d"=" -f2)"
find $paths -type f -mtimd $days -exec rm {} \;

Heres a logical problem. find command is drilling into subdirectories.
If I have a subdirectory inside archive say archive/subdirectory/innerdir
and the retention period for innerdir is 90 days.
The above procedure is deleting all files of subdirectory as it searches files from innerdir too.
Reason: Because in the first run the a command finds all files that are older by 30 days and deletes them

Let me repeat it again
If the retention period for an inner directory/child directory is more than the parent directory the above process fails to restore the files of child directory.

I hope I have made the point clear. Time for an expert to take a glance
Can any one provide me a good solution/idea?

Thanks.
Reply With Quote
  #4 (permalink)  
Old 05-05-2008
Registered User
 

Join Date: May 2008
Posts: 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
SOS!!
No Clue!?!!
Reply With Quote
  #5 (permalink)  
Old 05-05-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 119
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by ramu_indian View Post
Eg. line archive/subdirectory=30 ...
...
...
Heres a logical problem. find command is drilling into subdirectories.
If I have a subdirectory inside archive say archive/subdirectory/innerdir
and the retention period for innerdir is 90 days.
The above procedure is deleting all files of subdirectory as it searches files from innerdir too.
...

Use the maxdepth option of find in your code:

Code:
find $paths -maxdepth 2 -type f -mtimd $days -exec rm {} \;

Here is a good place to start:

advanced/complex uses of the find command
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 04:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102