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. |
you might try:
mkdir /home/*/new_dir |
it doesn't work
|
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 |
Code:
for dir in `ls -F`; do mkdir $dir"new_dir"; done |
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.
|
Code:
for wma in `find /music -iname "*.wma"`; do rm -i $wma ; done I am NOT responsible if you delete something you didn't mean to though! |
Quote:
|
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 Code:
mkdir -p /home/dominant/new_dir /home/dominant1/new_dir /home/dominant2/new_dir /home/dominant3/new_dir /home/dominant4/new_dir Code:
mkdir -p /home/dominant{,1,2,3,4}/new_dir{A,B} It's just a neat typing shortcut that I find useful at times (like making a backup: cp some_long_filename.txt{,.bak}) |
Yes but loop is much more automated!
|
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 ;) |
yes, but the loop (looks for all dirs in a parent dir).
In case there are 50-60 dirs ! |
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 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. |
Quote:
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. |