sort retrieved data files in different dirs

Hi,

I have a script to retrieve data files from server and store them in a directory on local disk. The script runs everyday as cron job. But now those files are too many so my boss wants me to put the files into different directories base on dates.

Those files look like this:

FCNCP104500104_SPA_2011-04-08_04-53-56-GMT_P08-00.nar

My original script:

#! /bin/bash

EMC_HOST='3.3.3.3'
EMC_USER='admin'
EMC_PASSWORD='password'
NAV_LOG_FILE='/var/log/naviseccli'
NAVISECCLI='/opt/Navisphere/bin/naviseccli'
NAV_BASE_CMD="${NAVISECCLI} -h ${EMC_HOST} -user ${EMC_USER} -password ${EMC_PASSWORD} -scope 0"
RETRIEVE_DIR='/mnt/emc_data'

#
# Simple logger.
#
log () {
    echo ${1} >> ${NAV_LOG_FILE}
}

#
# Retrieve file.
#
retrieve () {
    log "Retrieve file ${1}..."
    cmd="$NAV_BASE_CMD analyzer -archive -file ${1} -o"
    eval $cmd
    if [ $? -ne 0 ] ; then
        log "$(date): Retrieve $1 failed"
        log "command: $cmd"
        log "error code: $?"
        exit -1
    fi
}

#
# List all archives on EMC.
#
list () {
    file_list=$($NAV_BASE_CMD analyzer -archive -list | awk 'BEGIN {IGNORECASE=1;} /nar$/ { print $NF }')
    if [ $? -ne 0 ] ; then
        log "$(date): list archives failed"
        log "error code: $?"
        exit -2
    fi
}

cd $RETRIEVE_DIR || exit -1

list
for f in ${file_list} ; do
    [ -f ${f} ] || retrieve ${f}
done

Any suggestions to improve (rewrite) the script? :smiley:

adding a

mkdir `date'+%Y-%m-%'`

and chmod it to 777 at beginning of script
then change the retrieve directory to this new directory

Thanks for your reply, pkabali.

But the list function will list all files available on the server, that way would retrieve all files into each date '+%Y-%m-%' directory. It's hard to determine which file is already retrieved.

Based on file names with the format:

FCNCP104500104_SPA_2011-04-08_04-53-56-GMT_P08-00.nar

The following code will extract "YYYYMMDD" from it and create a directory with the same name, if it does not already exists:

  mYYYYMMDD=$(echo ${mFname} | sed 's/.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*/\1/;s/-//g')
  if [[ ! -d ${mYYYYMMDD} ]]; then
    mkdir ${mYYYYMMDD}
  fi
1 Like

Thanks, Shell_Life.

I used awk, but your way worked, too.