Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Script for moving series of sub folders

Shell Programming and Scripting


Tags
bash, find, rsync, solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-31-2014   -   Original Discussion by vipertech
vipertech's Unix or Linux Image
vipertech vipertech is offline
Registered User
 
Join Date: Jul 2014
Last Activity: 27 August 2014, 3:35 PM EDT
Location: Rochester NY
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Script for moving series of sub folders

Hello, I'm new to this forum. Did a search but I didn't quite find what I was looking for. This is probably a fairly easy request but I'm just not sure how to accomplish this.

I have a folder structure that looks something like this:

Code:
/names/company1/archive
/names/company1/newarchive

/names/company2/archive
/names/company2/newarchive

/names/company3/archive
/names/company3/newarchive


There are tons of companies under the /names folder. I want to start at the /names folder and search for any folders under the company/archive folder and move those folders to the same companies company/newarchive folder.

Thanks!

Last edited by rbatte1; 08-01-2014 at 06:21 AM.. Reason: Added CODE tags
Sponsored Links
    #2  
Old Unix and Linux 07-31-2014   -   Original Discussion by vipertech
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 February 2018, 3:15 PM EST
Location: Aachen, Germany
Posts: 12,167
Thanks: 371
Thanked 3,752 Times in 3,447 Posts
Will this be exhaustive, i.e. will all files/folders be moved and none remain? Then just rename /.../.../archive to /.../.../newarchive.
Sponsored Links
    #3  
Old Unix and Linux 07-31-2014   -   Original Discussion by vipertech
vipertech's Unix or Linux Image
vipertech vipertech is offline
Registered User
 
Join Date: Jul 2014
Last Activity: 27 August 2014, 3:35 PM EDT
Location: Rochester NY
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
I thought about the renaming process but the folder structures already exist and there are some that already are in place. i.e. already have the folders in the correct spot. So I really don't want to blanket rename the entire folder structure. Both the archive and newarchive folders will need to remain after the move.

---------- Post updated at 02:39 PM ---------- Previous update was at 01:03 PM ----------

I forgot to also mention that there are files within the folders I want to move.
    #4  
Old Unix and Linux 07-31-2014   -   Original Discussion by vipertech
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 23 February 2018, 8:47 PM EST
Posts: 3,466
Thanks: 151
Thanked 1,226 Times in 1,125 Posts
How about this solution using rsync.

The find command is to remove empty directories left under the company/archive directory, I'm not sure if you wanted those cleaned up or not.



Code:
cd /names

for arch in */archive
do
   newarch="${arch%archive}newarchive"
   if [ -d "$newarch" ]
   then
       rsync -a --remove-source-files "$arch" "$newarch"
       find "$arch" -mindepth 1 -type d -delete
   fi
done

Sponsored Links
    #5  
Old Unix and Linux 08-01-2014   -   Original Discussion by vipertech
vipertech's Unix or Linux Image
vipertech vipertech is offline
Registered User
 
Join Date: Jul 2014
Last Activity: 27 August 2014, 3:35 PM EDT
Location: Rochester NY
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Chubler_XL,

Thank you for the suggestion! That worked great other than one small issue....it takes the archive folder itself and all of its contents and places them under the newarchive folder.

IE:
before the move
/names/company1/archive/<a bunch of subfolders>
/names/company1/newarchive/

after the move
/names/company1/archive/
/names/company1/newarchive/archive/<a bunch of subfolders>

I need it to take the contents of the archive folder and move them to the newarchive folder.

IE:
before the move
/names/company1/archive/<a bunch of subfolders>
/names/company1/newarchive/

after the move
/names/company1/archive
/names/company1/newarchive/<a bunch of subfolders>

Thank you!

---------- Post updated at 09:27 AM ---------- Previous update was at 08:12 AM ----------

BTW, I fixed the issue I pointed out by making these simple changes:



Code:
#!/bin/bash
for arch in */archive/*
do
   newarch="${arch%archive/*}archivenew"
   if [ -d "$newarch" ]
   then
      rsync -a --remove-source-files "$arch" "$newarch"
      find "$arch" -mindepth 0 -type d -delete
   fi
done


Last edited by vipertech; 08-01-2014 at 09:29 AM..
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Execute script in series of folders 3therk1ll Shell Programming and Scripting 2 04-18-2014 09:51 AM
moving files from one folder to many folders realspirituals Shell Programming and Scripting 2 08-18-2011 05:30 AM
Moving folders gnpower Red Hat 4 11-30-2010 04:46 AM
Moving folders with script ghimakiran Ubuntu 4 02-08-2010 08:49 AM
moving multiple folders/files in subversion using bash script ravishan21 Shell Programming and Scripting 2 11-12-2008 05:35 AM



All times are GMT -4. The time now is 03:45 AM.