Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to move subdirectories

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I am planning a migration from an old email server to a more up to date server. I need to migrate the users for each domain I host for, which is no problem cause I can tar the directory and sftp that tar file to the new server. The problem lies with needing to move directories around in the user accounts. Right now the directory structure looks like
Code:
domain/user/Maildir/mailboxes

I am trying to figure out how to setup a script that will move into each user directory and move the Maildir/mailboxes up one level so directory now looks like:
Code:
domain/user/mailboxes

So basically a script that will move all directories under Maildir up one level. I want this to be a perl script but since I have very little experience with perl I thought I'd come here to see if someone can get me started. I really just need to know how to tell perl to go into each user directory, then I can tell it to run a command to move the files.
 
Personally I'd probably do this with a unix "find".

Maybe something similar to this:
Code:
find domain/*/Maildir -name mailboxes -type d -exec mv {} .. \;
I've not tested that so it could be wildly wrong but I suspect it's not too far from the mark.



Trojan.
 
find doesn't change the current working directory for each exec, so it wouldn't make that mv relative the the file's original location.

A perl solution might be something like this:

Code:
perl -we 'use File::Find; use Cwd; find(sub{$File::Find::name =~ /Maildir\/mailboxes/ && print "mv $File::Find::name $File::Find::dir/..\n";},getcwd);' > /tmp/somescript

You can then run /tmp/somescript... File::Find doesn't like stuff being moved around while it's running, hence the two-step process.

Or perhaps something like:

Code:
find domain/*/Maildir -name mailboxes -type d | while read m ; do echo mv $m $(echo $m | sed 's#/Maildir##') ; done > /tmp/somescript

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top