Register
It is currently Mon Dec 22, 2014 9:12 am

find a folder, for each user home directory, and rename it


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Sep 23, 2008 3:33 am   

Joined: Tue Sep 23, 2008 3:10 am
Posts: 2
Location: Cork, Ireland
Hi all,

This is my first post here, and I hope to learn some bash scripting from you guys.

Now I am faced with a challenge, basically the other day I messed up +400 network home folders, when playing with login and logout redirections. I managed to store the user music folders in their local clients at login, vlink to the local folder, and create a backup copy of the Music folder as Music 1. At logout whatever all new and modified files were copied back to the Music 1 folder. Until there, everything was OK until I got a request to remove the the folder redirections for the Music folder for all the users.

I got referred to these 2 commands as a way to delete the vlink and rename the Music 1 folder to Music:

###delete Music vlink
find . -maxdepth 4 -name 'Music' -type l -delete
###rename Music 1 to Music
find . -maxdepth 4 -name 'Music 1' -type d -exec mv {} 'Music' \;

The second command won't work, it gives "directory not empty".

Because the command being executed each time is:

mv 'path/user1/Music 1' 'Music'
mv 'path/user2/Music 1' 'Music'
mv 'path/user3/Music 1' 'Music'
mv 'path/user.../Music 1' 'Music'
...

I want to rename EACH path. So I need a way to rewrite the path, not just use 'Music' every time.

Since {} stores the variable path/user/Music 1

Is there a way, to reuse this variable to extract the username necessary for
each Music folder path?

as a crude example:
find . -maxdepth 4 -name 'Music 1' -type d -exec mv {} #/path/$user/#'Music' \;

Maybe using the sed command, but this is way above my UNIX knowledge.

Note that the user changes since its sourced from a find at a network users
sharepoint with +400 users.

Many thanks in advance.


Top
 Profile  
 PostPosted: Wed Sep 24, 2008 5:14 am   

Joined: Tue Sep 23, 2008 3:10 am
Posts: 2
Location: Cork, Ireland
Hi all,

I have put together this bash script, with some guidelines provided by Ned Harvey.

For some reason the mv command is not picking up the 'Music 1' string. And additionally is trying to create a folder named as "1".

I really need some help please with my poor syntax.


#!/bin/bash
for f in `find . -maxdepth 3 -name 'Music 1' -type d`;
do g=`echo $f | sed 's/Music 1$/Music$/'`;
mv -v $f $g;
done

This is the output I get:

mv: rename ./staff0809/acutts/Music to ./staff0809/acutts/Music: No such file or directory
mv: rename 1 to 1: No such file or directory


Many thanks in advance,

Eric


Top
 Profile  
 PostPosted: Wed Sep 24, 2008 9:22 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
eric_csm wrote:
#!/bin/bash
for f in `find . -maxdepth 3 -name 'Music 1' -type d`;
do g=`echo $f | sed 's/Music 1$/Music$/'`;
mv -v $f $g;
done

This is the output I get:

mv: rename ./staff0809/acutts/Music to ./staff0809/acutts/Music: No such file or directory
mv: rename 1 to 1: No such file or directory


The error is caused because the 'for' command delimits the entries by space, therefore the first run through f is set to '/path/to/Music' and the second time through f is set to '1'

I don't think for can be used in that way, I personally would use the following style of loop to get around it...

Code:
#!/bin/bash
find . -maxdepth 3 -name 'Music 1' -type d | while read f
do
  g=$(echo $f | sed 's/Music 1$/Music$/')
  mv -v "$f" "$g";
done


Using the while read loop removes the issue that the for loop caused, however also note that the mv command's options are also space delimited so the $f MUST be in quotes to deal with the space.

This type of loop also has issues (the loop is in a bash sub-shell) but would seem to be suitable in this situation.

I hope this helps


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


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