My folder tree

.
|-- 1
|-- 2
|-- 3
...
|-- 777

I would like to create the folder pictures to each folder.

I run unsuccesfully

mkdir */pictures

One way is of course creating 777 mkdir -commands with Vim's regex. However, I would know how you can do that in shell.

How can you mkdir */pictures?

link|improve this question

Move to SuperUser maybe... – Isaac Waller Jul 30 '09 at 22:24
1  
I don't know, does bash scripting count as programming? – Adam Batkin Jul 30 '09 at 22:35
feedback

migrated from stackoverflow.com Jul 30 '09 at 22:35

This question came from our site for professional and enthusiast programmers.

8 Answers

up vote 7 down vote accepted
for x in `seq 1 777`
do
    mkdir $x/pictures;
done

Also, in the past you have mentioned zsh and this should work without modification under at least bash and zsh.

link|improve this answer
feedback

Almost the same as others, but adding a slash, so it only counts directories and not regular files

for x in */
do
    mkdir $x/pictures;
done
link|improve this answer
feedback

Many ways to do this, I think this is the simplest:

for f in *; do mkdir $f/pictures; done

This is quick and dirty and will make a subdirectory for everything in your current directory. You will get harmless errors if there are files in the current working directory. If that bothers you a more complex solution using find or seq or the like is better. If your directories are numbered then a simpler version of Sean's seq example (in bash) is

mkdir {1..777}/pictures

BTW, mkdir can easily make several directories at once:

mkdir 1/pictures 2/pictures

The problem is that */pictures doesn't expand to anything in the shell since the directories don't exist yet.

link|improve this answer
feedback
for i in *; do cd $i; mkdir pictures; cd ..; done

Edit: I noticed that someone beat me to the punch so I'll improve the script a bit (test whether it is a directory):

for i in *; do if [ -d $i ]; then cd $i; mkdir pictures; cd ..; fi; done
link|improve this answer
1  
No need to change directories. for i in *; do if [ -d $i ]; then mkdir $i/pictures; fi; done – Richard Hoskins Jul 31 '09 at 3:19
1  
for i in *; do if [ -d "$i" ]; then mkdir "$i/pictures"; fi; done <p>Quote the shell vars, because it's the way we do things. – Richard Hoskins Jul 31 '09 at 3:29
feedback

I'm going to assume you're using bash...

find . -mindepth 1 -maxdepth 1 -type d | while read dir; do mkdir $dir/pictures; done

or if everything in the directory is a directory

ls | while read dir; do mkdir $dir/pictures; done

If you're using tcsh, you have to do this with an inelegant for loop.

link|improve this answer
feedback
mkdir ~/Pictures
mkdir ~/container
mkdir ~/container/1
mkdir ~/container/2
mkdir ~/container/3
mkdir ~/container/4
mkdir ~/container/5

cp -r ~/Pictures ~/container/**/
rm -rf ~/Pictures

This will do the trick, i've just tried it

link|improve this answer
Which OS do you use? - I could not get your series of commands to work. - I run your command in Bash and Zsh in OS X Leopard. – Masi Jul 30 '09 at 23:13
Wow really late reply here but i'm on debian linux in normal bash shell – Question Mark May 5 '11 at 15:46
feedback

How about:

find . -mindepth 1 -maxdepth 1 -type d -exec mkdir {}/pictures \;
link|improve this answer
feedback

I tested it out and this should be what you need:

#!/bin/bash 
list=$(/bin/echo *)

for file in $list
do
    if [ -d $file ]; then
    	cd $file
    	mkdir picture
    	cd ..
    	echo "Added folder to $file"
    fi
done
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.