Register
It is currently Sun Nov 23, 2014 6:13 am

Move and Flatten Folder hierarchy


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Jun 25, 2012 2:17 pm   

Joined: Mon Jun 25, 2012 1:10 pm
Posts: 1
Sorry if this is a bit verboseā€¦. Its hard to determine how to write this with enough detail for someone to help without writing a book.

Biggest challenge (which I am completely stuck on) is processing names with spaces.
I'm using OS X - Bash beginner.

My partial/non-working script
Code:
#!/bin/bash
in="/Volumes/Postal/dovecot-migrated"
out="/Volumes/Postal/Out"
for user in `ls $in`; do
     cd $in/$user
    # Step1 - Move first level folders
       find . -name '.*' -type d -maxdepth 1 -mindepth 1 -exec echo mv \"{}\" \"$out/.$user{}\" \;
    # Step2 - Move user folder
       mv $in/$user  $out/.$user
  done
exit


With lots-o-comments
Code:
#!/bin/bash
in="/Volumes/Postal/dovecot-migrated"
out="/Volumes/Postal/Out"
# usernames do not have spaces
for user in `ls $in`; do
     echo USER=$user
     cd $in/$user
    # Step1 - Move first level folders
    # need to move any folder which begins with . and is at this level to $out
    # folder name should change to include prefix of .user.
    # orig= .folder.nest     move to out as:  .user.folder.nest
    # these folders do contain spaces
    # line below is definitely not working but gives an idea where I was going
    # This was one of my many failed attempts.
    find . -name '.*' -type d -maxdepth 1 -mindepth 1 -exec echo mv \"{}\" \"$out/.$user{}\" \;
    # Step2 - Move user folder
    # move user folder to out with a . as prefix
    # user folders don't contain spaces
    # user folder of joebob moves to out as .joebob
    mv $in/$user  $out/.$user
  done
exit


Given:
Code:
in
   user1
      file 1
      file 2
      folder 1    (space in name)
      folder 2
      .folder 1   (notice the . prefix)
      .folder 2
   user2
      file 1
      file 2
      folder 1
      folder 2
      .folder 1
      .folder 2


Desired Results
Code:
out
   .user1
      file 1
      file 2
      folder 1
      folder 2
   .user1.folder 1
   .user1.folder 2
   .user2
      file 1
      file 2
      folder 1
      folder 2
   .user2.folder 1
   .user2.folder 2


What I'm trying to do is move all dovecot users (users don't begin with .) to become folders inside another user
My dovecot installation uses . as the folder delimiter
so 3 levels nested appear as
Code:
user1
  .level1
     .level1.level2
     .level1.level2.level3
     .level1.level2.level3.afolder
     .level1.level2.level3.another




The out folder (with other users processed should look something like
Code:
out
  .user1
  .user.level1
  .user.level1.level2
  .user.level1.level2.level3
  .user.level1.level2.level3.afolder
  .user.level1.level2.level3.another
  .user2
  .user.meetings
  .user.meetings.DC
  .user.meetings.DC.invites
  .user.meetings.DC.invites.sent
  .user.meetings.DC.invites.received


Any help is greatly appreciated

JJ


Top
 Profile  
 PostPosted: Mon Jun 25, 2012 8:36 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
hi,

parsing ls output is never a good idea due to possible spaces (or else) in filenames
Code:
#!/bin/bash

mainDir="/Volumes/Postal"
in="$mainDir/dovecot-migrated"
out="$mainDir/Out"

for dir in "$in"/*
do if test -d "$dir"
   then cd "$dir"
        find . -type d -name '.*' -maxdepth 1 -mindepth 1 -exec echo "mv {} \"$out/.$PWD\"{}" \;
        cd ~-
        mv "$dir" "$out"/
   fi
done
not tested!


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 6 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