Register
It is currently Fri Aug 01, 2014 6:22 pm

Directory Rename


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Nov 05, 2007 10:15 am   

Joined: Mon Nov 05, 2007 10:07 am
Posts: 2
I'm new to Bash scripting and need to do something that I think should be relatively easy... please help

basically I need to read through a directory and for each sub directory that contains a set character pattern I need to remove it... (basically I am renaming links to LUN's after they have been SnapMirrored with a NetApp Filer)

For example the directory may contain four subdirectories:

/vmfs/volumes/snapshot001~LUN1
/vmfs/volumes/snapshot004~LUN2
/vmfs/volumes/snapshot006~LUN3
/vmfs/volumes/snapshot002~LUN4

I need the script to read the subdir's and split at the ~ renaming the subdirs


/vmfs/volumes/LUN1
/vmfs/volumes/LUN2
/vmfs/volumes/LUN3
/vmfs/volumes/LUN4

Thanks in advance for your help - PS this is not homework.

Lardspice


Top
 Profile  
 PostPosted: Wed Nov 07, 2007 6:41 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
the following should work until you have too many snapshots to rename
i couldn't be bothered to setup a test but it's such a simple script it should be fine :)

Also I somewhat doubt that someone doing homework will have access to a snapmirror capable filer lol

#!/bin/bash

cd /vmfs/volumes
for ifile in $(ls -d snapshot*LUN*)
do
newfilename=$(echo $ifile | awk -F~ '{ print $2 }')
mv $ifile $newfilename
done


Top
 Profile  
 PostPosted: Mon Nov 12, 2007 8:48 am   

Joined: Mon Nov 05, 2007 10:07 am
Posts: 2
Thanks for that.. I've not had chance to test it yet but will... wait for it... give it a BASH ASAP...(see what I did there... bet nobody has ever cracked that one before?)


Top
 Profile  
 PostPosted: Mon Nov 12, 2007 11:09 am   

Joined: Wed Jul 25, 2007 11:45 am
Posts: 11
DarthWavy wrote:
the following should work until you have too many snapshots to rename
i couldn't be bothered to setup a test but it's such a simple script it should be fine :)

Also I somewhat doubt that someone doing homework will have access to a snapmirror capable filer lol

#!/bin/bash

cd /vmfs/volumes
for ifile in $(ls -d snapshot*LUN*)
do
newfilename=$(echo $ifile | awk -F~ '{ print $2 }')
mv $ifile $newfilename
done


If you change $(ls -d snapshot*LUN*) to snaphost*LUN* and add a [ ! -d "$ifile" ] && continue you should be able to avoid the "too many argument" error from ls (or the shell).

Code:
#!/bin/bash

cd /vmfs/volumes
for ifile in snapshot*LUN* ; do
  [ ! -d "$ifile" ] && continue
  newfilename=$(echo $ifile | awk -F~ '{ print $2 }')
  mv $ifile $newfilename
done


Top
 Profile  
 PostPosted: Tue Nov 13, 2007 8:34 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
slacker_nl wrote:

If you change $(ls -d snapshot*LUN*) to snaphost*LUN* and add a [ ! -d "$ifile" ] && continue you should be able to avoid the "too many argument" error from ls (or the shell).



I'm interested, why doesn't your method suffer from the same thing? I thought that the glob would expand to all matching files.


Top
 Profile  
 PostPosted: Fri Feb 08, 2008 7:58 am   

Joined: Tue Feb 05, 2008 8:24 am
Posts: 14
slacker_nl wrote:
Code:
#!/bin/bash

cd /vmfs/volumes
for ifile in snapshot*LUN* ; do
  [ ! -d "$ifile" ] && continue
  newfilename=$(echo $ifile | awk -F~ '{ print $2 }')
  mv $ifile $newfilename
done


I suppose, if there are no files named like snapshot~LUN*, it may be written even shorter, using bash parameter expansion:
Code:
cd /vmfs/volumes && for d in snapshot*~LUN* ; do mv ${d} ${d/snapshot*~/} ; done

of course, “[ ! -d "$ifile" ] && continue;â€￾ may be added if there are files with similar names.

Yet, it will nor work if there are spaces in some of the directory names. In this case I cannot think of something better than
Code:
# create temporary renaming script
printf '#!/bin/bash\nf=$1\nmv ${f} ${f/snapshot*~/}\n' > ./mymv && chmod a+x ./mymv
# rename
find /vmfs/volumes -type d -name 'snapshot*LUN*' -exec ./mymv '{}' \;
# delete temporary renaming script
rm ./mymv ;


--
UseFreeTools — my blog about GNU/Linux and Free Software


Top
 Profile WWW  
 PostPosted: Mon Feb 18, 2008 10:03 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
jetxee wrote:
Yet, it will nor work if there are spaces in some of the directory names.


This is indeed true, the too many files issue and spaces issue can be solved like so...

Code:

#!/bin/bash

cat <<EOF > /tmp/movesnaps
#!/bin/bash
myfilename="\$@"
mynewfilename=\$(echo \$myfilename | awk -F~ '{ print \$2 }')
mv "\$myfilename" "\$mynewfilename"
EOF

chmod +x /tmp/movesnaps

find /vmfs/volumes -depth -type d -name \*snapshot\*LUN\* -exec /tmp/movesnaps {} \;

rm /tmp/movesnaps



versions of find can vary significantly, you may want to try the following command to ensure that your version of find can deal with the options...
Code:
find /vmfs/volumes -depth -type d -name \*snapshot\*LUN\* -ls


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP