How Productive Are You? Take Our Productivity Quiz To Find Out

Creating Multiple Folders from a Text File in Terminal

By | 13 comments

At Thanh’s request, I’m sharing this.┬áConsider it a sneak preview for our upcoming “AE Labs” project.

One of  the ways that we organize ourselves at Asian Efficiency is to have one folder per article or post we write. This is used for storing the article, the mind map, images and anything else relevant. Think of it as a project folder.

Now obviously, efficiency comes from doing things in batches. So, how do you make 40+ folders at the same time?

1. Create a text file with folder names. I simply did this by copying out of our planning spreadsheet, replacing spaces with dashes and adding dates (all easily done in Excel). We then get something that looks like this:

20120601-article1
20120602-article2
20120603-article3
20120604-article4
20120605-article5
20120606-article6
20120607-article7
20120608-article8
20120609-article9
20120610-article10
20120611-article11
20120612-article12
20120613-article13
20120614-article14
20120615-article15
20120616-article16
20120617-article17
20120618-article18
20120619-article19
20120620-article20

Note that the date format of YYYMMDD allows computers to sort dates in ascending or descending orders easily, and completely eliminates any confusion caused by US-or-rest-of-the-world date formats.

2. Save as folders.txt in a temporary directory.

3. Open Terminal (Mac/Unix only). Not sure about Windows PowerShell. Go to the folder where folders.txt is.

4. Type the command:

$ cat folders.txt | xargs mkdir

I am by no means a proficient terminal user or programmer, but let me try to break that down.

$ is the standard prompt for Terminal. You don’t type it, it appears when you first launch Terminal.

cat stands for concatenate. Which means it takes folders.txt and reads it sequentially (line by line) then outputs it.

| is a pipe, which means it takes whatever is to the left of the pipe and outputs it to the command on the right of the pipe.

xargs I have no idea. Wikipedia has a complicated explanation. I’m thinking of it as “compleX ARGumentS“, meaning it takes a long list of something and lets other commands interpret it easier.

mkdir stands for make directory.

5. You’re done!

How Productive Are You?

Take our Productivity Quiz to find out. Learn the tactics and techniques that will work for you.

13 Comments

Posted by kitty  | February 27, 2017 at 6:18AM | Reply

how to create multiple folders from txt file and excel file, i trying to create 100 folders in ramdom codes. would you please help me, im newbie in unix environment.

Posted by TC  | March 18, 2015 at 11:28AM | Reply

xargs: unterminated quote means that in your text file, you probably have an ‘ or an ” in the name.

Posted by Kimberly Zimmerman  | December 14, 2012 at 12:10AM | Reply

I appreciate your post here Mr. Pham.
I am working to autocreate 738 folders
I have a text file of the folder names
Here is the error I receive:
Kimberly-Zimmermans-MacBook-Pro:temp kimberlyzimmerman$ cat folders.txt | xargs mkdir
xargs: unterminated quote
Kimberly-Zimmermans-MacBook-Pro:temp kimberlyzimmerman$

What does unterminated quote mean?
Thank you kindly

Posted by Basil  | October 23, 2012 at 7:38PM | Reply

When I followed these instructions with the following command:
$ cat folders.txt | xargs mkdir

on OSX 10.8.2 I get the following output error in terminal:

Last login: Tue Oct 23 19:36:04 on ttys000
Basils-iMac:United basilbabaa$ cat folders.txt | xargs mkdir
mkdir: 570794\r942970\r862753\r828649\r824309\r739813\r821033\r835299\r855291\r
953837\r943141\r725904\r688641\r764088\r722626\r743086\r738858\r723431\r688281\r
632846\r731605\r840810\r883648\r853775\r667601\r697051\r755493\r819548\r822878\r
947228\r844878\r960187\r961930\r979647\r815371\r689145\r686974\r686558\r: File name too long
Basils-iMac:United basilbabaa$

Any ideas what I am doing wrong?

Posted by Thanh Pham  | October 23, 2012 at 11:44PM

I have 10.8.2 as well and it worked fine. I suspect there is something wrong with your folders.txt file. Maybe the Returns/Enters/Line Returns are incorrect. Try using another text editor and see if that works.

Posted by Julian  | July 19, 2014 at 4:53PM

Hi Basil, I had the same problem initially. I had an .xls file containing a list of names for my desired folders. I used xcel to save this list as a .txt file. Although each name was in its own row in the .txt, they were not separated by spaces, only line returns. Figure out how to effectively add a space between each name, or terminal thinks it’s just one massively long name (all your names pasted together). I’m a super novice with this, so I just figured it out trial and error and added a space manually (I didn’t have that many names). I don’t know the technical explanation, but hopefully that makes enough sense.

Posted by Alan  | September 20, 2012 at 5:35AM | Reply

Last login: Thu Sep 20 10:32:52 on ttys000
alan-evanss-macbook-pro:~ alanevans$ cat folders.txt | xargs mkdir
cat: folders.txt: No such file or directory
alan-evanss-macbook-pro:~ alanevans$

This is the message I get. Sorry, I am a complete novice here.

Posted by Thanh Pham  | October 23, 2012 at 11:42PM

Alan, you need to navigate around using the “cd” command. If you save the folders.txt file in your home folder, then it should work. Right now you have that file stored elsewhere. Hence, the error message.

Posted by Alan  | September 20, 2012 at 5:32AM | Reply

Hi Thanh, many thanks.

Posted by Alan  | September 19, 2012 at 9:00PM | Reply

Hi, I have followed the steps and I am trying to create multiple folders with passwrod combinations using letters lower and upper case. Where do I save the folders.txt file? When I save it and then enter the $ cat folders.txt | xargs mkdir in terminal it says no such file can be found. I must be doing something wrong.

Posted by Thanh Pham  | September 20, 2012 at 12:41AM

Hi Alan,

Make sure when you use terminal that you’re in the same directory as where you text file is. So use the command “cd” to navigate.

Posted by David C.  | June 2, 2012 at 6:41AM | Reply

Cool idea.

I don’t have a need to plan folders that far out in advance, but I do like to create common folder structures for different projects.

As an example:
function site () {
#based on http://minimaldesign.net/articles/read/the-ultimate-web-dev-setup
filename=${1:-$(date “+%y%m%d-%H%M%S”)}
mkdir -p $filename/{_from-client,01-Strategy,02-Structure,03-Design/Assets/{_css,_img,_js},04-Production}
touch $filename/03-Design/index.html
mate $filename
}

The previous code gets added to my .profile file (I’m using bash). The I can call “site SITENAME” and it will create the folder named SITENAME in whatever directory I’m in. That folder will have my other folders ready to collect and organize info and add a base html file. There’s even a very simple check for when no argument is supplied. The final bit (mate $filename) opens the entire project into Textmate so I can get straight to work.

I have a few different functions set up this way for various tasks. Keeps things nice and tidy. I’m guessing you could use xargs to pipe into this function too (not confirmed).

“AE Labs”, eh? Looking forward to it. Thanks for the great articles.

Posted by Thanh Pham  | June 3, 2012 at 7:00AM

Thanks for sharing that snippet David! I’m a big fan of having useful shell scripts to make life easier. And yes, AE labs will be something exciting and coming soon :-)

Leave a Reply