LinuxQuestions.org
Review your favorite Linux distribution.
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 07-22-2004, 12:48 PM   #1
dominant
Member
 
Registered: Jan 2004
Posts: 409

Rep: Reputation: 30
creating sub-sub directories


thereis a directory

/home/dominant
/home/dominant1
/home/dominant2
/home/dominant3
/home/dominant4


and i want to create a dir under every dominant directory


/home/dominant/new_dir
/home/dominant1/new_dir
/home/dominant2/new_dir
/home/dominant3/new_dir
/home/dominant4/new_dir




can the above be done?

I would also chown these new_dir directories.
 
Old 07-22-2004, 01:11 PM   #2
fisheromen1031
Member
 
Registered: Jun 2004
Location: Lone Star State
Distribution: WinXP Pro, Puppy Linux, Ubuntu Warty & Hoary
Posts: 46

Rep: Reputation: 15
you might try:
mkdir /home/*/new_dir
 
Old 07-22-2004, 01:30 PM   #3
dominant
Member
 
Registered: Jan 2004
Posts: 409

Original Poster
Rep: Reputation: 30
it doesn't work

Last edited by dominant; 07-22-2004 at 01:53 PM.
 
Old 07-22-2004, 01:43 PM   #4
linux_ub
Member
 
Registered: May 2004
Location: NY
Distribution: fedora core 1
Posts: 65

Rep: Reputation: 18
how abt writing a script tht will do a 'ls' and then for each entry in the ls ... execute mkdir /home/dominant/new_dir
also split the listing on / and get the 'dominant' to chown the new dir

am not sure ... am also a newbie to linux but this is how i would try
hope it helps
 
Old 07-22-2004, 02:02 PM   #5
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Code:
for dir in `ls -F`; do mkdir $dir"new_dir"; done
Run if from the parent folder of dominant1 and dominant2 etc.
 
Old 07-22-2004, 04:31 PM   #6
buffed317
Member
 
Registered: Jun 2004
Location: New Jersey, USA
Distribution: Slackware 11
Posts: 191

Rep: Reputation: 31
what if you wanted to delete all wma files from the sub-sub and possibly sub-sub-sub directories? i tried "rm -r *.wma" from the parent directory and it says "rm: cannot remove *.wma: no such file or directory". there are wma file there though. i did an updatedb and then locate *.wma and many files come up.
 
Old 07-22-2004, 05:34 PM   #7
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Code:
for wma in `find /music -iname "*.wma"`; do rm -i $wma ; done
should do it. Replace /music with the base directory where your sub and sub-sub directories are. I've made it interactive (i.e. it will ask you before it deletes a file). If you're ABSOLUTELY sure you just want rid of them all then remove the -i bit from the rm command.

I am NOT responsible if you delete something you didn't mean to though!
 
Old 07-23-2004, 03:25 AM   #8
dominant
Member
 
Registered: Jan 2004
Posts: 409

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Komakino
Code:
for dir in `ls -F`; do mkdir $dir"new_dir"; done
Run if from the parent folder of dominant1 and dominant2 etc.
Thanks, it worked great!
 
Old 07-23-2004, 04:16 AM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It's a little late, but you can actually do it with a single command without the need for a script:
Code:
mkdir -p /home/dominant{,1,2,3,4}/new_dir
The shell expands that command by repeating the text surrounding the {}, but replacing what's inside the {} with each comma-separated value. Just as an example, it would expand to a single command like this:
Code:
mkdir -p /home/dominant/new_dir /home/dominant1/new_dir /home/dominant2/new_dir /home/dominant3/new_dir /home/dominant4/new_dir
You can even nest them. A command like this:
Code:
mkdir -p /home/dominant{,1,2,3,4}/new_dir{A,B}
would create two sub-directories (new_dirA and new_dirB) inside each of dominant, dominant1, etc.

It's just a neat typing shortcut that I find useful at times (like making a backup: cp some_long_filename.txt{,.bak})
 
Old 07-23-2004, 04:19 AM   #10
dominant
Member
 
Registered: Jan 2004
Posts: 409

Original Poster
Rep: Reputation: 30
Yes but loop is much more automated!
 
Old 07-23-2004, 04:29 AM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Not quite sure what you mean by "more automated"...

Both methods are a single command, and in fact, using the {} technique (in this particular case) saves you about 7 keystrokes
 
Old 07-23-2004, 07:02 AM   #12
dominant
Member
 
Registered: Jan 2004
Posts: 409

Original Poster
Rep: Reputation: 30
yes, but the loop (looks for all dirs in a parent dir).
In case there are 50-60 dirs !
 
Old 07-23-2004, 11:19 AM   #13
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Point taken, but you can use wildcards to a limited extent with the {}. You can use wildcards just so long as the they aren't adjacent to the opening curly brace.

While it doesn't use wildcards, you could handle the 50-60 dirs with a command like this:
Code:
mkdir -p /home/dominant{,1,2,3,4,5}{0,1,2,3,4,5,6,7,8,9}/new_dir
You would end up with a /home/dominant0 through /home/dominant59.

Anyway... different tools are better suited for different situations. I don't usually see people use the {} expansion, and since it easily applied to the original post, I thought I would mention it.

Last edited by Dark_Helmet; 07-23-2004 at 05:37 PM.
 
Old 07-23-2004, 05:16 PM   #14
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Quote:
Originally posted by dominant
Thanks, it worked great!
No problem.
That's actually about the limit of my bash scripting (though I can do more in Perl) but it's amazing how useful it is to know even that much when you have a lot of files to change in some way.
 
  


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
gtkpod - Problem creating iPod directories rose_bud4201 Linux - Software 0 09-20-2005 10:06 PM
Uploading/creating/removing directories on website stevsom Linux - General 4 05-12-2005 03:19 AM
Creating user directories in Apache root robojerk Linux - Networking 1 09-21-2004 12:34 PM
Creating home directories with samba and PAM mkhomedir ferrantepunto Linux - Software 2 01-21-2004 09:13 AM
got stuck while creating directories chupacabra Linux From Scratch 3 11-21-2002 07:58 PM

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

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

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