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


👤 Login to reply

    #1  
Old 07-31-2014
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 05:21 AM.. Reason: Added CODE tags
Sponsored Links
    #2  
Old 07-31-2014
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 July 2018, 12:24 PM EDT
Location: Aachen, Germany
Posts: 13,082
Thanks: 452
Thanked 4,017 Times in 3,693 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 07-31-2014
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 07-31-2014
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 19 July 2018, 8:54 PM EDT
Posts: 3,553
Thanks: 159
Thanked 1,267 Times in 1,161 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 08-01-2014
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 08:29 AM..
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 08:51 AM
moving files from one folder to many folders realspirituals Shell Programming and Scripting 2 08-18-2011 04:30 AM
Moving folders gnpower Red Hat 4 11-30-2010 03:46 AM
Moving folders with script ghimakiran Ubuntu 4 02-08-2010 07:49 AM
moving multiple folders/files in subversion using bash script ravishan21 Shell Programming and Scripting 2 11-12-2008 04:35 AM



All times are GMT -4. The time now is 04:56 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?