1752479 Members
5761 Online
108788 Solutions
New Discussion

Re: script help

 
javedk
Advisor

script help

I have around 20 directories dirA,dirB.... and there are 3 to 4 subdirectories subdirA, subdirB ...in each of these 20 directories. I have files of last month and the months before in these subdirectories. I would like to move these files for archival purposes by month to another directory in the the format /archive/dirA/subdirA/Feb2010 . Could someone guide me on how to do this.

I have been doing this manually as below
cd to DirA and then use the below command
for i in `ls -l |grep Feb|awk '{print $9}'`
do
mkdir -p /archive/dirA/subdirA/Feb2010
mv $i /archive/dirA/subdirA/Feb2010
done

This is taking quite a long time.
1 REPLY 1
James R. Ferguson
Acclaimed Contributor

Re: script help

Hi:

You could try this (untested):

# cat ./archiveit
#!/usr/bin/sh
for DIR in DirA DirB DirC #...etc...
do
cd ${DIR} || exit 1
for SUBDIR in subdirA surdirB subdirC #...etc
do
cd ${SUBDIR} || continue #...skip if missing...
mkdir -p /archive/${DIR}/${SUBDIR}/Feb2010
for FILE in $(ls -l|awk '{if ($6=="Feb") {print $9}}')
do
mv ${FILE} /archive/${DIR}/${SUBDIR}/Feb2010
done
cd .. #...back out of current SUBDIR...
done
cd .. #...backup out of current DIR...
done
exit

...
Regards!

...JRF...