loop directories mv files to target in range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop directories mv files to target in range
# 1  
Old 06-02-2011
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.
# 2  
Old 06-02-2011
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
# 3  
Old 06-02-2011
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
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 03:04 PM..
This User Gave Thanks to Shell_Life For This Post:
# 5  
Old 06-03-2011
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
This User Gave Thanks to panyam For This Post:
# 6  
Old 06-03-2011
Hello,I now have a working solution based on the examples you have both provided. Thank you very much.


Jaysunn
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Move several files into specific directories with a loop

Hello, I'm a first time poster looking for help in scripting a task in my daily routine. I am new in unix but i am attracted to its use as a mac user. Bear with me... I have several files (20) that I manually drag via the mouse into several named directories over a network. I've used rsync... (14 Replies)
Discussion started by: SonnyClark
14 Replies

2. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

3. UNIX for Dummies Questions & Answers

Loop over certain user directories and find files

Hello I have user directories that contain /temp directory. Example folders: /user1/temp/ /user2/temp/ /user3/temp/ How can i loop over all user directories and find all files only in their /temp folder? Thanks a lot for help! (3 Replies)
Discussion started by: flavius42
3 Replies

4. Shell Programming and Scripting

Need shell script to compare directories and delete files on target server

Hello, I need help in writing the shell script for below mentioned case. There are 2 servers(server A, server B). A cronjob syncs files between these 2 servers. Existing script is copying files from A to B. This is done using the command rsync. However, the files are not deleted... (2 Replies)
Discussion started by: SravaniVedam11
2 Replies

5. Shell Programming and Scripting

For Loop Range Create Directories

Hello, I am a bit stumped on this. I am attempting to create 24 empty directories with a loop. Seems like I have incorrect syntax. When I run the following command I get the error below. Command $ for i in {2..24}; do mkdir $i_MAY_2011 ; doneError x 24 mkdir: missing operand Try `mkdir... (2 Replies)
Discussion started by: jaysunn
2 Replies

6. UNIX for Dummies Questions & Answers

Loop through Sub Directories and search for set of files

I have the below directory in unix environment /home/bkup/daily: ls -lrt drwxrwx--x 2 user user 256 Jan 12 18:21 20110112/ drwxrwx--x 2 user user 256 Jan 13 17:06 20110113/ drwxrwx--x 2 user user 256 Jan 14 16:44 20110114/ drwxrwx--x 2 user user ... (2 Replies)
Discussion started by: prasannarajesh
2 Replies

7. Shell Programming and Scripting

Loop to move files in different directories

Hi, I have various log files in different paths. e.g. a/b/c/d/e/server.log a/b/c/d/f/server.log a/b/c/d/g/server.log a/b/c/h/e/server.log a/b/c/h/f/server.log a/b/c/h/g/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log and above these have an archive folder... (6 Replies)
Discussion started by: acc01
6 Replies

8. Shell Programming and Scripting

Sometimes my until loop misses it's target

Hello all, I wrote a quick function (alarm) in my bash_profile (in cygwin) for practice. It uses until loops to wait for a specified time, and once that time passes triggers a play on a mp3. Most of the time it works, however sometimes it seems like it's looping through too slowly and will miss... (1 Reply)
Discussion started by: DeCoTwc
1 Replies

9. Shell Programming and Scripting

How to loop through directories to touch files

Hi, Please help me on this. Suppose i have the following directory structure. /app/data /app/data/eng /app/data/med /app/data/bsc each of the directories data,data/eng,data/med,data/bsc holds files with date extension like a.20081230 b.20081230 and so on I need a script to loop... (9 Replies)
Discussion started by: sussane
9 Replies

10. Shell Programming and Scripting

Script to loop through all files and directories.

I'm trying to write a script that will loop through all files and directories down from a path I give it, and change the permissions and ACL. I was able to do the obvious way and change the files and folders on the same level as teh path...but I need it to continue on deeper into the file... (2 Replies)
Discussion started by: cheetobandito
2 Replies
Login or Register to Ask a Question