LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   recursively making directories (http://www.linuxquestions.org/questions/linux-newbie-8/recursively-making-directories-721435/)

will.flanagan 04-24-2009 12:59 PM

recursively making directories
 
Hi linux gurus,

I am trying to find a way to recursively make directories.

For example, if I have three directories below me:

Dir1/ Dir2/ Dir3/

and I want to make a directory called "temp" in each so that it would look like:

ls */temp
Dir1/temp Dir2/temp Dir3/temp

Is there an easy way to do this with the mkdir command?

Thanks in advance!
-Will

Samotnik 04-24-2009 02:33 PM

for i in $(find Dir1 -type d); do mkdir $i/temp; done

anomie 04-24-2009 02:34 PM

How about:
Code:

$ for _i in dir1 dir2 dir3 ; do mkdir -p ${_i}/tmp ; done
(-p option is not necessary if the parents directories already exist, BTW.)

will.flanagan 04-24-2009 04:24 PM

Is it possible to be more general?
 
Thanks anomie and Samotnik,

Is it possible to make the command more general? I ask because I have hundreds of directories with very different names.

For example, if my directory names are something like:

Steve/ Joe/ Bob/ .....(hundreds).... Phil/

and I want to put a temp/ subdirectory in each...

Is there a way to do this?

Also, can I do something like:
for i in $(find Dir1 -type d); do mkdir $i/temp; done

on the command line, or does this need to be in a script? I've never written a script before, so its probably about time I learned how...

Cheers
Will

anomie 04-24-2009 04:32 PM

A find invocation like this should do the trick for you:
Code:

find . -maxdepth 1 -type d
(Note that .hidden directories will be matched by that.)

You can incorporate that (or some version of it) into what you have so far. What you've posted does not need to be in a script. You can run it from one, or you can run it directly on the command line.

As always, test this first in a safe area before you try to run it on important directories.

will.flanagan 04-25-2009 01:26 AM

Thanks anomie...

I'm afraid I'm a bit of a newbie though... How would I incorporate the find bit into the previous commands?

Also, I'm afraid I haven't seen many command line arguments with $ and ;... Are these scripts on the command line? Is there anywhere that would be good to read up on this?

Cheers,
Will

billymayday 04-25-2009 01:37 AM

Something like

find . -maxdepth 1 -type d -exec mkdir {}/temp \;

will.flanagan 04-27-2009 02:51 PM

Still having some troubles
 
Thanks for the help, but it still seems to not quite be working.

I haven't ever used commands with stuff like {}, \, and ;. Is there a tutorial that could explain what this stuff means to a newbie like me?

Thanks for the help!
Will

billymayday 04-27-2009 04:39 PM

That should work - I tested it on my system.

Anyway, the strange characters you question are all specific in this instance to the find command, so see "man find". In short, {} is replaced by the filenames found when used in an -exec option, and \; terminates the -exec.

chrism01 04-27-2009 07:11 PM

You should bookmark and read these:

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

jschiwal 04-27-2009 07:18 PM

It is easy to do using the -p (parent) option and bash's brace expansion

Code:

mkdir -p Dir{1..3}/temp

mkdir -p Dir{1,2,3}/temp

sudo mkdir -p /srv/samba/{john,sally,mike}/{Documents,Downloads}

If you have gaps in a sequence, you can separate each number with commas. If you have a complete sequence of numbers, or letters, you can use the double dot from.

You can't mix them however:
Dir{1,3,5..8} won't work.


All times are GMT -5. The time now is 01:00 PM.