|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
doneany help would be greatly appreciated. |
| Sponsored Links | |
|
|
|
#2
|
|||
|
|||
|
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`
doneRegards Ravi |
| Sponsored Links | ||
|
|
|
#3
|
||||
|
||||
|
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_2011So basically I run the 2 command sets 24 times. I will do automate this process. jaysunn |
|
#4
|
||||
|
||||
|
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
doneLast 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
|
|||
|
|||
|
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
|
||||
|
||||
|
Hello,I now have a working solution based on the examples you have both provided. Thank you very much.
Jaysunn |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|