{"id":789,"date":"2025-12-10T14:17:08","date_gmt":"2025-12-10T06:17:08","guid":{"rendered":"https:\/\/play.datalude.com\/blog\/?p=789"},"modified":"2025-12-10T15:10:34","modified_gmt":"2025-12-10T07:10:34","slug":"bash-script-to-replicate-a-directory-structure","status":"publish","type":"post","link":"https:\/\/play.datalude.com\/blog\/2025\/12\/bash-script-to-replicate-a-directory-structure\/","title":{"rendered":"Bash script to replicate a directory structure"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I found a need for this once upon a time. It will descend through a Linux directory, get the permissions, ownership details for all the subdirs and then write a script for you to recreate it somewhere else. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You use it like this. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Put the script in the parent directory of the one you want to clone and run it.\nclone_directory_structure.sh targetdirectory &gt; regenerate_directory_structure.sh\n\n# Then move the script to where you want to clone the directory and run it. \nregenerate_directory_structure.sh \n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Script listing<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# Check if directory is provided\nif &#91; $# -ne 1 ]; then\n    echo \"Usage: $0 &lt;directory>\"\n    echo \"Example: $0 \/path\/to\/directory\"\n    exit 1\nfi\n\n# Remove trailing slash if present\nTARGET_DIR=\"${1%\/}\"\nTARGET_BASENAME=\"$(basename \"$TARGET_DIR\")\"\nTARGET_PARENT=\"$(dirname \"$TARGET_DIR\")\"\n\n# Check if directory exists\nif &#91; ! -d \"$TARGET_DIR\" ]; then\n    echo \"Error: Directory '$TARGET_DIR' does not exist\"\n    exit 1\nfi\n\necho \"#!\/bin\/bash\"\necho \"# Script generated on $(date)\"\necho \"# To recreate the directory structure, run this script as root or with sudo\"\necho \"set -e  # Exit on error\"\necho\n\n# Process the target directory itself\necho \"# Creating target directory: $TARGET_BASENAME\"\necho \"mkdir -p \\\"$TARGET_BASENAME\\\"\"\n\n# Get permissions, owner, and group of the target directory\nTARGET_PERMS=$(stat -c \"%a\" \"$TARGET_DIR\")\nTARGET_OWNER=$(stat -c \"%U\" \"$TARGET_DIR\")\nTARGET_GROUP=$(stat -c \"%G\" \"$TARGET_DIR\")\n\necho \"chmod $TARGET_PERMS \\\"$TARGET_BASENAME\\\"\"\necho \"chown $TARGET_OWNER:$TARGET_GROUP \\\"$TARGET_BASENAME\\\"\"\necho\n\n# Find all subdirectories and process them\nfind \"$TARGET_DIR\" -type d -not -path \"$TARGET_DIR\" -print0 | while IFS= read -r -d $'\\0' dir; do\n    # Quote the directory path to handle spaces\n    dir=\"$dir\"\n    # Get relative path from target directory\n    rel_path=\"${dir#$TARGET_DIR\/}\"\n    \n    # Quote the path to handle spaces\n    rel_path_quoted=\"$(printf '%q' \"$rel_path\")\"\n    \n    # Get permissions in octal\n    perms=$(stat -c \"%a\" \"$dir\")\n    \n    # Get owner and group\n    owner=$(stat -c \"%U\" \"$dir\")\n    group=$(stat -c \"%G\" \"$dir\")\n    \n    # Output commands with proper quoting for paths with spaces\n    echo \"# Creating directory: $TARGET_BASENAME\/$rel_path\"\n    echo \"mkdir -p $(printf '%q' \"$TARGET_BASENAME\/$rel_path\")\"\n    echo \"chmod $perms $(printf '%q' \"$TARGET_BASENAME\/$rel_path\")\"\n    echo \"chown $owner:$group $(printf '%q' \"$TARGET_BASENAME\/$rel_path\")\"\n    echo\ndone\n\necho \"echo \\\"Directory structure recreation complete!\\\"\"\necho \"echo \\\"Created structure under: $TARGET_BASENAME\/\\\"\"\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I found a need for this once upon a time. It will descend through a Linux directory, get the permissions, ownership details for all the subdirs and then write a script for you to recreate it somewhere else. You use it like this. Script listing<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[144,4],"tags":[],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-bash-script","category-linux"],"_links":{"self":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/comments?post=789"}],"version-history":[{"count":2,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/789\/revisions"}],"predecessor-version":[{"id":791,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/789\/revisions\/791"}],"wp:attachment":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/media?parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/categories?post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/tags?post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}