MyTetra Share
Делитесь знаниями!
Is there a way to create multiple directories at once with mkdir?
Время создания: 24.10.2018 14:44
Текстовые метки: linux mkdir multiple directory
Раздел: Linux

Short answer

mkdir takes multiple arguments, simply run

mkdir dir_1 dir_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 through test10.

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.

It's very simple, lets you want to create a directory structure such as:







You can do it in a single command like this:

mkdir -p Website/{static/{cs,js},templates/html}

Be careful not to enter any spaces

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.

Yes, 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 Feb 10 '16 at 18:41

Yeah - you can't even successfully do any kind of escaping for the spaces, either - junk\ dir in the list file gives two directories, junk\ and dir. Gave me a panic when I saw a \ in a directory name. – Jon V Feb 11 '16 at 0:02

This can be combined with the -p flag mentioned in other answers. If so, the list file doesn't have to include parent directories as their own lines, they will be detected and made automatically. – mcw0933 Jul 20 at 14:19

Так же в этом разделе:
MyTetra Share v.0.53
Яндекс индекс цитирования