How do I automatically create multiple folders from terminal where folders have spaces in name

One of those things with the Mac that should be a 30 second research project but turns out to take way longer than doing it manually, but now I've sunk time into trying to learn the "correct" syntax, i.e. whatever syntax the Mac OS uses, so I want to finish the chore.


I want to make a bunch of folders from the terminal using mkdir or "mkdir FILENAME | xargs mkdir"


Per a how-to, any name that contains spaces must be enclosed in quotes. This is consistent with what I recall from UNIX and DOS command-line syntax. However, no matter what I do, the OS keeps treating every single word as its own name and tries to create a folder for each word.


How do I tell the terminal to create a folder whose name contains spaces


MacBook Pro 13″, macOS 10.14

Posted on Sep 10, 2021 8:06 AM

Reply
7 replies

Sep 10, 2021 1:57 PM in response to GuyAskingQuestionsAboutMacOS

The syntax is standard Unix/Linux syntax. It is not specific to macOS.


Multiple directories at once would be


mkdir -p /path/to/first   /path/to/second   /path/to/third


If you want spaces in the name, then you quote the path string which can be "..." or '...' or a backslash before each space character

mkdir -p "/path/to first"  '/path/to second'   /path/to\ third\ or\ forth\ or\ fifth

Sep 10, 2021 12:42 PM in response to VikingOSX

Sorry, I meant:


cat FILENAME.txt | xargs mkdir


Strangely, now both methods work (either specifying the names via command line, or via source file). There must have been a syntax error somewhere, but I have no idea where. I was super careful to use plain text, straight double quotes, no extraneous spaces, etc., and I tried it over and over, but it just wouldn't work, and now it does.


Thanks for the assistance, all.

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 automatically create multiple folders from terminal where folders have spaces in name

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