Create child directories from parent directory

A parent directory has many directories, for example:

$ cd parent
$ ls
ALA_31_C ALA_31_D ALA_31_G ALA_31_L

I want to create the same name directory within each child directory, something like this:

$ cd parent
$ ls
ALA_31_C ALA_31_D ALA_31_G ALA_31_L
$ cd ALA_31_C
$ ls
ALA_31_C

Similarly, I need to create it for all remaining directories: ALA_31_D, ALA_31_G and ALA_31_L from the parent directory.

1

2 Answers

A simple Bourne shell script will do it:

#!/bin/sh
for dir_name in */ ; do echo "$dir_name" mkdir "$dir_name/$dir_name"
done 

Loop through only the directories (*/), and for each directory, make a child with the same name as the parent (mkdir "$dir_name/$dir_name").

This only works for one level - it is not recursive - which is probably what you want anyway.

For more examples, see How do I loop through only directories in bash?

I know I'm a little late to the game here, but Occam had it right (the simplest solution...):

mkdir -p ~/folder/{child1,child2,child3,child4}

This gives the results:

user@computer:~/folder/$ ls -a
. .. child1 child2 child3 child4

The folders are not nested, but all folders are children of the parent!

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like