Is there a way to create multiple directories at once with mkdir?
Solution 1
Short answer
mkdir
takes multiple arguments, simply run
mkdir dir_1 dir_2
Solution 2
You can use lists to create directories and it can get pretty wild.
Some examples to get people thinking about it:
mkdir sa{1..50}
mkdir -p sa{1..50}/sax{1..50}
mkdir {a-z}12345
mkdir {1,2,3}
mkdir test{01..10}
mkdir -p `date '+%y%m%d'`/{1,2,3}
mkdir -p $USER/{1,2,3}
- 50 directories from sa1 through sa50
- same but each of the directories will hold 50 times sax1 through sax50 (-p will create parent directories if they do not exist.
- 26 directories from a12345 through z12345
- comma separated list makes dirs 1, 2 and 3.
- 10 directories from
test01
throughtest10
. - same as 4 but with the current date as a directory and 1,2,3 in it.
- same as 4 but with the current user as a directory and 1,2,3 in it.
So, if I understood it correctly and you want to create some directories, and within them new directories, then you could do this:
mkdir -p sa{1..10}/{1,2,3}
and get sa1, sa2,...,sa10 and within each dirs 1, 2 and 3.
Solution 3
It's very simple, lets you want to create a directory structure such as:
Websites/
static/
cs
js
templates/
html and xhtml
You can do it in a single command like this:
mkdir -p Website/{static/{cs,js},templates/html\ and\ xhtml}
Be careful to escape the spaces in your directory names.
Solution 4
Make a list of the names for your desired directories using line breaks instead of commas as a separator. Save that list.
mkdir `cat list`
You should now have all the directories named in your list.
Solution 5
Something like this? (thanks to muru for the printf
tip)
printf '%s' 'foo,bar,baz' | xargs -d, mkdir
$ ls
$ printf '%s' 'foo,bar,baz' | xargs -d, mkdir
$ ls
bar baz foo
$
You can wrap it into a function for ease of use:
function mkdir_cs {
printf '%s' "$1" | xargs -d, mkdir
}
$ ls
$ mkdir_cs 'foo,bar,baz'
$ ls
bar baz foo
$
Related videos on Youtube
BGroat
Just getting started in Web Development, eager to learn as much as I can Languages: HTML, CSS, Javascript, JQuery, Ruby. Awful at all of them
Updated on September 18, 2022Comments
-
BGroat almost 2 years
If I wanted to create multiple directories (on the same level) and then feed it a comma seperated list of directory names (or something to that effect)?
-
Sparhawk over 8 years
man mkdir
.... -
David Richerby over 8 yearsAny time you want to know if a command can do some thing you want done, type
man
followed by the name of the command. -
Jacob Vlijm over 8 years@DavidRicherby although
man mkdir
"kind of" suggests the possibility to create multiple directories at once, it is not mentioned specifically, nor how to do that. -
David Richerby over 8 years@JacobVlijm The GNU coreutils 8.21 manpage for mkdir says, "NAME mkdir - make directories // SYNOPSIS mkdir [OPTION]... [DIRECTORY]... // DESCRIPTION Create the DIRECTORY(ies) if they do not already exist." That seems pretty clear that you can make more than one directory.
-
Jacob Vlijm over 8 years@DavidRicherby like I said "suggests the possibility to create multiple directories at once, it is not mentioned specifically, nor how to do that"
-
Blaisorblade over 8 years@JacobVlijm Man pages jargon is not obvious without instruction, but in the technical language of man pages, the info is totally clear, even in the given quote. Note the
...
after[DIRECTORY]
, meaning that argument can be repeated. SoDIRECTORY(ies)
refers to all passedDIRECTORY
arguments. However, "RTFM" comments sometimes forget there is a notation to learn.
-
-
muru over 8 yearsJust a
printf "%s" "$1" | xargs -d, mkdir
should be enough. -
Slipp D. Thompson over 8 yearsI agree with @gniourf_gniourf… there are no usages of regular expressions in your answer. Did you mean to say something else, or to provide a different example?
-
Rinzwind over 8 years@gniourf_gniourf done :)
-
David Richerby over 8 yearsI don't think the asker actually wants the list to be comma-separated; I'm pretty sure that was just their guess at what the syntax might be.
-
Sergiy Kolodyazhnyy over 8 yearsYes, that will work, however , with a caveat - the directory names have to be one whole string. If one line is
spaced dir
, then it will create two dirs ,spaced
and `dir. -
Sergiy Kolodyazhnyy over 8 yearsWell, OP probably didn't to exactly have comma-separated list, however it's sure may be used as a method, point being " lists can be used,too, you don't have to type out name of each dir individually". Especially that will be useful when a big list cannot be generated with ranges, like
dir{1..1000}
. Say , you want to make a directory per username, or per project, or per geographic location, like a city -
Jon V over 8 yearsYeah - you can't even successfully do any kind of escaping for the spaces, either -
junk\ dir
in thelist
file gives two directories,junk\
anddir
. Gave me a panic when I saw a\
in a directory name. -
deed02392 over 8 yearsThis answer depends on the shell doing expansion of your input before providing that input as arguments to mkdir. It's much more accurate to just say that mkdir can create multiple dirs with multiple arguments and then talk about how a given shell can make that easier.
-
Plinth over 7 yearsIf I try to do something like
mkdir sa$(seq 0.1 0.1 1)
, all it does is create one directory named sa0.1 and the rest 0.2, 0.3, ..., 1. How can I get decimal brace expansion without writing the entire list? -
runlevel0 over 7 years
man 1 seq
or even betterinfo seq
are your friends. This will do the job:for f in $(seq -f %g 0.2 1 10) ; do mkdir -p test/$f ; done
it will create a directory test with subdirs 0,2 up to 9,2 Note that my local is Dutch, if LC_TYPE is en_US you will get the dot as decimal separator. The key is thatseq
accepts printf syle formatting. -
Isaac Gregson over 7 yearsYou can also press tab here to expand them before running the command.
-
Dominic Motuka about 6 yearsI think you meant
mkdir {a..z}12345
notmkdir {a-z}12345
-
mcw almost 6 yearsThis can be combined with the
-p
flag mentioned in other answers. If so, thelist
file doesn't have to include parent directories as their own lines, they will be detected and made automatically. -
balaji over 4 yearsThanks you working file .....
-
Manuel Jordan over 2 years@Rinzwind just being curious, what is the difference between
mkdir {1,2,3}
andmkdir 1 2 3
? practically both do the same, so when is mandatory the former? -
Mark Lee over 2 yearsMake sure the 'list' file has no file ending. list.txt will result in the creation of the directory list.txt in the location of the file. And those are backticks, not single quotes: `cat list` not 'cat list'.