LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-24-2009, 11:59 AM   #1
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Rep: Reputation: 0
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
 
Old 04-24-2009, 01:33 PM   #2
Samotnik
Member
 
Registered: Jun 2006
Location: Belarus
Distribution: Debian GNU/Linux testing/unstable
Posts: 471

Rep: Reputation: 40
for i in $(find Dir1 -type d); do mkdir $i/temp; done
 
Old 04-24-2009, 01:34 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.)
 
Old 04-24-2009, 03:24 PM   #4
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 04-24-2009, 03:32 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 04-25-2009, 12:26 AM   #6
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 04-25-2009, 12:37 AM   #7
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Something like

find . -maxdepth 1 -type d -exec mkdir {}/temp \;
 
Old 04-27-2009, 01:51 PM   #8
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 04-27-2009, 03:39 PM   #9
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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.
 
Old 04-27-2009, 06:11 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
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/
 
Old 04-27-2009, 06:18 PM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursively chmod directories simonx Linux - Newbie 6 10-31-2007 01:01 PM
Remove directories recursively with same name cboyd Linux - Newbie 5 01-14-2007 09:48 PM
chmod directories recursively mfilippa Linux - Newbie 3 04-17-2006 07:24 PM
Recursively traversing sub-directories ark86 Linux - Newbie 2 01-29-2006 01:15 PM
Can ls recursively list only directories? Vosper Linux - General 3 07-16-2005 03:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration