The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Moving files not directory. senthil_is Shell Programming and Scripting 1 05-09-2008 01:21 AM
Getting unusual error - moving files to another directory aismann Shell Programming and Scripting 4 12-21-2007 03:29 AM
How to Compress files before moving them in a directory godalle Shell Programming and Scripting 3 11-10-2005 11:59 AM
moving files from a unix directory to a windows directory gleads UNIX for Dummies Questions & Answers 2 08-29-2002 08:42 PM
Report with mutiple files. Cameron Shell Programming and Scripting 4 01-28-2002 08:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-20-2007
tekster757 tekster757 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 33
Mutiple For loops - moving files to another directory

I need to clean out some application subdirectories from backup scripts we used to rename to various backup extensions just in case the script failed in production and we need to rollback. I will be moving these old scripts to a staging directory and then removing them after 30 days (I have the find command for removing scripts after so many days).

The subdirectories would be application directories in a specified place with another subdirectory that would contain the scripts, both of which I have to loop through, they will look like this: /dir/dir/*/scripts (the ‘*’ is the application subdirectory that I will have to loop through, like /dir/dir/finance/scripts or /dir/dir/admin/scripts for example – there are around 15 of them or so). So I have to loop through the application directories, go into their scripts directory, and loop through the list of files to find any backup files and move them to a new staging directory like (/dir/dir/staging).

These scripts could have one of a variety of possible extensions: kshO, .bkp, .bkup, .OLD, or _OLD
The environment is KSH.

My knowledge of Unix at this point is limited to simple loops, so I’m not sure how to do 2 loops in 2 different places yet - could it be as simple as nesting a for loop in another for loop?
  #2 (permalink)  
Old 06-20-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Does this work for you:
Code:
mDirs="/dir/dir/*/"
for mFile in ${mDirs}.[kshO|bkp|bkup|OLD|_OLD]
do
  echo "mFile = "$mFile
done
  #3 (permalink)  
Old 06-20-2007
tekster757 tekster757 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 33
Mutiple For loops - moving files to another directory

I tried it as you stated and it came back with a syntax error of '|' unexpected. I then tried it with ||, and [[ ]]. Using your logic, I will research some more. If you have any other ideas, I'm appreciative!
  #4 (permalink)  
Old 06-20-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,748
You have to be in a POSIX shell to have that work - like bash or ksh.
  #5 (permalink)  
Old 06-20-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Sorry Tekster -- here is another version:
Code:
mDirs="/dir/dir/*/*"
for mFile in ${mDirs}
do
  mExt=`echo $mFile | sed 's/.*\.//'`
  case ${mExt} in
    kshO|bkp|bkup|OLD|_OLD) echo "mFile = "${mFile};;
  esac
done
  #6 (permalink)  
Old 06-20-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,302
With find:

Code:
find /dir/dir \( -name "*.kshO" -or -name "*.bkp" -or -name "*.bkup" -or -name "*.OLD" -or -name "*._OLD" \) -exec mv {} /dir/dir/staging \;
Regards
  #7 (permalink)  
Old 06-20-2007
tekster757 tekster757 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 33
Mutiple For loops - moving files to another directory

Hey ShellLife,

That last one seemed to work better, I got some results back, but not quite as I expected, but getting closer ! At some point, I will have to exchange the last '*' for the directory "scripts" because I will be searching only the scripts folder in each application directory (/dir/dir/*/scripts), but for now, I'm just trying to work with the code you're suggesting. Funny, when I tried it several more times, I didn't get anything back (I checked my syntax over and over again). At any rate, this is what it gave me:

ksh[3]: mExt/dir/dir/admin/data: not found
ksh[3]: mExt/dir/dir/admin/scripts: not found
ksh[3]: mExt/dir/dir/finance/data: not found
ksh[3]: mExt/dir/dir/finance/scripts: not found

So it seems to be finding the folders, but not the stuff listed in the case statement, and it seems to be tacking on mExt to the end. I like the idea of the case statement though, it seems to be helping. I'll keep playing with it...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0