Shashikant shah

Thursday, 15 January 2015

Delete files based on date :-

#!/bin/bash

P=$1
d=$(date +%d --date="2 days ago")

file=$(ls -lh $P | awk '{print $6 " " $7 " " $9}' | sed -n /$d/p | awk '{print $3}')

echo  "$file" > /tmp/word

for w in `cat /tmp/word`
        do
        find $P -name $w >> /tmp/path
done

e=`cp -rf /tmp/path /tmp/path.bkp`

c=`echo -n "$file" | wc -l`
 echo "count $c "
if [ 0 == "$c" ];then

        echo "not deleted."
exit 2
else
        for x in `cat /tmp/path`
        do
        rm -rf $x
done
fi
rm -rf /tmp/path
====================================================
./script.sh  /dir_name

No comments:

Post a Comment