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)?
8 Answers
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
test01throughtest10. - 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:
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.
- 3
- 851
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.
- 171
To actually create directories from a comma-separated list of names (thanks to muru for the printf tip):
printf '%s' foo,bar,baz | xargs -d, mkdir
% printf '%s' foo,bar,baz | xargs -d, mkdir
% ls -la
total 0
drwxr-xr-x 1 dev users 18 Oct 24 11:36 .
drwxr-xr-x 1 dev users 90 Oct 24 11:34 ..
drwxr-xr-x 1 dev users 0 Oct 24 11:36 bar
drwxr-xr-x 1 dev users 0 Oct 24 11:36 baz
drwxr-xr-x 1 dev users 0 Oct 24 11:36 foo
You could wrap the command into a function for ease of use:
mkdir_cs() {
printf '%s' "${1}" | xargs -d, mkdir
}
% mkdir_cs() {
printf '%s' "${1}" | xargs -d, mkdir
}
% mkdir_cs foo,bar,baz
% ls -la
total 0
drwxr-xr-x 1 dev users 18 Oct 24 11:37 .
drwxr-xr-x 1 dev users 90 Oct 24 11:34 ..
drwxr-xr-x 1 dev users 0 Oct 24 11:37 bar
drwxr-xr-x 1 dev users 0 Oct 24 11:37 baz
drwxr-xr-x 1 dev users 0 Oct 24 11:37 foo
- 41,268
mkdir command takes multiple arguments simply run as below
mkdir dir1 dir2 dir3 ... dirN
If you would like to create multiple subdirectories then you can pass those argument in {} as shown below (use only commas to separate the argument, without spaces).
mkdir -p dir1 dir2/{subdir1,subdir2,subdir3,subdirN} dir3 dirN
Using the option "-p" to make parent directories as needed.
- 3
- 261
So you want comma separated list of directory names ? That can be done.
Shell + coreutils
Since everybody is posting oneliners, here's mine as well ( mkdir + parameter substitution plus + shell redirection ).
DIR:/testdir
skolodya@ubuntu:$ ls
DIR:/testdir
skolodya@ubuntu:$ mkdir $( tr '[,\n]' ' ' < /home/xieerqi/dirList.txt )
DIR:/testdir
skolodya@ubuntu:$ ls
dirFive/ dirfour/ dirone/ dirthree/ dirtwo/
AWK
AWK is a text processing language, but it has very nice system() function which will call the default shell , and run command[s] enclosed in parenthesis ( which must be a string).
DIR:/xieerqi
skolodya@ubuntu:$ awk -F ',' '{for(i=1;i<=NF;i++) system("mkdir "$i)}' dirList.txt
DIR:/xieerqi
skolodya@ubuntu:$ ls -ld dir*
-rw-rw-r-- 1 xieerqi xieerqi 23 Feb 9 11:41 dirList.txt
drwxrwxr-x 2 xieerqi xieerqi 4096 Feb 9 11:42 dirone/
drwxrwxr-x 2 xieerqi xieerqi 4096 Feb 9 11:42 dirthree/
drwxrwxr-x 2 xieerqi xieerqi 4096 Feb 9 11:42 dirtwo/
DIR:/xieerqi
skolodya@ubuntu:$ cat dirList.txt
dirone,dirtwo,dirthree
Or you could remove , with gsub() function, and call system("mkdir "$0) but that may be a problem if you want to create directories with spaces in their name
Python
Pythonic way of doing the same , would be to read each line, get rid of trailing \n , shove everything into one list, and iterate over the list items and create dirs per list item. Note that in the example bellow, /home/xieerqi/dirList.txt is the full path given to my file, and we make up full path of each new directory by joining string /home/username/ and the dir name read from list. Substitute your own values as necessary
DIR:/testdir
skolodya@ubuntu:$ ls
DIR:/testdir
skolodya@ubuntu:$ /home/xieerqi/makeDirs.py
DIR:/testdir
skolodya@ubuntu:$ ls
dirFive/ dirfour/ dirone/ dirthree/ dirtwo/
DIR:/testdir
skolodya@ubuntu:$ cat /home/xieerqi/makeDirs.py
#!/usr/bin/env python
import os
with open("/home/xieerqi/dirList.txt") as file:
for line in file:
for directory in line.strip().rsplit(','):
path = '/home/xieerqi/testdir/' + directory
os.makedirs(path)
- 107,582
Simply use the -p argument:
mkdir -p app/code/Foo/Bar
This is what the help page says about -p. I would not understand it by that text...
-p, --parents no error if existing, make parent directories as needed
- 844