Skip to content

Instantly share code, notes, and snippets.

@louxiu
Created October 24, 2012 13:41
Show Gist options
  • Save louxiu/3946108 to your computer and use it in GitHub Desktop.
Save louxiu/3946108 to your computer and use it in GitHub Desktop.
list new files(on specified directory) added on remote ftp server
#/bin/sh
get_files_list()
{
ftp_db="$1"
lftp ftp.hrbeu.edu.cn <<EOF
set ftp:charset GBK
cls -l --time-style='+%d-%b-%Y' /影视/动漫 > $ftp_db
!echo "------------------------------------------------------------ " >> $ftp_db
cls -l --time-style='+%d-%b-%Y' /影视/电影/标清 >> $ftp_db
!echo "------------------------------------------------------------ " >> $ftp_db
cls -l --time-style='+%d-%b-%Y' /影视/电影/高清 >> $ftp_db
!echo "------------------------------------------------------------ " >> $ftp_db
cls -l --time-style='+%d-%b-%Y' /影视/电影/精选全集 >> $ftp_db
!echo "------------------------------------------------------------ " >> $ftp_db
cls -l --time-style='+%d-%b-%Y' /影视/电影/恐怖片 >> $ftp_db
bye
EOF
}
export LC_ALL="en_US.UTF-8"
ftp_db_old=~/.ftp_db
if [ $# -eq 0 ]
then
if [ -f $ftp_db_old ]
then
ftp_db_temp=`mktemp`
get_files_list $ftp_db_temp
diff -rupN $ftp_db_old $ftp_db_temp | grep "^+" | awk '{print $6 " " $7}'
if [ ! $? -eq 0 ]
then
echo "There is no new movies"
fi
rm $ftp_db_temp
else
echo "The ftp_db must be init firstly"
exit 1
fi
exit 0
fi
# check wether the cache is outdate or not
# if the cache is not updated today, then it is outdate
# if the cache is updated then use the local one or update before use
today=$(date --rfc-3339=date)
file_date=$(stat .ftp_db | awk '{if ($1 == "Modify:" )print $2}')
if [ $today != $file_date ]
then
get_files_list $ftp_db_old
fi
while getopts ":i u d a m s:" ARGUMENT 2>/dev/null
do
case $ARGUMENT in
i)
# init local list cache
if [ -f $ftp_db_old ]
then
echo "The ftp_db has already be inited!"
exit 1
else
get_files_list $ftp_db_old
echo "init the ftp_db done"
fi
;;
u)
# forcus to update local lists cache
get_files_list $ftp_db_old
echo "update the ftp_db done"
;;
s)
# search files by name
echo "Search $OPTARG result:"
grep "$OPTARG" $ftp_db_old
;;
d)
# list new files uploaded today
dyear=`date | awk '{print $6}'`
dmonth=`date | awk '{print $2}'`
dday=`date | awk '{print $3}'`
key="${dday}-${dmonth}-${dyear}"
# grep "$dday-$dmonth-$dyear" $ftp_db_temp | awk '{print $7}'
awk -v akey=${key} '{ if ($6 == akey ) print $6 $7 }' $ftp_db_old
;;
m)
# list new files uploaded this month
dyear=`date | awk '{print $6}'`
dmonth=`date | awk '{print $2}'`
# grep "$dmonth-$dyear" $ftp_db_old | awk '{print $7}'
awk '{print $6 "-" $7}' $ftp_db_old | \
awk -v ayear=${dyear} -v amonth=${dmonth} \
' BEGIN {FS="-"} {if($2 == amonth && $3 == ayear) print $4}'
;;
a)
# list all in cache
if [ -f $ftp_db_old ]
then
cat $ftp_db_old
else
echo "There is no cache file!"
exit 1
fi
;;
\?)
echo "invild paramters"
;;
esac
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment