8

I am learning Unix online and i came across this question to create a hierarchical structure. I have created the directories using mkdir command but I am stuck while creating the files inside directory.

Directory Structure to be created

My command for creating directories is

mkdir -p mydir/{colors/{basic,blended},shape,animals/{mammals,reptiles}}
wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • 6
    Well, what touch command(s) did you try? what happened? – steeldriver Jun 23 '17 at 16:03
  • I tried this; touch mydir/{colors/{basic/{red,blue,green},blended/{yellow,orange,pink}}, shape,animals/{mammals/{pltypus,bat,dog},reptiles/{snakes,crocodile,lizard}}}. But i am getting an error stating "Cannot touch : No such file or directory found" – Neeraj gupta Jun 28 '17 at 13:32
  • 1
    Practical approach. 1. Try replacing touch by echo to see if there are typos in your command. 2. Try with smaller examples and make them bigger gradually until you find the issue. – user334639 Sep 26 '17 at 22:34
  • A related question, from the same homework, is https://askubuntu.com/questions/916727/ . – JdeBP Aug 10 '18 at 13:13

2 Answers2

13

Here's two ways to do it. There's more, but the concept would be the same: either extending what you have, or iterating over a list and breaking each list item into parts.

Long way to do it

There's nothing special that needs to be done with touch. Simply extend the same arguments that you had for mkdir command to include files.

bash-4.3$ mkdir -p mydir/{colors/{basic,blended},shape,animals/{mammals,reptiles}}
bash-4.3$ tree mydir
mydir
├── animals
│   ├── mammals
│   └── reptiles
├── colors
│   ├── basic
│   └── blended
└── shape

7 directories, 0 files
bash-4.3$ touch mydir/{colors/{basic/{red,blue,green},blended/{yellow,orange,pink}},shape/{circle,square,cube},animals/{mammals/{platipus,bat,dog},reptiles/{snakes,crocodile,lizard}}}
bash-4.3$ tree mydir
mydir
├── animals
│   ├── mammals
│   │   ├── bat
│   │   ├── dog
│   │   └── platipus
│   └── reptiles
│       ├── crocodile
│       ├── lizard
│       └── snakes
├── colors
│   ├── basic
│   │   ├── blue
│   │   ├── green
│   │   └── red
│   └── blended
│       ├── orange
│       ├── pink
│       └── yellow
└── shape
    ├── circle
    ├── cube
    └── square

Short way

If you observe, all of your directories have files to create. Thus what we can do is create list of items( effectively a bash array ) and iterate over them, with using mkdir with suffix removal and then touch. Like this:

bash-4.3$ arr=( mydir/{colors/{basic/{red,blue,green},blended/{yellow,orange,pink}},shape/{circle,square,cube},animals/{mammals/{platipus,bat,dog},reptiles/{snakes,crocodile,lizard}}} )
bash-4.3$ for i in "${arr[@]}"; do  mkdir -p "${i%/*}" && touch "$i"; done
bash-4.3$ tree mydir
mydir
├── animals
│   ├── mammals
│   │   ├── bat
│   │   ├── dog
│   │   └── platipus
│   └── reptiles
│       ├── crocodile
│       ├── lizard
│       └── snakes
├── colors
│   ├── basic
│   │   ├── blue
│   │   ├── green
│   │   └── red
│   └── blended
│       ├── orange
│       ├── pink
│       └── yellow
└── shape
    ├── circle
    ├── cube
    └── square

7 directories, 15 files

Side note: if you have spaces in any file or directory name, ensure that you single or double quote those items, e.g.:

arr=( mydir/{'with space',without_space/{file1,file2}} )

See also.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • I tried the first one but i am getting an error stating "Cannot touch : No such file or directory found". Please Help. – Neeraj gupta Jun 28 '17 at 13:37
-2
 mkdir -p mydir/{colors/{basic,blended},shape,animals/{mammals,reptiles}}
 touch mydir/colors/{basic/{red,blue,green},blended/{yellow,orange,pink}}
 touch mydir/shape/{circle,square,cube}
 touch mydir/animals/{mammals/{platypus,bat,dog},reptiles/{snakes,crocodile,lizard}}

this is perfect solution.....

waltinator
  • 36,399