Search code examples
unixfor-loopterminalfishseq

create a multi variable nested directory structure UNIX


Trying to create a nested directory structure YEAR/FILE_TYPE.

Desired Directory Structure:

~/2016/doc
~/2016/pdf
~/2016/txt
~/2015/doc
~/2015/pdf
~/2015/txt

etc...

create by YEAR variable with: set varYear (seq 1996 2016) # fish shell syntax

create File Type variable set varType (doc pdf txt) # fish shell syntax

1st failed attempt: running echo "mkdir /"$varDate/$varType

2nd failed attempt: for i in $varDate echo $i "/" $varType end # fish shell syntax

Would appreciate some help on how to combine the two variables together in a for loop to create the directory structure.


Solution

  • Glenn's answer is good but the braces aren't actually needed. Also, parentheses are for subcommands (similar to bash's $()). So the parens in your set varType (doc pdf txt) don't do what you want unless you meant to run a command named doc. Try this which relies on fish's unique manner of expanding vars with more than one value:

    set varYear (seq 1996 2016)
    set varType doc pdf txt
    mkdir -p $varYear/$varType
    

    Or using traditional loop structures:

    for varYear in (seq 1996 2016)
        for varType in doc pdf txt
            mkdir -p $varYear/$varType
        end
    end