Copy Folder Structure and Keep Certain Files [SOLVED]

OK. After rather too long walking through the weeds with Terminal...

You have a folder called "Project" on your Desktop and you'd like to duplicate the folder structure, including any custom icons but none the files in it or any subfolders, to "New Project" on your Desktop:

rsync -a -E ~/Desktop/Project/ ~/Desktop/New\ Project --include \*/ --include \Icon\? --exclude \*

-a gives recursion, the copying of "normal" attributes like permissions, etc. -E includes extended permissions -- part of which is icon information. Note the trailing / for the source directory, without that you'll copy the contents but not the directory itself (and so not the icon).

For filters, * matches everything except a /. The filters are, in order: include any path that ends with a / (directories); include anything named "Icon?" (the hidden icon files); exclude everything whose path doesn't end with a / (all non-directories. Filters are tested in order and first match finishes, so "exclude all files" must come after the icon file test.

But you want to include the files of a subdirectory called "My Files"? Add another includes filter:

rsync -a -E ~/Desktop/Project/ ~/Desktop/New\ Project --include \*/ --include \Icon\? --include \My\ Files/\* --exclude \*

...which will you the files of "My Files" but not the files in any subdirectory of "My Files" (remember, * doesn't match a /). If you want to include the files contained by "My Files" and the files in all its subdirectories then use two *s, which does match /:

rsync -a -E ~/Desktop/Project/ ~/Desktop/New\ Project --include \*/ --include \Icon\? --include \My\ Files/\*\* --exclude \*

I'll leave how to use that in a KM action, with variables pumped in as source, target, and may included subdirectories, as an exercise for the Reader. (In other words, I need sleep!)

You'll find much, much, more about rsync and especially its filters via man rsync in Terminal.

Edit to add:

Forgot to say -- beware of things you think are files but really aren't! Two obvious ones are applications and RTFD "files" -- both are considered directories on the Unix side, so if you have those in your template you'll end up with orphaned folders. I'm sure there's other software that saves "files" as packages, so test and adjust inclusions/exlusions to suit.

9 Likes

Well done Nige! :sunglasses:

1 Like

Oh, I see what you mean. You're right, I was thinking along the lines of creating a new directory tree that merely has the same hierarchical structure as another. Sorry for jumping in like that.

1 Like

This whole thing is way out of my capacity to comprehend what you just shared, but I tested it and it works like a charm! Thank you so much for your time and help! :smiley:

Even if I don't add this as a macro (since I don't have that level of expertise when it comes to shell and KM together), having this option as a Terminal solution is already a great help and I will save that for sure! Once again, really appreciate you taking the time to find a solution for this!

For your specific use-case, try this out:

Recreate Folder Structure.kmmacros (29 KB)

Macro screenshot

3 Likes

Awesome @noisneil ! Thanks!! :slight_smile:

So I don't really need that extra section with "My Files" and all that (but I'm still keeping it, disabled, just in case). When I mentioned that, it was just an example of what I plan on doing now with that folder, so the simpler version works great. Here's what I ended up keeping. Let me know if this is what I should keep? I tested and it works, but let me know if there's something that could cause any issues in the future:

Keyboard Maestro Export

1 Like

Nice!

You can save yourself a couple of actions and, more importantly perhaps, an user prompt by combining actions 3, 4, and 5 into one so you grab the new name and destination with a single dialog:

newFolderPrompt

The "Choose New Folder" option returns a path to a "potential" folder and doesn't create the folder itself, so you won't get "folder already exists" problems with the shell script.

2 Likes

Very slick! Never used the Folder Prompt before, so that's helped me get my head around it. Thanks!

So here's a more general version that recreates the folder structure with icons (and without any nested files), for anyone who comes looking for it:

Recreate Folder Structure.kmmacros (23 KB)

Macro screenshot

4 Likes