For loop find statement file name manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop find statement file name manipulation
# 1  
Old 07-03-2009
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  
Old 07-03-2009
what do you mean by $filename "minus"??
is it mv test.txt test-.txt??
# 3  
Old 07-03-2009
Quote:
Originally Posted by cbo0485

(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  
Old 07-03-2009
Quote:
Originally Posted by cfajohnson

(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  
Old 07-03-2009
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  
Old 07-06-2009
Quote:
Originally Posted by vidyadhar85
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

0403-016 Cannot find or open the file in If Statement

Hi, I am comparing the number of records in the .bad record from sql loader, if it is higher than the number passed in as an argument, the program should exit. However, I constantly receive the error " 0403-016 Cannot find or open the file." on the line with the if statement "elif ;". The code... (3 Replies)
Discussion started by: MIA651
3 Replies

2. Shell Programming and Scripting

Loop multiple directory, find a file and send email

Hello ALL, need a BASH script who find file and send email with attachment. I have 50 folders without sub directories in each generated files of different sizes but with a similar name Rp01.txt Rp02.txt Rp03.txt ...etc. Each directors bound by mail group, I need a script that goes as... (1 Reply)
Discussion started by: penchev
1 Replies

3. Shell Programming and Scripting

While loop within if statement

Hi, I'm a rookie who is trying to learn this stuff. What I need help with is putting together a non complicated "while" loop within the below "if" statement. I also need the while loop to keep looping until the user types a key to end the loop. Please reveal the proper insertion points. Thank... (4 Replies)
Discussion started by: jefferj54
4 Replies

4. Shell Programming and Scripting

awk if statement in a for loop?

I need to match multiple values in a single column in a file: example: Source file: abd,123,one def,232,two ghi,987,six Target file: 12345,abcde,123 09876,zxvyr,566 56789,lmnop,232 Variable: var1=`grep 2 sourcefile | awk '{print$1}' essentially, echo "$var1" would read:... (2 Replies)
Discussion started by: kopfgeldjagar
2 Replies

5. UNIX for Dummies Questions & Answers

Statement to find if an entry exists in a file

I need to check if an entry input by the user is in a file. If so, I need to run a command, and if it does not exist then it should output entry does not exist. So I have so far... echo "Enter record:" read record //command || //command Can I use an if statement to do this? (3 Replies)
Discussion started by: itech4814
3 Replies

6. Shell Programming and Scripting

how to create a loop in an if statement

Hey guys, a=`cat abc | wc -l` b=`cat def | wc -l` if $a== $b then echo "a" else echo "b" fi I want the if condition to retry itself , untill a==b. I can't use goto statemt. Please help. Thanx in advance. Please use next time code tags for your code and data (5 Replies)
Discussion started by: jaituteja
5 Replies

7. UNIX for Dummies Questions & Answers

For loop using find with file name spaces

Hello All, This question is actually for the service console of VMware ESX 3.5 but is relevant to this forum I think. I have been advised to use the following commands: for i in `find /vmfs/volumes/Test_VMFS/ -name "*.vmx"` do echo "$i" #sed -i 's/scsi1:0.present =... (3 Replies)
Discussion started by: mronsman
3 Replies

8. Shell Programming and Scripting

For loop statement - catch error

I'm having a question about for loops. (bash) I have the following for example: for file in `ls *.txt` do read file ... done Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such... (4 Replies)
Discussion started by: lumdev
4 Replies

9. UNIX for Dummies Questions & Answers

if statement in a while loop

#!/usr/bin/ksh echo Please enter while read n do echo $n >> datafile done question: How can I enject an if statement that if the users enter 0 (zero) the program will exit? this is what I have but not working #!/usr/bin/ksh echo Please enter number while read n do if $n=0 then... (2 Replies)
Discussion started by: bobo
2 Replies

10. UNIX for Dummies Questions & Answers

if statement in for loop of a string

I am attempting to pass a string into awk and loop through it, and then for every occurrance of a certain character perform an action. In this case, for example, echo 1 for each time character r is found in the string. Except I can't get it to work. Could someone please tell me why? echo... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question