Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create multiple folders from Terminal UNIX
I needed to create a new folder for each of a list of customers, and knew there had to be a better way than doing it manually. So I looked at the mkdir command, which creates new folders or directories. It uses the following syntax:
mkdir folder1 folder2 folder3
I tried redirecting a file with a list of folder names into mkdir with no success, so I ended up just copying and pasting my list into Terminal. You cannot use a list with returns; all the names need to be on one line, separated by spaces.

[kirkmc adds: While this is a pretty basic hint, it shows some interesting things that you can do from Terminal. First, you need to tweak your list of customers (or whatever) so all the names are on one line. Next, you need to worry about spaces: if you have names with spaces, the names need to be in quotes, such as "Henry David Thoreau"; otherwise, you'd get individual folders named Henry, David and Thoreau. You could also use underscores (Henry_David_Thoreau), or other characters as separators.

For example:
$ mkdir "Henry David Thoreau" Ralph_Waldo_Emerson Walt-Whitman
The above command makes a total of three new folders, the first separated by spaces, the second by underscores, and the third with a hyphen; the only constraints are the actual tweaking of your list. You may need to take a tab- or return-delimited list and replace the tab or returns, or manually add quotes, but you can use this to create any number of folders very quickly.]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[43,872 views]  

Create multiple folders from Terminal | 36 comments | Create New Account
Click here to return to the 'Create multiple folders from Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
try xargs
Authored by: davidduff on Apr 25, '06 08:07:14AM

you might find it informative to do "man xargs".

xargs is a core unix tool used to accomplish exactly what you are talking about, namely to take a list of data (command arguments) from a file and execute a certain command repeatedly. so in this case, for example, if you had a list of directory names in a file, one per line, you could use xargs to execute the mkdir command once for each line in the file.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: lts on Apr 25, '06 08:10:30AM

You can try this: if you have a list of names of new directories that you want to create--e.g., call it "list":

cat list | xargs mkdir -vp

xargs will take the lines from its input [the output from cat], combine them into a single line and pass that line as additional arguments to the command specied as xargs' 1st argument--in this case, "mkdir -vp". If the resulting line would be too long [there IS an upper limit on the length of your argument string], xargs will invoke the command multiple times with segments of the list that "fit".

As kircmc mentioned, be sure that the folder names in the list contain no spaces, or are enclosed in quotes, else you'll get one folder per word on the line.

See the mkdir man page for info on the 'v' and 'p' options. Also, see the xargs man page for more info.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: wendo on Apr 25, '06 08:11:29AM

it is possible to create multiple folders from a list in a file, you will just need to use an additional UNIX command, xargs. for example:
% cat file_names
folder1
folder2
folder3
%cat file_names | xargs -i mkdir \{\}
% ls | grep folder
folder1
folder2
folder3
%
Note, this was tested on Solaris (don't have access to a mac right now), so the syntax might be slightly different. i'm not sure if the -i is necessary or not. also, i use tcsh, so the backslashes (\) escape the shell for the curly brackets ({}). depending on what shell you use, you may or may not need these.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: dlacher on Apr 25, '06 08:16:09AM

Another unix method of createing multiple folder is as follows:

% mkdir long_folder_name_{00,01,02,03,04,05,06}

this will creat the seven folders long_folder_name_ and ending in the expansion of the numbers in the array.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: mgastel on Apr 25, '06 08:19:02AM
Or you can execute mkdir `cat dirfile` where dirfile contains the names of the directories. Those are backquotes around the cat command.

[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: hj45lp on Apr 25, '06 08:29:03AM

Most (all?) of the suggestions here require the directory names not to have spaces. The following command creates directory names with spaces. It requires that the directory names are listed in the file "list", one per line:

awk '{system("mkdir \"" $0 "\"")}' list

Have fun.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: hj45lp on Apr 25, '06 08:32:15AM

Sorry, in the previous post some crucial backslashes vanished as a consequence of the html coding. Here I'll try again:

awk '{system("mkdir \"" $0 "\"")}' list



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: Oneota on Apr 25, '06 08:50:54AM

Ah, but what if they have Double Quotes or Single Quotes in their names? How would one deal with that?



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: adrianm on Apr 25, '06 09:11:05AM
To not care about spaces, quotes, etc, and the name of dirs is in folders.txt:

perl -n -e 'mkdir($_);' < folders.txt


[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: ScottTFrazer on Apr 25, '06 10:35:36AM

and _that's_ why I love perl.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: juanfal on Apr 25, '06 10:49:05AM
perl, perl, what a nightmare.. !!!! You should use
   perl -ne 'chomp($x=$_); mkdir($x);' < dirnames.txt
and the utf8 accented etc chars are not welcome :(

[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: kitzkikz on Apr 27, '06 04:11:33PM
There's no need for the chomp if you use the lowercase L switch. There's no need for the parens, semicolon, or redirection either.
perl -lne 'mkdir $_' dirnames.txt


[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: osxpounder on Apr 25, '06 11:42:10AM

Doesn't do a thing for me. Not even an error.

---
--
osxpounder



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: osxpounder on Apr 25, '06 11:45:43AM

I was referring to the first perl command, when I said it did nothing for me, but I'll add that the second perl command also did nothing. If I just run "perl" on the command line, I see a process in Activity Monitor, so it's not that I lack perl.

So far, the only commands in this thread that have worked for me are those of the original post, and then the xargs suggestion immediately following.

---
--
osxpounder



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: pmccann on Apr 25, '06 08:52:23PM

Then something's fishy: I made a text file "dirnames.txt" in my home directory with the contents

one
two
three

and then executed the above command in the same directory:

perl -ne 'chomp($x=$_); mkdir($x);' < dirnames.txt

Bingo: three folders, appropriately named.

Cheers,
Paul



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: osxpounder on Apr 25, '06 11:41:07AM

This one did nothing but gave me an error: "Wilson Mug-Base.jpg: File name too long" Hmm. Is that because the file name had a space in it? I thought the point of the awk trick was that it would work with items that had spaces in their names.

BTW, that's the last item in the list, the Wilson photo.

---
--
osxpounder



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: ocdinsomniac on Apr 25, '06 08:55:54AM

The "for" command is very nice for this:

for item in `cat your_list.txt`; do mkdir /path/to/location/$item; done

No text reformatting required.

-systemsboy



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: osxpounder on Apr 25, '06 11:51:45AM
The "for" command is very nice for this:

for item in `cat your_list.txt`; do mkdir /path/to/location/$item; done

No text reformatting required.

This also fails for me. I get the error:

mkdir: your_list.txt: File exists

---
--
osxpounder

[ Reply to This | # ]

Create multiple folders from Terminal
Authored by: juanfal on Apr 25, '06 10:39:17AM
Why not jot
    mkdir `jot -w Folder%02d_of_10 10`
I had problems with the spaces... It was impossible for me to include spaces in the name of the folders with this simple method... any sugestion?

[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: suarez on Apr 25, '06 11:53:19AM
I've always used
$ cat folders.txt | sed 's/ /\\ /g' | xargs mkdir
for this. The sed command converts spaces to backslashed spaces, necessary to "escape" each space and allow folder names with spaces.

[ Reply to This | # ]
Unix command hints?
Authored by: sjk on Apr 25, '06 11:55:59AM

What criteria is used to determine which Unix commands are worthy to be posted as hints here?



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: suarez on Apr 25, '06 12:03:12PM
Silly me: you don't need the cat. This will do it, too:
$ sed 's/ /\\ /g' folders.txt | xargs mkdir
The for loop and backtick solutions don't work unless the folder names are separated by spaces on one line. Then again, you can't have multiple folders containing spaces that way, and it seems you'd never want to create any such folders.txt file like that anyway.

[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: shadowofged on Apr 25, '06 12:25:07PM
Here's a way to do it that will create folders even if your list of names have spaces. The file dir_list.txt contains a list of directory names, one per line. Here goes:

cat dir_list.txt | while read FILE
do
    mkdir "${FILE}"
done

That should work for all files, spaces or not.

[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: osxpounder on Apr 25, '06 02:13:44PM

Well, dangit, it didn't work for me. Maybe I goofed, but here's what I did:

Created a plain text document in TextEdit, saved as plain text, UTF-8.

In the same folder, I opened a Terminal, and used pico to create a text file that contained your script. I changed the name of the list.txt file to match mine.

Saved out of pico to a file called "do". I chmoded "do" 0700.

Then I typed:

./do

Nothing happened. No errors, no new folders.

---
--
osxpounder



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: suarez on Apr 25, '06 04:01:42PM

Do is a reserved word in bash. See the reserved words section in the bash man page. If you change the script name to anything else that's not a reserved bash word, it works.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: osxpounder on Apr 26, '06 12:24:22PM
Thanks for the tip. I renamed it to "herbie", but still, same results -- i.e, none, not even an error message.

herbie's contents:

cat list.txt | while read FILE

do

mkdir "${FILE}"

done

I'm using bash.

---
--
osxpounder

[ Reply to This | # ]

Create multiple folders from Terminal
Authored by: merlyn on Apr 30, '06 03:35:21PM

Both uses of "cat file | ..." in this thread are "useless use of cat". Google for that for details. In other words, sloppy coding, time to learn to clean up your messes.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: BobHarris on Apr 25, '06 04:07:59PM
-- AppleScript wrapper to execute UNIX shell scripts with a drag and drop interface.
-- Found basic script at http://MacOSXHints.com
-- Modified to Create directories from a list stored one per line in a file dropped on the script
-- script should handle spaces in name, apostrophes in names, etc...
-- Bob Harris	25-Apr-2006

on open filelist
	repeat with dir in filelist
		do shell script 
			"while read dir;do mdir -p \"$dir\";done <" & quoted form of POSIX path of dir
	end repeat
end open


[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: osxpounder on Apr 26, '06 12:36:26PM

Bob: your kind offer of an Applescript was appreciated. It didn't compile for me, though. It breaks at the $ sign, saying "Syntax error Expected end of line but found unknown token".

---
--
osxpounder



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: osxpounder on Apr 26, '06 12:46:50PM

I thought I found a typo or two, so I changed Bob's script a bit, but still no results. I changed the quotes around $dir to single quotes, and that allowed the script to compile, but it did nothing. I saved it as an app and dropped a plain text file, with names, one per line, on it.

I changed "mdir" to "mkdir", but that made no difference, either.

---
--
osxpounder



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: BobHarris on Apr 26, '06 02:49:55PM

This is my 3rd attempt at getting this Applescript posted cleanly. First I lost the < because it looked like the start of an html tag. The 2nd time I lost the \'s and messed up the mkdir spelling. This time I'm just going to use Plain Old Text, and the heck with indentation and hope that line wrapping doesn't happen


-- AppleScript wrapper to execute UNIX shell scripts with a drag and drop interface.
-- Found basic script at http://MacOSXHints.com
-- Modified to Create directories from a list stored one per line in a file dropped on the script
-- script should handle spaces in name, apostrophes in names, etc...
-- Bob Harris 25-Apr-2006

on open filelist
repeat with afile in filelist
do shell script "while read DIR;do mkdir -p \"$DIR\";done <" & quoted form of POSIX path of afile
end repeat
end open



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: jaysoffian on Apr 26, '06 09:39:02PM
So as long as we're all posting various ways to do this, here is one that:
  • takes a list of directory (folder) names from a file, one per line
  • doesn't require any special escaping (no need for quotes)
  • doesn't have any problems with unicode
Open TextEdit. Type the names of your folders one per line. Make sure there are no extra lines at the end of the files. Save this as, say "names.txt" (make sure you've done "Format -> Make Plain text" in TextEdit. Then in a terminal window:

python -c 'import sys,os,codecs;[os.mkdir(d) for d in codecs.open(sys.argv[1],"r","macroman")]' names.txt
This assumes that TextEdit uses macroman encoding when you save as plain-text. It did for me. If you've got a UTF8 editor to create names.txt with, then you'd do this instead:

python -c 'import sys,os,codecs;[os.mkdir(d) for d in codecs.open(sys.argv[1],"r","utf8")]' names.txt
I was unable to make Perl's mkdir work properly with unicode. And I don't know ruby well enough, though I'd be happy to see a ruby contribution.

j.

[ Reply to This | # ]

Create multiple folders from Terminal
Authored by: jaysoffian on Apr 26, '06 09:45:30PM
Ah, just realized you set the encoding in TextEdit when you save. In that case, select "UTF-8" and just use:

python -c 'import sys,os;[os.mkdir(d) for d in open(sys.argv[1])]' names.txt
Oddly, if you re-open that that file TextEdit, it improperly decodes it as MacRoman. Oh well.

[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: jaysoffian on Apr 26, '06 09:48:36PM

Okay, I win the award for replies to self. :-)

TextEdit -> Preferences -> Open and Save -> Plain text file encoding.

I had both opening/saving set to Automatic. Setting to UTF-8 does the right thing.

j.



[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: jaysoffian on Apr 26, '06 09:53:18PM
If you use UTF-8 when saving in TextEdit, this perl invocation will work:

perl -ple 'mkdir($_)' names.txt
:-)

[ Reply to This | # ]
Create multiple folders from Terminal
Authored by: ngb on Apr 27, '06 02:39:07PM

what, no ruby solution? ;-)



[ Reply to This | # ]