• Home
  • /
  • Blog
  • /
  • Creating Multiple Folders from a Text File in Terminal

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!


You may also like

Last Updated: November 6, 2014

Read More

Last Updated: April 28, 2012

Read More
Leave a Repl​​​​​y

Your email address will not be published. Required fields are marked

  1. Great tip, very helpful!

    In case you want to do the same thing but create a list of files, use this in Terminal (same basic process applies)

    cat filelist.txt | xargs mkfile -n 1k

    Filelist.txt: contains your text file with one file name per line (if the file names have spaces, enclose them in double-quotes, like “Day 1-1 Notes.txt”

    mkfile settings: -n means new file, 1k means make the file size 1K. You can also set it to 0b for zero bytes

  2. When I tried this, I could only do it within my home folder, and so it produced 100+ folders in and among my “Documents”, “Pictures”, “Downloads”, etc. folders. Also, there was a question mark added to the beginning of each new folder’s name for some reason. Any ideas?

  3. 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.

  4. 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

  5. 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?

    1. 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.

      1. 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.

  6. 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.

    1. 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.

  7. 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.

    1. 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.

  8. 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.

    1. 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 :-)

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}