0

I have the following folder structure and have 20-30 subfolders inside the last folder and I wanted to see what is the quickest way to come up with all these folders on my machine using linux commands.

My Folder structure:

Folder A ---- Project A -----Sample 1
Folder A ---- Project A -----Sample 2
Folder A ---- Project A -----Sample 3

So you can see above, there are three levels - Folder, then Project and then Samples.

iF I use a text file, or whatever you recommend, what will be the best way to do it.

Tanktalus
  • 21,664
  • 5
  • 41
  • 68
mywayz
  • 11
  • 1
  • 2
  • 1
    Possible duplicate of [How to create nonexistent subdirectories recursively using Bash?](https://stackoverflow.com/questions/1731767/how-to-create-nonexistent-subdirectories-recursively-using-bash) – IoCalisto Jul 15 '19 at 14:33
  • 1
    @IoCalisto, I think these fall into slightly different use cases. The other question doesn't cover multiple directories at the leaf directory. – Mason Jul 15 '19 at 14:44
  • Yes, I want subfolders with specific names. How do I create such hierarchy with linux – mywayz Jul 15 '19 at 16:17

1 Answers1

0

To create exactly what is described above, this Bash one-liner should work.

for i in {1..30};do mkdir -p Folder_A/Project_A/Sample_"$i";done creates:

├── Folder_A
│   ├── Project_A
│       ├── Sample_1
│       ├── ...
│       ├── Sample_30

If you want something more complex, you could do something like

for f in top_level_1 top_level_2; do for i in sub_folder_1 sub_folder_2;do mkdir -p Folder_A/$f/$i;done;done

or make this a full bash script and execute it

#!/bin/bash
for f in mid_level_1 mid_level_2;
        do for i in sub_folder_1 sub_folder_2;
                do mkdir -p parent_directory/$f/$i
        done
done

This gives this structure:

├── parent_directory
│   ├── mid_level_1
│       ├── sub_folder_1
│       ├── sub_folder_2
│   ├── mid_level_2
│       ├── sub_folder_1
│       ├── sub_folder_2

You can change what directories are made by changing the list of directories after the word for. Instructions for bash for loops can be found here

If you need anything even more complex, use these principles and try writing a bash script that can do it. I think that is where the fun of programming/scripting really is.

Mason
  • 4,326
  • 1
  • 12
  • 19
  • what does "I" mean here when you write "for I"? Is there a way to list in a text file to just create ll the folders so that fit can follow the hierarchy listed in the text file? – mywayz Jul 15 '19 at 15:56
  • "i" is used in the for loop, like used in most programming languages. – Mason Jul 15 '19 at 16:01
  • Okay, but with the above command, how would it know what folder name to give to my subfolders.i don't want just random 30 folders to be created. I want with specific names. – mywayz Jul 15 '19 at 16:16
  • I'll update my answer to make it more clear how you do that. – Mason Jul 15 '19 at 16:22
  • Thanks Mason, will wait for the updated answer. It will be helpful to me. Thanks – mywayz Jul 15 '19 at 16:52
  • @mywayz, Hope this is helpful – Mason Jul 15 '19 at 18:07
  • Thanks Mason, I will try this. today and get back to you – mywayz Jul 17 '19 at 15:10