Cygal.net.pl

Systemy, Sieci i takie tam :)

Skrypt do znajdowania i usuwania plików z zadanym zakresie dat

Poniżej zamieszczam prosty skrypt napisany w bashu, który służy do znajdowania oraz usuwania plików stworzonych lub modyfikowanych w skazanym zakresie dat.
Mam nadzieję że komuś się przyda tak jak przydaje się mnie :). Dzięki temu skryptowi można zaoszczędzić sporo czasu.
Skrypt może prócz usuwania plików z zadanego przedziału dat usuwać również pliki starsze niż “N” dni

[bash]
#!/bin/bash
function show_usage(){

if [[ $1 == "" || $2 == "" || $3 == "" || $4 == "" ]]; then
echo -e "\033[31mSyntax for script with 4 arguments\033[0m"
echo -e "\033[32mUsage: ./seekanddrstroy.sh <PATH_TO_DIR> \033[0m"
echo -e "\033[32mEXAMPLE:(Deleting files from /home/user/ for day 2013-07-04) ./seekanddestroy.sh /home/user 2013-07-04 2013-07-05 delete\033[0m"
echo -e "\033[32mUsage2: ./seekanddrstroy.sh <PATH_TO_DIR> <NUBERS_OF_DAYS> -now\033[0m"
echo -e "\033[32mEXAMPLE2:(Deleting files from /home/user/ between 30 days ago and present data) ./seekanddestroy.sh /home/user 30 -now last\033[0m"
echo -e "\033[32mOPTIONS: delete – delete files in certain period\033[0m"
echo -e "\033[32mOPTIONS: last – delete files older than X days\033[0m"
echo -e "\033[32mOPTIONS: show – list all found files in certain period\033[0m"
echo -e "\033[32mOPTIONS: showlast – list all found files older than X days\033[0m"
echo -e "\033[32mATTRIBUT: -now – works only with OPTION show last and last and this attribute determined current date \033[0m"
echo -e "\n"
echo -e "\033[31mSyntax for script with 3 arguments\033[0m"
echo -e "\033[32mUsage: ./seekanddrstroy.sh <PATH_TO_DIR> <NUBERS_OF_DAYS> \033[0m"
echo -e "\033[32mEXAMPLE:(Deleting files from /home/user/ older than 30 days) ./seekanddestroy.sh /home/user 30 older\033[0m"
echo -e "\033[32mOPTIONS: showolder – list all found files older than X days\033[0m"
echo -e "\033[32mOPTIONS: older – delete all found files older than X days\033[0m"

exit;
fi
}

function last_few_days(){
NOW=`date +%Y-%m-%d`
AGO=`date –date="$START_DATE days ago" +%Y-%m-%d`
}

FIND=`which find`
DIR=$1
START_DATE=$2
END_DATE=$3
OPTION=$4

function main(){

echo -e "Your choice is: \033[33m$OPTION \033[0m"

if [ "$OPTION" == "show" ]; then
echo -e "\033[32mStart date: $START_DATE \033[0m"
echo -e "\033[32mEnd date: $END_DATE \033[0m"
$FIND $DIR -newermt $START_DATE -not -newermt $END_DATE -type f -ls
elif [ "$OPTION" == "delete" ]; then
echo -e "\033[32mStart date: $START_DATE \033[0m"
echo -e "\033[32mEnd date: $END_DATE \033[0m"
$FIND $DIR -newermt $START_DATE -not -newermt $END_DATE -type f | awk ‘END { print "Deleted files:" NR }’
$FIND $DIR -newermt $START_DATE -not -newermt $END_DATE -type f -delete
elif [[ "$OPTION" == "showlast" && "$END_DATE" == "-now" ]]; then
last_few_days "$START_DATE"
NUMBERS_DAYS=$START_DATE
START_DATE=$AGO
END_DATE=$NOW
echo -e "\033[32mdate for $NUMBERS_DAYS days ago: $START_DATE \033[0m"
echo -e "\033[32mcurrent date: $END_DATE \033[0m"
$FIND $DIR -newermt $START_DATE -not -newermt $END_DATE -type f -ls
elif [[ "$OPTION" == "last" && "$END_DATE" == "-now" ]]; then
last_few_days "$START_DATE"
NUMBERS_DAYS=$START_DATE
START_DATE=$AGO
END_DATE=$NOW
echo -e "\033[32mdate for $NUMBERS_DAYS days ago: $START_DATE \033[0m"
echo -e "\033[32mcurrent date: $END_DATE \033[0m"
$FIND $DIR -newermt $START_DATE -not -newermt $END_DATE -type f | awk ‘END { print "Deleted files:" NR }’
$FIND $DIR -newermt $START_DATE -not -newermt $END_DATE -type f -delete
elif [ "$OPTION" == "showolder" ]; then
$FIND $DIR -type f -mtime +$OLDER_THAN -ls
elif [ "$OPTION" == "older" ]; then
$FIND $DIR -type f -mtime +$OLDER_THAN | awk ‘END { print "Deleted files:" NR }’
$FIND $DIR -type f -mtime +$OLDER_THAN -delete
else
echo -e "\033[31mPlease type correct OPTION\033[0m"
echo -e "\033[32mCorrect options are: show, showlast, showolder, delete, last, older\033[0m"
echo -e "\n"
show_usage
fi
}

if [[ $# -lt 3 || $# -gt 4 ]]; then
echo -e "\033[31mWrong number of arguments!!!\033[0m"
show_usage
exit;
elif [ $# -eq 3 ]; then
OPTION=$3
OLDER_THAN=$2
main "$OPTION"
elif [ $# -eq 4 ]; then
main "$OPTION"
fi

[/bash]

2 thoughts on “Skrypt do znajdowania i usuwania plików z zadanym zakresie dat
  • Tomekw pisze:

    Ciekawy skrypt. Proszę na przykładzie jak wpisywać warunek.

    • destine pisze:

      Prosze o to przykłąd uzycia:

      (usuwanie plikow z katalogu /home/user/ za dzien 2013-07-04)
      ./seekanddestroy.sh /home/user 2013-07-04 2013-07-05 delete
      ——————————————–

      (usuwanie plikow z katalogu /home/user/ utworzonych pomiedzy 30 dni temu a aktualna data)
      ./seekanddestroy.sh /home/user 30 -now last
      ——————————————-

      (usuwanie plikow z katalogu /home/user/ starszych niz 30 dni)
      ./seekanddestroy.sh /home/user 30 older

      ponizej wydruk USAGE ze skryptu
      Usage: ./seekanddrstroy.sh EXAMPLE:(Deleting files from /home/user/ for day 2013-07-04) ./seekanddestroy.sh /home/user 2013-07-04 2013-07-05 delete
      Usage2: ./seekanddrstroy.sh
      -now
      EXAMPLE2:(Deleting files from /home/user/ between 30 days ago and present data) ./seekanddestroy.sh /home/user 30 -now last
      OPTIONS: delete – delete files in certain period
      OPTIONS: last – delete files older than X days
      OPTIONS: show – list all found files in certain period
      OPTIONS: showlast – list all found files older than X days
      ATTRIBUT: -now – works only with OPTION show last and last and this attribute determined current date

      Syntax for script with 3 arguments
      Usage: ./seekanddrstroy.sh
      EXAMPLE:(Deleting files from /home/user/ older than 30 days) ./seekanddestroy.sh /home/user 30 older
      OPTIONS: showolder – list all found files older than X days
      OPTIONS: older – delete all found files older than X days

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *


Time limit is exhausted. Please reload CAPTCHA.