#!/bin/bash DEF_SOURCE=/media/H6_SD/FOLDER01/ DEF_TARGET=/media/ramdisk/ DEF_FTYPE=.WAV DEF_IFS=$IFS SOURCE=$DEF_SOURCE TARGET=$DEF_TARGET FTYPE=$DEF_FTYPE TMP_FLIST= LIST=empty typeset -i TMP_COUNT=0 typeset -i FAIL=0 function ERROR() # displays error message and explains how to operate this script { printf "\033[0;31mError! $1\033[0;32m\n\n" cat < ..... Set file type (default is $DEF_FTYPE) -s .......... Set source directory (default is $DEF_SOURCE) -t .......... Set target directory (default is $DEF_TARGET) -y ... Filter by year (must be written exactly as in the file list) -m ... Filter by month -d ... Filter by day -h ... Filter by hour -n ... Filter by file number If you wish to change file type or source directory, the -e and -s options must be the first of all options. When setting a file extension, consider that everything is case sensitive. Each filter option must be followed by either a single number, several numbers separated by comma or a range of numbers specified by the first and the last number divided by a "-". And remember: If you intend to filter by year, the value must be written exactly as given in the file list (i.e. if year ist 2010 then write 2010 and not just 10 instead). You can combine all the filter options at discretion. Using no filter will of course bring up the entire file list. Beware: When copying files, all existing files with same names in the target folder will be overwritten without request! EOF printf "\033[0m\n" exit 1 } function CHECK_MATCH() # checks if current file specs match with filter settings and calls BUILD_LIST() function { typeset -i NUM=$(echo "obase=10;$1" | bc) if [ $NUM -ge $NUM_LO ] && [ $NUM -le $NUM_HI ]; then BUILD_LIST; fi } function BUILD_LIST() # builds file lists, skips existing lines and counts gathered entries { NEW_LIST=$(printf "$NEW_LIST")"\n"$LINE TMP_LINE=$(printf "/$FILE\t time stamp: $YEAR $MONTH $DAY $HOUR (year, month, day, hour)") PREV_IFS=$IFS IFS=$'\n' for SEG in $(printf "$TMP_FLIST"); do if [ "$TMP_LINE" = "$SEG" ]; then TMP_LINE=; fi done IFS=$PREV_IFS if [ ! -z "$TMP_LINE" ]; then TMP_FLIST=$(printf "$TMP_FLIST")"\n"$TMP_LINE TMP_COUNT=$TMP_COUNT+1 fi } function FILTER() # reads and filters the files according to the filter options set by the user { if [ "$LIST" = empty ]; then if [ ! -d "$SOURCE" ]; then ERROR "Source directory $SOURCE does not exist."; fi for PROJECT in $(ls "$SOURCE"); do if [ -d "$SOURCE$PROJECT" ]; then if ls "$SOURCE$PROJECT"/*$FTYPE >/dev/null 2>/dev/null; then if [ "$LIST" = empty ]; then LIST=; fi LIST=$(printf "$LIST")"\n"$(ls -g --time-style=long-iso "$SOURCE$PROJECT"/*$FTYPE) fi fi done fi IFS="," for VAL in $1; do typeset -i NUM_LO=${VAL%-*} typeset -i NUM_HI=${VAL#*-} IFS=$'\n' for LINE in $(printf %b "$LIST"); do if [ ! -z "$LINE" ]; then DATE=$(echo $LINE | awk {'print $5'}) YEAR=${DATE%-*-*} MONTH=${DATE%-*} MONTH=${MONTH#*-} DAY=${DATE#*-*-} DAY=${DAY%\ *} TIME=$(echo $LINE | awk {'print $6'}) HOUR=${TIME%:*} FILE=${LINE#*\ /} PNAME=${FILE%/*} FRONT=${FILE#$PNAME/ZOOM} REAR=${FRONT#????} if [ ! $1 = nothing ]; then if [ $2 = h ]; then CHECK_MATCH $HOUR; fi if [ $2 = d ]; then CHECK_MATCH $DAY; fi if [ $2 = m ]; then CHECK_MATCH $MONTH; fi if [ $2 = y ]; then CHECK_MATCH $YEAR; fi if [ $2 = n ]; then CHECK_MATCH ${FRONT%$REAR}; fi else BUILD_LIST fi fi done IFS="," done IFS=$DEF_IFS LIST=$(printf "$NEW_LIST") NEW_LIST= FLIST=$TMP_FLIST TMP_FLIST= COUNT=$TMP_COUNT TMP_COUNT=0 } while getopts :e:s:t:h:d:m:y:n: OPT; do case $OPT in e) if [ ! -z "$LINE" ]; then ERROR "-e must be specifyed BEFORE any filter option."; fi FTYPE=$OPTARG if [[ ! $FTYPE = .* ]]; then FTYPE=.$FTYPE; fi ;; s) if [ ! -z "$LINE" ]; then ERROR "-s must be specifyed BEFORE any filter option."; fi SOURCE="$OPTARG" if [[ ! "$SOURCE" = */ ]]; then SOURCE="$SOURCE"/; fi ;; t) if [ ! -d "$OPTARG" ]; then ERROR "Target directory $OPTARG does not exist."; fi TARGET="$OPTARG" ;; h) FILTER $OPTARG h ;; d) FILTER $OPTARG d ;; m) FILTER $OPTARG m ;; y) FILTER $OPTARG y ;; n) FILTER $OPTARG n ;; :) ERROR $OPTARG" requires an argument." ;; ?) ERROR "Unknown option: "$OPTARG ;; esac done if [ -z "$LINE" ]; then FILTER nothing; fi if [ -z "$FLIST" ]; then ERROR "$SOURCE doesn't contain any useful data."; fi printf "$FLIST\n\n" read -p $COUNT" files selected. Copy these files to $TARGET (y/N)? " YN case $YN in y | Y | yes | Yes | YES) IFS=$'\n' typeset -i COUNT=0 echo for PROCESS in $(printf "$FLIST"); do FILE_PATH=${PROCESS%$'\t'" time stamp:"*} if cp -v --preserve=timestamps $FILE_PATH $TARGET; then COUNT=$COUNT+1 else FAIL=$FAIL+1 fi done IFS=$DEF_IFS echo && echo "Done - "$COUNT" files copied, "$FAIL" failed." ;; ?) ;; esac