Shell script to sort & mv file based on date

July 19, 2011 at 03:30:47
Specs: HP-UX
Im new to unix, I have search a lot of info but still don not how to make it in a bash script.

What i know is used this command ls -tr|xargs -i ksh -c "mv {} ../tmp/" to move file by file.

Now I need to make a script that sorts all of these files by date and
moves them into a directory, (The first 1000 oldest files being to be moved.)

Example files r like these

KPK.AWQ07102011.66.6708.01
KPK.AWQ07102011.68.6708.01
KPK.EER07102011.561.8312.13
KPK.WWS07102011.806.3287.13

KPK_<FILE>DDMMYYYY.<SEQUENCE1>.<SEQUENCE2>.<SEQUENCE3>

alwsys sort the files base on <SEQUENCE1>

Anyone can gv me hand ?.im rushing this


See More: Shell script to sort & mv file based on date

Report •


#1
July 19, 2011 at 09:38:14
Assume you are in the directory where the files reside, this script performs an ls -1 one object per line, and then uses the sort command where as the field seperator is a period, the sequence1 number is the third field; -n forces a numeric sort.

Pipe that to a while loop where each file object is operated on up to 1000 objects:

#!/bin/ksh

# untested
x=0
ls -1|sort -t . -n -k 3,3|while read file
do
   while [ $x -lt 1000 ]
   do
      ((x+=1))
      # add your move command here
   done
done


Report •
Related Solutions


Ask Question