LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   creating sub-sub directories (https://www.linuxquestions.org/questions/linux-newbie-8/creating-sub-sub-directories-208360/)

dominant 07-22-2004 12:48 PM

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.

fisheromen1031 07-22-2004 01:11 PM

you might try:
mkdir /home/*/new_dir

dominant 07-22-2004 01:30 PM

it doesn't work

linux_ub 07-22-2004 01:43 PM

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

Komakino 07-22-2004 02:02 PM

Code:

for dir in `ls -F`; do mkdir $dir"new_dir"; done
Run if from the parent folder of dominant1 and dominant2 etc.

buffed317 07-22-2004 04:31 PM

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.

Komakino 07-22-2004 05:34 PM

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!

dominant 07-23-2004 03:25 AM

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!

Dark_Helmet 07-23-2004 04:16 AM

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})

dominant 07-23-2004 04:19 AM

Yes but loop is much more automated!

Dark_Helmet 07-23-2004 04:29 AM

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 ;)

dominant 07-23-2004 07:02 AM

yes, but the loop (looks for all dirs in a parent dir).
In case there are 50-60 dirs !

Dark_Helmet 07-23-2004 11:19 AM

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.

Komakino 07-23-2004 05:16 PM

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.


All times are GMT -5. The time now is 06:46 AM.