From f21cbfd5fbaf82208c40276912669a459d1128c5 Mon Sep 17 00:00:00 2001 From: Q Date: Sat, 7 Dec 2019 08:38:11 +0200 Subject: [PATCH] copy folder structure only --- bin/cp-dir-struct | 1 + files/cp-dir-struct | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 120000 bin/cp-dir-struct create mode 100755 files/cp-dir-struct diff --git a/bin/cp-dir-struct b/bin/cp-dir-struct new file mode 120000 index 0000000..f91d809 --- /dev/null +++ b/bin/cp-dir-struct @@ -0,0 +1 @@ +../files/cp-dir-struct \ No newline at end of file diff --git a/files/cp-dir-struct b/files/cp-dir-struct new file mode 100755 index 0000000..ecd32fc --- /dev/null +++ b/files/cp-dir-struct @@ -0,0 +1,40 @@ +#!/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 +