![]() |
|
|
|||||||
| 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 |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 Can any one give me a push! :-) |
| Forum Sponsor | ||
|
|
|
|||
|
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. |
|
|||
|
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. |
|
||||
|
Quote:
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 |
||||
| Google UNIX.COM |