Deleteing archived files


 
Thread Tools Search this Thread
Operating Systems Linux Deleteing archived files
# 1  
Old 05-02-2008
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! :-)
# 2  
Old 05-02-2008
MySQL

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.
# 3  
Old 05-03-2008
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 Smilie
Can any one provide me a good solution/idea?

Thanks.
# 4  
Old 05-05-2008
SOS!!
No Clue!?!!
# 5  
Old 05-05-2008
Quote:
Originally Posted by ramu_indian
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:

https://www.unix.com/answers-frequent...d-command.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need Help with Archived UNIX/Linux IBM (UniVerse) Database

I have an old database that ran on UNIX/LINUX that was archived and stored years ago. We currently run on Windows and have no way to open/view this database or recover data. Does anyone have any experience or know how to recover? All I know is that it was a UniVerse IBM product. Thanks! (2 Replies)
Discussion started by: Chantelle
2 Replies

2. UNIX for Dummies Questions & Answers

Archived Emails in UNIX Executable Files

Hello, A while back someone"archived" my emails for me, so I didn't have to worry about my email account filling up with emails. I need to get back into those emails and view them. When I opent the folder it has several files. The largest being an "mbox", which I am assuming has all of... (3 Replies)
Discussion started by: shaffer1921
3 Replies

3. UNIX for Dummies Questions & Answers

deleteing duplicate lines sing uniq while ignoring a column

I have a data set that has 4 columns, I want to know if I can delete duplicate lines while ignoring one of the columns, for example 10 chr1 ASF 30 15 chr1 ASF 20 5 chr1 ASF 30 6 chr2 EBC 15 4 chr2 EBC 30 ... I want to know if I can delete duplicate lines while ignoring column 1, so the... (5 Replies)
Discussion started by: japaneseguitars
5 Replies

4. Shell Programming and Scripting

Deleteing one character after an special character

I have below line in a unix file, I want to delete one character after "Â". 20091020.Non-Agency CMO Daily Trade Recap Â~V Hybrids The result should be : 20091020.Non-Agency CMO Daily Trade Recap  Hybrids i dont want to use "~V" anywhere in the sed command or any other command, just remove... (1 Reply)
Discussion started by: mohsin.quazi
1 Replies

5. Shell Programming and Scripting

Avoid files being archived

hi all, i want to write a shell script which can automatically touch my all files within a folder in an interval of 90 days ...so that i can avoid them being archived. I don't want to manually touch the all files instead i want an automated shell script to do this. Thanks in advance, Om (3 Replies)
Discussion started by: koti
3 Replies

6. Shell Programming and Scripting

deleteing

Dear Experts , I have 15 files and each files contains the data as mentioned below fileA name company salary job location nationality 12121 234234 13123 12121 234234 13123 12121 234234 13123 12121 234234 13123 12121 234234 13123 12121 234234 13123 12121 234234 13123... (4 Replies)
Discussion started by: shary
4 Replies

7. UNIX for Advanced & Expert Users

How to open a file that is archived

Hello, 1)Can anybody please tell me that how can i open a file that is archived. Like say i have a file say 1.txt archived inside 1.tar. 2)So how can i open this file to perform operations on this file (1.txt) using "open" system call and perform other system calls like "read" and "write" ,but... (1 Reply)
Discussion started by: Tanvirk
1 Replies

8. Shell Programming and Scripting

tar :: rename the file after archived

Hi, I am archived two files using tar. Then I tried to extract the file in Windows. But only one file extracted. Other file is NOT extracted as the UNIX file name NOT compatible with Windows platform (In my case semicolon operator included in file name). Is it possible to rename the file (... (1 Reply)
Discussion started by: thambi
1 Replies

9. UNIX for Dummies Questions & Answers

deleteing .doc file

hi i ama dunmmies in Unix. I created a .doc file and copied it to unix via FTP as a ,doc file itself i want to delete that file , i tried with rm command it does not work file name is DDL's.doc plz help :confused: (2 Replies)
Discussion started by: agarwalniru
2 Replies

10. Shell Programming and Scripting

Showing that something is deleteing

Hi there, I have a scritp that is clearing out old files from some directories, rather than echoing the name of the file when it gets deleted i want to echo a ". " I have tried to just echo it after the deletion but it lists like this . . . . I want to show it like this . . . . . . . . .... (1 Reply)
Discussion started by: nhatch
1 Replies
Login or Register to Ask a Question