use glob to expand filenames in /basedir use grep to grab all directory names for each directory name grab its subdirs pass them onto map to where there matched to the regex '.*/(.*?)$' assign the (array) result from the map to the corresponding %dir key