I have some code that converts a date in a file name into a path.
#!/bin/bash
aFile="bla_2016-11-24_17-24.zip" # Many files, each with same format: "bla_{year}-{month}-{day}_{hour}-{minute}.zip"
aDate=${aFile#*_} # remove "bla_"
aDate=${aDate%.*} # remove ".zip"
aDate=${aDate//_/ } # replace "_" with " "
aDate=(${aDate//-/ }) # replace "-" with " " and make array
aPath="${aDate[0]}/${aDate[1]}/${aDate[2]}/${aDate[3]}/${aDate[4]}"
mkdir -p $aPath
I've seen both of these (1, 2), which suggest that nested parameter expansion expressions in bash scripting are not possible.
I was wondering if there might be a better way to implement my code above, more specifically reduce the length of the code. It's just five lines, so not a huge irritation if it's not possible, but it seems like I'm doing it in a dumb way.
Python solution:
aFile = "bla_2016-11-24_17-24.zip"
import re
aPath = "/".join(re.split("[_-]", aFile[4:-4])) # 2016/11/24/17/24