1831242 Members
2897 Online
110021 Solutions
New Discussion

Unix Shell Script

 
SOLVED
Go to solution
pauline_1
Occasional Advisor

Unix Shell Script

find / -mtime +30 -print

i need to write a script(UNIX) to delete files created older than 30 days
i'm only a beginner and i only found the above function -mtime

i tried this :
for fn in `ls *.*`
do

if [find ${WKDIR}/${fn} -mtime +30]; then
rm ${fn}

fi

done


*but this doesnt work
can someone help?

thanks a lot..reply soon..urgent

9 REPLIES 9
T G Manikandan
Honored Contributor

Re: Unix Shell Script


Hello,

find $dir -type f -mtime +20 -exec rm {} \;


Thanks
Carlos Fernandez Riera
Honored Contributor

Re: Unix Shell Script


cd $DIR

find . -mtime +30 -type f | xargs rm



Be aware that this command will do a recursive list of all dirs bellow $DIR, and delte all files older that 30 days.


Check what this will do first:

ll $( find . -mtime +30 -type f ) | more

unsupported
T G Manikandan
Honored Contributor

Re: Unix Shell Script

Hello,
Carlos is right.
It will remove all files recursively under the directory.

do a list of files
find $dir -type f -mtime +20 -exec ll {} \;
then
remove them
find $dir -type f -mtime +20 -exec rm {} \;


Thanks

pauline_1
Occasional Advisor

Re: Unix Shell Script

Hi all,

for a specified directory in Unix, i need to do these three things.

1. for files within a month, the files will remain
2. for files that are more than 4 mths old, these files are to be removed
3. for files that are between 2nd mth and 4th mth ,these files are to be gzip and tar.

may i know how can u do that in one simgle script?

i dun hav any idea at all..
is ther any other function i can use?
pls help..thanks to all~
Steven Sim Kok Leong
Honored Contributor
Solution

Re: Unix Shell Script

Hi,

Assuming
- 4 months = approx 120 days
- 1 months = approx 30 days

1. for files within a month, the files will remain

# Do nothing

2. for files that are more than 4 mths old, these files are to be removed

# find $dir -mtime +120 -exec rm -f {} \;

3. for files that are between 2nd mth and 4th mth ,these files are to be gzip and tar.

# tar cvf oldfiles.tar `find $dir -mtime +30 -print`

Your script simply executes each in sequence:

archive.sh
====================================
#!/sbin/sh

dir=$1
find $dir -mtime +120 -exec rm -f {} \;
tar cvf oldfiles.tar `find $dir -mtime +30 -print`
====================================

To execute, e.g.

# archive.sh /tmp/testdir

WARNING: Always test your script first on a test directory before actually running it on a production basis.

Hope this helps. Regards.

Steven Sim Kok Leong
Carlos Fernandez Riera
Honored Contributor

Re: Unix Shell Script

Two ideas...

If you move older files to another(s) directories you will never find any file older that x months.


See man sh-posix.... search test

See man touch


touch MMDDhhmm date1
touch MMDDhhmm date2 # older date

for file in *
do

if ( $file -nt $date1 ) ; then

echo $file >> files_to_remove

else if ( $file -nt $date2 ); then

echo $file >> files_to_tar
fi
fi
done

It is not checked, only the basis of a script...


Good Luck.

unsupported
pauline_1
Occasional Advisor

Re: Unix Shell Script

if under that directory there are sub-directories... will they be affected?
S.K. Chan
Honored Contributor

Re: Unix Shell Script

Yes all sub dirs will be processed (as mentioned by Carlos already). If you want to exclude a dir from being traversed you can do this ..
# cd /dira
# find . \( -name dirb -prune \) -o -mtime +30 -exec rm -f {} \;
That will exclude "dirb" from being "processed". Another option you can use is the "-newer" option. For example you want to list all files under /dira which has a timestamp later than say May08 02:05am you can do this ..
# touch 05080205 ref
===> create an "empty file" dated May08 02:05
# cd /dira
# find . -newer /tmp/ref -exec ll {} \;
===> list all files newer than /tmp/ref
pauline_1
Occasional Advisor

Re: Unix Shell Script

thanks to all~ :)
appreciated..script completed.