Read from file then purge or archive.

Hi All,
I have a root directory /tmp and I want to purge files or archive files in its subsequent subfolders.I listed the path of files I want to purge(archive) and the #of days.
(purge)
DAYS PATH
7 /tmp/arsenal/*
5 /tmp/chelsea/*

(archive?
the same as above but different folders

To purge I want to use

find $PATH -type f -mtime +$DAYS -exec rm {} \;

The problem now is, in order to do this,I need to first read the DAYS and PATH from the files which I am struggling to do.

I tried this

cat 'filestopurge.txt' | while read DAYS PATH
do
case $AGE in
*) find ........

Please dont forget that it will be read from a file filestopurge.txt

Please advise.

Regards,
Dougy

Something like this ?

while read DAYS PATH
do
   find $PATH -type f -mtime +$DAYS -exec rm {} \;
done < filestopurge.txt

jean-Pierre.

Hi Pierre,
Does your code take filestopurge.txt as the input file?I mean in read the path and days from it?

Thanks a lot
Dougy

I executed your script against the filestopurge.txt and got this:
./purgeam.sh
./purgeam.sh[4]: find: not found
./purgeam.sh[4]: find: not found
./purgeam.sh[4]: find: not found

the filestopurge.txt looks like this:
DAY PATH
90 /interface/backup/dbmig/tmp/*
100 /interface/backup/dbmig/*

Your script apparently read the DAY as 90 and the path, and executes the command.

Am I right?

For each line of the file, the script read the two fields in the variables DAYS and PATH and execute the find command.

PATH is not a good choice for a variable because it is used by the shell.

while read DAYS ARCH_PATH
do
   find $ARCH_PATH -type f -mtime +$DAYS -exec rm {} \;
done < filestopurge.txt

If the header 'DAY PATH' is present in your file, it must be skipped:

while read DAYS ARCH_PATH
do
   [ "$DAYS" = "DAY" ] && continue # Edit: Correct Variable Name  
    find $ARCH_PATH -type f -mtime +$DAYS -exec rm {} \;
done < filestopurge.txt

Another way

tail +2 filestopurge.txt | \
while read DAYS ARCH_PATH
do
   find $ARCH_PATH -type f -mtime +$DAYS -exec rm {} \;
done 

Jean-Pierre.

Thanks Jean,
THe problem now, I got this error:Error in processing the argument DAYS. I thought probably, its unable to read the numbers and the path. I was thinking of using a case statement.What do you think?

Thanks

Run your script with debug option to see the command that are executed

sh -x the_script

In the input file, remove the trailing '/*' from paths.

Jean-Pierre.

I got an error "find: Error in processing the argument DAYS"

I reckon its not able to identify the number below the DAYS

DAYS ARCH_PATH
7 /inter/dfd/
5 /inter/fdfd/

Its unable to process 7,5 etc

Is that right?

Run your script with the -x option and show us the result please.

I correct a variable name in my previous code.

Jean-Pierre.

Hi Pierre,

Heres the output

sh -x purgeam.sh

+ 0< filestopurge.txt
+ read DAYS ARCH_PATH
+ find ARCH_PATH -type f -mtime +DAYS -exec rm {} ;
find: Error in processing the argument DAYS
+ read DAYS ARCH_PATH
+ find /interface/backup/dbmig/tmp -type f -mtime +44 -exec rm {} ;
+ read DAYS ARCH_PATH
+ find /interface/backup/dbmig -type f -mtime +100 -exec rm {} ;
+ read DAYS ARCH_PATH

The filetopurge.txt looks like this:
DAYS ARCH_PATH
44 /interface/backup/dbmig/tmp
100 /interface/backup/dbmig

Does the code recognise 44 and 100 as a substitute for DAYS?

Thanks Pierre

Hi

I removed the DAYS and ARCH_PATH from the filestopurge.txt and got this output. The files still werent purged.

+ 0< filestopurge.txt
+ read DAYS ARCH_PATH
+ find -type f -mtime + -exec rm {} ;
Usage: find path-list [predicate-list]
+ read DAYS ARCH_PATH
+ find /interface/backup/dbmig/tmp -type f -mtime +44 -exec rm {} ;
+ read DAYS ARCH_PATH
+ find /interface/backup/dbmig -type f -mtime +100 -exec rm {} ;
+ read DAYS ARCH_PATH

Regards,

Most importantly Pierre,There could be a problem.
In a directory structure like

A|
-B
-C|
-D

Should I have in the filetopurge.txt /A/ and run the purgeam.sh file, then its purges automatically B,C and D since its recursive. THe problem now is,I might want to archive C i.e not purge the files,there will be situations whereby I need to archive a subfolder of a folder I have in filetopurge.txt(in this case,subfolder C). How do I go about it?
I considered having two text files, filestopurge.txt and filestoarch.txt,but this obviously would work, because in filestopurge,I would have:
7 /A and filetoarch.txt would be 5 /A/C .
After running filestopurge.sh, C would be gone.

Hope u understand the question.

Thanks

Sorry Pierre

A is the root folder,it contains two subfolder B and C,C has a subfolder D

Except the last question I asked.I am able to purge files now, but the script purges everything,even the ones I want to archive because its recursive and purges even the subfolders of the root directory. How do I escape this?

If you want to do non-recurcive find :

cd $ARCH_PATH
find . \( -type d ! -name . -prune  \) -o  -type f -mtime +$DAYS -exec rm {} \;
cd -

Jean-Pierre.

I appreciate it.That was nice of you.

Regards,
Kayarsenal