0

I'm trying to create a simple shell script for recursively creating directories inside a list of directories.

I have the next file structure: A directory called v_79, containing a list of "dirs" (from dir_0 to dir_210), and inside each of them there are several directories called ENSG00000??????, where '?' stands for a character between [0-9].

I would like to create a directory called "my_dir" inside every one of the ENSG00000????? dirs.

I know how to create a directory once being inside each of the dir_XX 's,

    for i in ENSG00000??????; do mkdir $i/my_dir; done

but I don't know how to create the directory that I need, in the v_79 directory.

4
  • 1
    This just needs an outer loop to iterate over the dir_0 to dir_210 structure Commented Jun 23, 2011 at 12:00
  • 2
    That's an incredibly large amount of directories to create - 422 million?? dir_0 to dir_210 = 211 x ENSG00000000000 to ENSG00000999999 = 1,000,000 + another million for the my_dir's per directory, so 2 million per dir_n = 422 million. Unless you have explained something wrong, you'll struggle to get that on to any file system. Or are the ENSG00000?????? already existing? Commented Jun 23, 2011 at 12:05
  • 2
    "I know how to create a directory once being inside ... " - no, I don't think you do :-) That will only attempt to create directories that already exist. Commented Jun 23, 2011 at 12:08
  • @Orbling: Not in every directory must there be 1.000.000 dirs; (from 0 to 999.999). In fact its no more than 100 dir with the name ENSG?????? the numbers are only for nomenclature purposes. Sorry! Commented Jun 23, 2011 at 12:56

7 Answers 7

3

If current dir is v_79, you can use a combination of find and xargs:

find . -name 'ENSG00000......' -type d | xargs -I DIR mkdir DIR/my_dir 
Sign up to request clarification or add additional context in comments.

Comments

2

if your current directory contains directory "v_79", then

for dir in v_79/dir_{0..210}/ENSG00000??????; do mkdir $dir/my_dir; done

I wonder if that might give you an "argument list too long" error, in which case find is the way to go.

1 Comment

Its perfect, glenn. This did the job. Thx so much!
2
mkdir -p v_79/dir{0,1}{1,2,3}

will create the directories v79/dir01, v79/dir02, v79/dir03, v79/dir11, v79/dir12 and v79/dir13 even if v_79 does not exist.

The -p options will create all required directories recursively.

1 Comment

The directories dir_xx already exist; i don't need to create them, and as @mouviciel said, it doesn't create the my_dir directory inside. Thx anyway!
1

You can do so from your v_79 directory:

for i in `find . -type d -name "ENSG00000??????"`; do mkdir $i/my_dir; done

1 Comment

This will create directories called my_dir inside of directories in the current directory called find, -type, d, -name and ENSG00000??????, but only if those directories already exist. You meant to say for i in $(find . -type d -name "ENSG00000??????") ; do mkdir -i $i/my_dir ; done, but this will also only create the subdirectory if the parent already exists.
1

this is for dry run - if satisfied, delete the echo before mkdir

echo ./v_79/**/ENSG* | xargs -I% echo mkdir %/my_dir #or
echo ./v_79/**/dir_*/ENSG* | xargs -I% echo mkdir %/my_dir

you need for this bash4 and "shopt -s globstar" (e.g. in your profile)

If you have too much directories, you may get "argument list too long" error (for the 1st echo). In this case the the solution with the find is better

find v_79 -type d -print | grep '/ENSG' | xargs -I% echo mkdir %/my_dir
  • find all directories in v_79
  • filter out only these with name ENSG (you can add more "filters")
  • run (echo) mkdir for the result

is somewhere in the path can be space, modify the above with:

find v_79 -type d -print0 | grep -z '/ENSG' | xargs -0 -I% echo mkdir %/my_dir

Also, you can limit the depth of the find command, e.g.:

find v_79 -depth 2 -type d -print0 | grep -z '/ENSG' | xargs -0 -I% echo mkdir %/my_dir

again, all above is for the dry run - remove the echo for the run. ;)

1 Comment

I already did it with the solution in the first answer. :) Anyway, i will vote up your solution, because as I m a newb, its always interesting a solution with the code explained as u did... ;)
0

Just add the -p option, then your work will done.

BTW: -p option for mkdir command means "no error if existing, make parent directories as needed"

Comments

0

You want

mkdir v_79/dir_{0,1,2}{,0,1,2,3,4,5,6,7,8,9}{,0,1,2,3,4,5,6,7,8,9}/ENSG00000??????/my_dir

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.