How to copy files from one location to another based on a priority?

Hi Gurus,

I am a newbie to shell scripting and I am facing a problem right now.I have to automate the copy of files based on a priority.The scenario is as below:

1) There will be files from Mon-Fri with Mon file being named as abc_def_01_YYYYMMDD and Tue file being abc_def_02_YYYYMMDD and so forth till Friday's file which would be abc_def_YYYYMMDD.

2) I have to set a priority such that if on any given day if i have more than one file, I have to copy the earlier file first.For instance if on Thursday i have monday's file and wednesday's file I have to copy the Monday's file first, then after a pre-defined delay send Wednesday's file-delay- and then send the thursday's file.

Please let me know how could i do it..Thanks in anticipation.

From this I assume that the source files are being removed periodically by something? i.e. you won't have old files hanging around that you've already processed?

Yes Carlo.Thanks for the quick response.The old files are being periodically removed.Once the file is copied is to the target location, they should be deleted.
Further, the file is the trigger to the script.
For some reason if the four files do not come on mon-thu and on fri if all 5 files come at once they should be sent based on the priority described above.
can this be done please?

  1. Is it a typo that Friday's file is missing the 05 i.e. should abc_def_YYYYMMDD above be abc_def_05_YYYYMMDD

  2. if the files are sorted by the YYYYMMDD part of the file name will this do the trick in getting the order right.

Say your filenames are like this:

221_170_01_20131014
rtn_17_02_20131015
trm_01_03_20131016
xxx_yyy_04_20131017
abc_def_05_20131018

You could try doing something like this:

LOOK_DIR=/var/spool/dumpdir
PROC_DIR=/var/spool/ready
WAIT=120

ls $LOOK_DIR | sort -t_ -k 4 | while read file
do
    cp "$LOOK_DIR/$file" "$PROC_DIR/$file"
    sleep $WAIT
done
1 Like

Hi Chubler! Thanks a lot! It worked perfectly!! :slight_smile: :b:
Yes it was a typo.The friday's file shoudl have been abc_def_05_YYYYMMDD.

Can you please explain me:

ls $LOOK_DIR | sort -t_ -k 4 | while read file
do
    cp "$LOOK_DIR/$file" "$PROC_DIR/$file"
    sleep $WAIT
done

Again! Thanks a ton! :slight_smile: :slight_smile: