The UNIX and Linux Forums  


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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 07-03-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,372
Quote:
Originally Posted by cbo0485 View Post

(Code reformatted for legibility)
Quote:

Code:
for i in `find . -name "*.BEFORE_DISASTER_RECOVERY"`

That will fail if any filenames contain spaces. Pipe the output of find into a loop:


Code:
find . -name "*.BEFORE_DISASTER_RECOVERY" |
while read file
do
  : do whatever
done

(And your script will be more legible if you use a meaningful variable name for the loop.)
Quote:

Code:
do
  dir_name=`dirname $i`
  file_name=`basename $i`

There is no need for either external command, dirname or basename. The Unix shell can do it internally:


Code:
dir_name=${file%/*}
file_name=${file##*/}

Quote:
Code:
  cd $dir_name
  mv $file_name # (STUCK HERE)

Both the cd and mv will fail if $dir_name contains spaces. Quote variable references:


Code:
  cd "$dir_name"
  mv "$file_name"

Quote:
Code:
  pwd
  cd $BASE_DIR
done

Okay, so I was able to get to this point. As you can see, I have a small for loop that searches for any files with the string BEFORE_DISASTER_RECOVERY in the file name, it then sets two variables dir_name and file_name, cd's to the dir_name directory, and then this is where I'm stuck. I need to mv $file_name to $filename minus ".BEFORE_DISASTER_RECOVERY.

I'm sure it's something simple, but this is where I'm currently stuck.

All you need is:


Code:
find . -name "*.BEFORE_DISASTER_RECOVERY" |
while read file
do
  mv "$file" "$file%.BEFORE_DISASTER_RECOVERY}"
done