Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a full directory tree at once

Tags:

linux

bash

I would like to create a complex directory structure in a bash script and was under the impression that the following would work:

mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags} 

Which would create:

          tmpdir     ________|______    |        |      | branches   tags  trunk                    |                  sources                ____|_____               |          |           includes     docs 

However when I run my script I end up with:

tmpdir    | trunk 

Is there a quick and easy way to do this or am I going to have to resort to

mkdir -p tmpdir/trunk/sources/includes mkdir -p tmpdir/trunk/sources/docs mkdir -p tmpdir/branches mkdir -p tmpdir/tags 
like image 426
James Avatar asked Jul 16 '13 10:07

James


People also ask

How do I create a directory tree structure?

Creation of an entire directory tree can be accomplished with the mkdir command, which (as its name suggests) is used to make directories. The -p option tells mkdir to create not only a subdirectory but also any of its parent directories that do not already exist.

How do I make multiple directories in one command?

You can create directories one by one with mkdir, but this can be time-consuming. To avoid that, you can run a single mkdir command to create multiple directories at once. To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.

How do I create a directory and sub directory in Linux in one command?

If you want to create a directory containing several subdirectories, or a directory tree, using the command line in Linux, generally you have to use the mkdir command several times.

How will you create 100 files in Unix?

Just create a loop that generates a number and saves it to a file until it's created 100. Then read those 100 files into an array. Sort the array to have the numbers in order. Convert the array to a string, and save it to a new text file.


1 Answers

Change shebang to

#!/bin/bash 

to run the script with bash as it supports brace expansion.

The problem is that you are using shell that does not support it. Your /bin/sh does not point to /bin/bash but to something like /bin/dash.

https://wiki.ubuntu.com/DashAsBinSh#A.7B

like image 144
Grzegorz Żur Avatar answered Sep 18 '22 15:09

Grzegorz Żur