1

I have a excel file which i will convert to csv or txt file with following data:

ALFA ROMEO > 147 > Scheinwerferblenden
ALFA ROMEO > 156 > Scheinwerferblenden
ALFA ROMEO > 156 > Kühlergrill
AUDI > 80 B3 > Heckspoiler
.
.

and so on

I need to create folders and subfolders based on this data with following syntax:

├───ALFA ROMEO
│            ├───147
│            │     └───Scheinwerferblenden
│            └───156
│                  ├───Scheinwerferblenden
│                  └───Kühlergrill        
│
└───AUDI
       └───80 B3
               └───Heckspoiler

I tried to write mkdir -p bash scripts but with no success.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

1

With the input you have provided I was able to accomplish this with the following command

while read -r dir; do mkdir -p ./"$dir"; done< <(sed 's@ > @/@g' input)

You can replace ./ with the directory path you would like the directory tree to start in, if not the current directory.

This uses sed to convert your input lines from something like:

ALFA ROMEO > 147 > Scheinwerferblenden

to:

ALFA ROMEO/147/Scheinwerferblenden

Then it feeds this output to a while loop that uses mkdir -p to create the directory tree.

$ cat input
ALFA ROMEO > 147 > Scheinwerferblenden
ALFA ROMEO > 156 > Scheinwerferblenden
ALFA ROMEO > 156 > Kühlergrill
AUDI > 80 B3 > Heckspoiler
$ while read -r dir; do mkdir -p ./"$dir"; done< <(sed 's@ > @/@g' input)
$ tree
.
├── ALFA\ ROMEO
│   ├── 147
│   │   └── Scheinwerferblenden
│   └── 156
│       ├── K\303\274hlergrill
│       └── Scheinwerferblenden
├── AUDI
│   └── 80\ B3
│       └── Heckspoiler
└── input

9 directories, 1 file
jesse_b
  • 37,005
0

Try this, based on your input (using bash parameter expansions) :

while IFS=">" read -r c1 c2 c3; do
    c2=${c2% }
    mkdir -p "${c1% }/${c2# }/${c3# }"
done < file-or-input

Output :

$ tree AUDI ALFA\ ROMEO/
AUDI
└── 80 B3
    └── Heckspoiler
ALFA ROMEO/
├── 147
│   └── Scheinwerferblenden
└── 156
    ├── Kühlergrill
    └── Scheinwerferblenden

7 directories, 0 files