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 -->
  #4 (permalink)  
Old 07-03-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
Quote:
Originally Posted by cfajohnson View Post

(Code reformatted for legibility)

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.)

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##*/}


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


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


All you need is:


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

I definitely knew I was new to linux scripting, but never realized I was that bad, lol.

Anyways, I think you misunderstood or had a typo, I don't want to add that to my file, I want to remove it.

I have files named:

startServerABC.BEFORE_DISASTER_RECOVERY
or
startServer.sh.BEFORE_DISASTER_RECOVERY

I need to remove the .BEFORE_DISASTER_RECOVERY from the file name, to be left with

startServerABC or startServer.sh