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
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 11:40 AM
Replace text in multiple files on9west Shell Programming and Scripting 1 05-20-2008 01:35 AM
Find and replace a string in multiple files pharos467 UNIX for Dummies Questions & Answers 2 11-05-2007 11:47 PM
Find and Replace in multiple files (Shell script) jatins_s Shell Programming and Scripting 13 11-05-2007 02:11 PM
How can I replace multiple lines from different files ranga27 Shell Programming and Scripting 2 02-07-2007 08:57 PM

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

Join Date: Sep 2007
Posts: 29
Find and replace files in multiple folders

Hi there,

I would like to write a script to automate the copy and renaming of files in multiple dir.

I have a generic file named s253e.prb and would like to copy this to multiple dir and rename it.

Example:


Dir is AL-M1 and the prb file name is AL-M1.prb. I would like to be able to copy the generic prb file to this dir and rename it AL-M1.prb while backing up the original.

I know that I can use the find command to complete this but I dont know where to start..

The Generic file is kept in the dir S25E3 dir and again we have multiple dirs:

AL-M1
AL-M2
CD-M1

etc

Any starters and help would be grateful to point me in the right dir...

I am using the ksh shell for this
  #2 (permalink)  
Old 09-27-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Do you have a list of the directories where the file is to be copied?

Here's my interpretation of your problem:

Code:
cd /parent/dir/to/all/work/dirs
for dir in AL-M1 AL-M2 CD-M1; do
   cp -p $dir/${dir}.prb $dir/${dir}.prb.backup
   cp -p /path/to/s253e.prb $dir/${dir}.prb
done
  #3 (permalink)  
Old 09-27-2007
lodey lodey is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 29
There is 97 dirs that I need to copy and rename the file to. This is why I would like to try and automate the process as much as possible. Is there a way to use the code submitted above but to wildcard the dir input?
  #4 (permalink)  
Old 09-27-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Are all the 97 directories in the same path? And are there no other directories in that path? If yes, just do:

cd /parent/dir/to/all/work/dirs
for dir in *; do...
  #5 (permalink)  
Old 09-27-2007
lodey lodey is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 29
All the directories are in the smae path ie:

/users/clyons/probes/

One other question (the script above works and thanks for it )

The s25e3.prb file is also contaained within this dir structure. How would I get the scrit to ignore this dir so that an error message is not created
(see attached screen capture)

Again thanks for all your help on this
Attached Thumbnails
find-replace-files-multiple-folders-screen-capture.jpg  
  #6 (permalink)  
Old 09-27-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
It isn't an error, just a warning, but you can ignore this directory. Just shove an if in there.

Code:
cd /parent/dir/to/all/work/dirs
for dir in *; do
   if [ "$dir" = "s25e3" ]; then   #you can replace this with whatever dir you want to ignore
      continue
   fi
   cp -p $dir/${dir}.prb $dir/${dir}.prb.backup
   cp /path/to/s253e.prb $dir/${dir}.prb
done
  #7 (permalink)  
Old 09-28-2007
lodey lodey is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 29
Thanks...it works
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 08:25 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