#!/bin/bash set -e function helpexit() { echo Usage: $( basename "$0" ) -v source[/] target echo Copy directory structure to a folder. echo 'This command always recurses to subdirectories!' echo '-v verbose' exit } VERBOSE="" for ((i=1; i<=${#@}; i++)) { [[ "${!i}" = "-h" ]] && helpexit [[ "${!i}" = "--help" ]] && helpexit [[ "${!i}" = "-v" ]] && { VERBOSE="-v"; continue; } [[ -z "$SRC" ]] && { SRC="${!i}"; continue; } [[ -z "$TGT" ]] && { TGT="${!i}"; continue; } echo Too many arguments exit 1 } is_subfolder=1 if [[ "${SRC: -1}" = "/" ]]; then is_subfolder=0 fi src_abs=$( realpath "$SRC" ) tgt_abs=$( realpath "$TGT" ) src_base=$( basename "$src_abs" ) if [[ $is_subfolder -eq 1 ]]; then tgt_abs+="/$src_base" fi find "$SRC" -mindepth 1 -type d -printf "$tgt_abs/%P\0" | while read -d $'\0' file; do mkdir -p $VERBOSE "$file" done