1

I tried incorrectly to add my question on to a very similar thread w/ good solutions here:

mac os x terminal batch rename

I have essentially the same question, but I'm wanting to do this and change the folder path when renaming. Here is what I asked:

Would any of these solutions work to change underscores to a folder path? For example, I have mbox files on one level that need to be nested, such as:

TopLevel_NextLevel_mbox
TopLevel_NextLevel_FinalLevel_mbox

I'd like to automatically put these in a hierarchy like so:

TopLevel/NextLevel/mbox
TopLevel/NextLevel/FinalLevel/mbox

Can this be done? When I try simple replacement with "/", I get this:

fred$ for f in *_mbox; do mv "$f" "${f/_//}"; done
mv: rename TopLevel_NextLevel_mbox to TopLevel/NextLevel_mbox: No such file or directory

Looks like it just tries to sub in the "/", but then gets confused because there is no current folder TopLevel w/ NextLevel_mbox inside it...

Thanks, Fred

Community
  • 1
  • 1
fhturner
  • 11
  • 2
  • 1
    Hi Fred, this is a different situation and not really a programming question. You have to not only replace the text, but create the directories if needed. I suggest working on a script for that, to replace the name and then move the original file to the new name (and searching for "mv create directory if don't exist"). If you have a more specific question about a problem im programming that, once you started, post here ;-) – sidyll Jun 26 '15 at 18:05
  • Thanks so much for the reply, sidyll! I will bang my head against the wall a little further and then shout back w/ more details if I can. -Thx, FT – fhturner Jun 26 '15 at 20:37
  • Sorry that I can't be more specific, it is not accepted by the rules in the site. Since here are for programming questions only. I recommend trying superuser sister site for these things. But to point out some things what you did was almost correct. Once you get the text conversion done right, [this question](http://stackoverflow.com/q/547719/557306) will help you. – sidyll Jun 26 '15 at 20:42
  • @fhturner, In your hierarchy are wanting the folders nested like so: `TopLevel > NextLevel > mbox & FinalLevel > mbox` or something such as `TopLevel > NextLevel > mbox > TopLevel > NextLevel > FinalLevel > mbox` — It's a bit unclear. – l'L'l Jun 26 '15 at 22:56
  • Yes, Sidyll, obviously a n00b here as far as Stack Overflow. Sometimes it's a bit overwhelming to understand the exact ways things are to be done (even when reading the FAQs) when the main thing one is after in the first experience is to just ask a question. :-) – fhturner Jun 27 '15 at 19:59
  • @I'L'I, the first example you give is what I'm after. I'm sorry to be unclear- I meant those 2 lines as 2 examples. Probably should've kept it to just one. But anyway, I'm basically wondering how to insert a "hierarchy break" at each underscore. Sounds like it might be a little involved, based on sidyll's response above. Thanks for taking the time to reply, folks! – fhturner Jun 27 '15 at 20:01

1 Answers1

0

The basics of making this work starts with the process of creating an array from the current directories that contain *mbox. Each array key then contains the resulting delimited word found between the underscores:

TopLevel_NextLevel_mbox

Is transformed into an array like this:

( TopLevel, NextLevel, mbox )

From there we create the first directory TopLevel then perform a cd followed by mkdir on the next key — repeating the process until there are no more keys. By doing this each array key creates a new nested directory (as a bonus it also copies any data from the original directory into the new one whilst keeping it's structure).

Create Nested Folders from Original

#!/bin/bash

DIR=$PWD
for f in *mbox
    do cd $DIR         
    if [[ -d $f ]]; then
        ARR=(${f//_/ }); n=0
        for i in "${ARR[@]}"   
            do echo $n
                if [[ $n -eq 0 ]]; then 
                    mkdir -p $i && cp -R $f/* $i && cd $_
                else
                    mkdir -p $i && cd $_
                fi
            let n++
        done
    fi
done

Pseudo One-liner

DIR=$PWD; for f in *mbox; do cd $DIR; if [[ -d $f ]]; then ARR=(${f//_/ }); n=0; for i in "${ARR[@]}"; do if [[ $n -eq 0 ]]; then mkdir -p $i && cp -R $f/* $i && cd $_; else mkdir -p $i && cd $_; fi; let n++; done; fi; done

This is the same exact script as the one above it, however, it's formatted to be one line. * The script leaves the original directories intact (I'll leave the exercise of removing them up to the OP).

l'L'l
  • 44,951
  • 10
  • 95
  • 146