How do I copy folder/directory structure without copying the contents?

How do I copy folder/directory structure without copying the contents?

A lot of answers require a fair amount of command-line knowledge but this was the simplest for me and just "worked".


1. Navigate to the Parent Folder you wish to duplicate (Open Terminal, Type "cd" and then drag the source parent folder onto the Terminal. Enter, you should now see the name of the current directory listed before the $ prompt (the folder/directory that you wish to duplicate without any contents).


2. Enter this command into the Terminal:

ls -R | grep :$ | sed 's/\.\/\(.*\):$/\1/' | \

while read thisFolder; do mkdir -p “CopiedFolderStructure”/“$thisFolder"; done


You will find a Folder called "CopiedFolderStructure" inside the parent folder with all of the empty sub-folders.

I got this from here... (thank-you :-)

https://stackoverflow.com/questions/23588221/recreate-folder-structure-osx


Posted on Sep 20, 2019 5:02 AM

Reply
Question marked as Top-ranking reply

Posted on Sep 20, 2019 5:38 AM

cd /path/to/beginning/dir
find . -type d | \
while read thisFolder; do mkdir -p “/path/to/CopiedFolderStructure”/“$thisFolder"; done


Or


cd /path/to/beginning/dir
find . -type d -print0 | xargs -0 -I{} mkdir -p "/path/to/CopiedFolderStructure”/{}"


It would be wise to test by replacing 'mkdir' with 'echo' to see what the new path would look like before actually doing any of the code examples on blind faith 😀


NOTE: The 'find' command is very useful for this kind of operation, and when combined with the 'xargs' command extremely powerful. The -print0 and -0 options handle macOS file and directory names with spaces in them.

Similar questions

2 replies
Question marked as Top-ranking reply

Sep 20, 2019 5:38 AM in response to AtherosChipstfsckd

cd /path/to/beginning/dir
find . -type d | \
while read thisFolder; do mkdir -p “/path/to/CopiedFolderStructure”/“$thisFolder"; done


Or


cd /path/to/beginning/dir
find . -type d -print0 | xargs -0 -I{} mkdir -p "/path/to/CopiedFolderStructure”/{}"


It would be wise to test by replacing 'mkdir' with 'echo' to see what the new path would look like before actually doing any of the code examples on blind faith 😀


NOTE: The 'find' command is very useful for this kind of operation, and when combined with the 'xargs' command extremely powerful. The -print0 and -0 options handle macOS file and directory names with spaces in them.

Sep 20, 2019 7:10 AM in response to BobHarris

Instead of echo, this will also provide a visual of the folder structure if the optional tree is installed via package manager (e.g. home brew):


find . -type d | tree -d
├── Artwork
├── Audio\ Files
│   ├── Music
│   └── SFX
├── Documents
├── FINAL
├── Finals
├── Images
├── Project\ Files
└── Video\ Files
    ├── Raw\ Footage
    ├── Video\ Elements
    └── Videos


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I copy folder/directory structure without copying the contents?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.