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 ENSG00开发者_如何学C000??????; do mkdir $i/my_dir; done
but I don't know how to create the directory that I need, in the v_79 directory.
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
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.
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.
You can do so from your v_79 directory:
for i in `find . -type d -name "ENSG00000??????"`; do mkdir $i/my_dir; done
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. ;)
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"
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
精彩评论