Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-02-2011
jaysunn's Avatar
Registered User
 

Join Date: May 2009
Location: NYC - USA
Posts: 92
Thanks: 21
Thanked 5 Times in 4 Posts
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.


Code:
$ 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.

Code:
#!/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.
Sponsored Links
    #2  
Old 06-02-2011
panyam panyam is offline Forum Advisor  
Registered User
 

Join Date: Sep 2008
Posts: 1,024
Thanks: 4
Thanked 85 Times in 82 Posts
This worked for me


Code:
 

        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
Sponsored Links
    #3  
Old 06-02-2011
jaysunn's Avatar
Registered User
 

Join Date: May 2009
Location: NYC - USA
Posts: 92
Thanks: 21
Thanked 5 Times in 4 Posts
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:

Code:
$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:

Code:
$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
    #4  
Old 06-02-2011
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 100 Times in 97 Posts
See if this works for you:

Code:
#!/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


Last edited by Shell_Life; 06-02-2011 at 02:04 PM..
The Following User Says Thank You to Shell_Life For This Useful Post:
jaysunn (06-03-2011)
Sponsored Links
    #5  
Old 06-03-2011
panyam panyam is offline Forum Advisor  
Registered User
 

Join Date: Sep 2008
Posts: 1,024
Thanks: 4
Thanked 85 Times in 82 Posts
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
The Following User Says Thank You to panyam For This Useful Post:
jaysunn (06-03-2011)
Sponsored Links
    #6  
Old 06-03-2011
jaysunn's Avatar
Registered User
 

Join Date: May 2009
Location: NYC - USA
Posts: 92
Thanks: 21
Thanked 5 Times in 4 Posts
Hello,I now have a working solution based on the examples you have both provided. Thank you very much.


Jaysunn
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
For Loop Range Create Directories jaysunn Shell Programming and Scripting 2 06-02-2011 10:33 AM
Loop through Sub Directories and search for set of files prasannarajesh UNIX for Dummies Questions & Answers 2 02-09-2011 07:57 AM
Loop to move files in different directories acc01 Shell Programming and Scripting 6 10-06-2010 09:41 PM
How to loop through directories to touch files sussane Shell Programming and Scripting 9 12-29-2008 04:25 AM
Script to loop through all files and directories. cheetobandito Shell Programming and Scripting 2 06-19-2008 02:17 PM



All times are GMT -4. The time now is 04:20 AM.