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
For loop using find with file name spaces mronsman UNIX for Dummies Questions & Answers 3 09-12-2008 08:23 AM
Variable problem in for loop with if statement ejdv Shell Programming and Scripting 6 06-17-2008 08:52 AM
For loop statement - catch error lumdev Shell Programming and Scripting 4 09-20-2007 07:50 AM
if statement in a while loop bobo UNIX for Dummies Questions & Answers 2 11-07-2006 12:38 PM
if statement in for loop of a string Sniper Pixie UNIX for Dummies Questions & Answers 7 03-02-2006 07:28 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-03-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
For loop find statement file name manipulation

Code:
for i in `find . -name "*.BEFORE_DISASTER_RECOVERY"`;do dir_name=`dirname $i`;file_name=`basename $i`;cd $dir_name;mv $file_name (STUCK HERE) ;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.
  #2 (permalink)  
Old 07-03-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,382
what do you mean by $filename "minus"??
is it mv test.txt test-.txt??
  #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,309
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
  #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
  #5 (permalink)  
Old 07-03-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,382
then use the basename only
Code:
basename "$filename" .BEFORE_DISASTER_RECOVERY
home> basename startServer.sh.BEFORE_DISASTER_RECOVERY .BEFORE_DISASTER_RECOVERY
startServer.sh
home>  basename startServerABC.BEFORE_DISASTER_RECOVERY .BEFORE_DISASTER_RECOVERY
startServerABC
I think you got my point...
  #6 (permalink)  
Old 07-06-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
Quote:
Originally Posted by vidyadhar85 View Post
then use the basename only
Code:
basename "$filename" .BEFORE_DISASTER_RECOVERY
home> basename startServer.sh.BEFORE_DISASTER_RECOVERY .BEFORE_DISASTER_RECOVERY
startServer.sh
home>  basename startServerABC.BEFORE_DISASTER_RECOVERY .BEFORE_DISASTER_RECOVERY
startServerABC
I think you got my point...
This works perfectly inside my loop.

Thanks
Code:
for i in `find . -name "*.BEFORE_DISASTER_RECOVERY"`;do dir_name=`dirname $i`;file_name=`basename $i`;cd $dir_name;cp $file_name `basename $file_name .BEFORE_DISASTER_RECOVERY`;pwd;cd $BASE_DIR;done
Sponsored Links
Reply

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 01:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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