Duplicating a Folder Structure

Can anyone tell me how to duplicate a folder/directory structure without files without using terminal?

There are certainly ways to do this without issuing commands through the Terminal app, but the best answer for you will depend on what sorts of automation tools you have on your computer. Do you own Keyboard Maestro? Alfred? Are you running Monterey so Shortcuts is available?

Also, it’s not clear how you want this to work. Do you want to run this automation when you have selected (in the Finder) the outermost folder of the structure to be duplicated? Do you want to duplicate that folder in addition to all the folders inside it? And how would you like to tell the automation where to put the duplicated structure?

1 Like

In situations like this, it’s always wise to defer to @drdrang!

Assuming you are working at the Terminal, and (this is important) your current working directory (folder) is the folder that contains the folder that you want to duplicate, then:
find theFolder -type d
will output a listing of every folder starting at theFolder and working down.

It is important that your current working folder is the folder containing theFolder, because you want the output of find to be a relative path, not an absolute path.

You can then use the xargs command to actually create the folders output by find eg:
find theFolder -type d | xargs -I{} mkdir -p /path/to/new/folder/tree/{}

This will put the duplicated folder structure into the folder at /path/to/new/folder/tree, so you will wind up with /path/to/new/folder/tree/theFolder and everything below that.

If you are comfortable working at the Terminal you can certainly customize this process for your specific needs.

It would be possible to script this in AppleScript as well, although a bit more complex of a script.

Thank you for your response. I would like to duplicate the folder and the sub-folders. They can duplicate anywhere, but i would move them. Basically i am duplicating folder structures from one year i. e. 2021 and creating the same folder structure for the next year i e 2022 I am running Monterrey. I do not have Alfred but did at one time. That may be how I did it in the past after listening to the MPU podcasts, but I can’t remember exactly how I did it.

Thank you but that is a bit complicated. I believe there are ways without using terminal.

Quite possibly. Google does find some mentions of apps to do this, but nothing that looked like it was still available that I found with a very quick look.

If you are only doing this once / year, I would propose a Terminal command is the most expeditious and cost effective approach, but if it is something you will do a lot and you are not comfortable at the Terminal, that may not work well for you.

1 Like

Here’s a Shortcut that works for me:

Duplicate Directory

It’s meant to be used as a Quick Action. After you install it in Shortcuts, you may have to adjust its settings in the Shortcut Details sidebar to look like this:

To use it, you right-click (or Control-click) on the directory whose structure you want to copy. When the contextual menu appears, go down to Quick Actions and select Duplicate Directory from its submenu. You will soon see a new folder on your Desktop that’s named using the current date in yyyy-mm-dd format (e.g. 2022-04-09). Inside that folder is the directory structure you want. (The extra outer folder is there in case you ever want to copy the directory structure of a folder that’s already on your Desktop.)

You’ll probably need to give it access permissions the first time you run it.

The shortcut has just one step, a shell script:

The input is the directory path to the selected folder, which is passed in as an argument to the script. The script is

folder=$(basename "$1")
cd "$1"
dest=$(printf "$HOME/Desktop/%s/%s" $(date +%Y-%m-%d) "$folder")
find . -type d | cpio -pd "$dest"

The final line of the script is basically @nlippman’s suggestion above. The main difference is that I used the cpio command instead of mkdir to build the directories from the results of the find command. It’s a little cleaner than dealing with xargs.

You might consider this cheating, as I’m just hiding the Unix commands in a Shortcuts wrapper. But to me, making shell commands like this more convenient to access is one of the best uses of Shortcuts (and Automator before it).

As I say, it works for me, and I tried it out on a variety of directories. Let me know if you run into any problems.

I like the clever use of cpio in this application. I’m just used to using xargs for this sort of thing, although I suppose I really should have just done the whole thing in find:
find . -type d -exec mkdir -p {} \;

I tend to embed these sorts of scripts into Keyboard Maestro or Alfred. I should start learning to use Shortcuts. I’m behind the times there.

It’s worth learning, but don’t go all-in on Shortcuts yet. It’s still a clumsy environment for building automations and many things don’t work right.

1 Like

If you’re looking at creating the same structures over, check out Post-Haste

Post Haste is a free project management tool that allows you to setup file and folder templates for your projects. Create a new project and everything’s organized, ready for you to start.

2 Likes

No idea how to do it without terminal…

In the shell:
rsync -a --include ‘/’ --exclude '’ “$from” “$to”