loop directories mv files to target in range

Hello,
Currently I have a painstaking process that I use to move file for a monthly archive. I have to run the same two commands for 24 different directories. I wish to have a script with a for loop automate this and I have not been able to succeed. Here is what I do 24 times. I know this is possible with a range of some how.

$ FILES=`find /mnt/raw_vendor_data/raw_1_hist/primary/ -type f | xargs ls -l | grep May | awk '{print $NF}'`

$ mv $FILES /mnt/raw_vendor_data/raw_data_archive/RAW_1_MAY_2011/

My Failed Attempt.

#!/bin/bash

for i in {1..24} ; do 
FILES=`find /mnt/raw_vendor_data/raw_${i}_hist/primary/ -type f | xargs ls -l | grep May | awk '{print $NF}'`

mv $FILES /mnt/raw_vendor_data/raw_data_archive/RAW_${i}_MAY_2011

done

any help would be greatly appreciated.

This worked for me

 

        i=1
        j=24
        while [ $i -le $j ];
        do
        FILES=`find . -name tem"$i" -type f | xargs ls -l | grep Jun  | awk '{print $NF}'`
         mv $FILES ../
        i=`expr $i + 1`
        done

Regards
Ravi

Hello,
Maybe I am not understanding your code correctly.
The files that I wish to move live in separate directories and the target directories are separate as well. That find command you have posted seems to just search . current working directory, it does not iterate through all of them from what I can tell. Also it looks like it is moving the files ../ back one directory.

Each time I run the command that I am using I need to increment the integer.

Command Set 1:

$FILES=`find /mnt/raw_vendor_data/raw_1_hist/primary/ -type f | xargs ls -l | grep May | awk '{print $NF}'`

$mv $FILES /mnt/raw_vendor_data/raw_data_archive/RAW_1_MAY_2011/

Command Set 2:

$FILES=`find /mnt/raw_vendor_data/raw_2_hist/primary/ -type f | xargs ls -l | grep May | awk '{print $NF}'`

$mv $FILES /mnt/raw_vendor_data/raw_data_archive/RAW_2_MAY_2011

So basically I run the 2 command sets 24 times. I will do automate this process.

jaysunn

See if this works for you:

#!/usr/bin/ksh
typeset -i mNbr=1
while [[ ${mNbr} -le 24 ]]
do
  mDirA="/mnt/raw_vendor_data/raw_${mNbr}_hist/primary/"
  mDirB="/mnt/raw_vendor_data/raw_data_archive/RAW_${mNbr}_MAY_2011"
  for mFName in $(find ${mDirA} -type f | xargs ls -l | grep May | awk '{print $NF}')
  do
    echo "Now moving <${mFName}>..."
    mv ${mFName} ${mDirB}
  done
  mNbr=${mNbr}+1
done
1 Like

Hello Jaysun,

I posted a sample script to show how to increment the variable and use it in find. You might need to adopt the same technique and do the changes accordingly.

Regards
Ravi

1 Like

Hello,I now have a working solution based on the examples you have both provided. Thank you very much.

Jaysunn