October 27th, 2009Handy mkdir, rm, ls commands with {}
I always find these commands handy when working with apache.
Part of any VirtualHost config involves making the right directories and you can make your job easier by using curly braces. I suppose you could call it regular expressions. I don’t actually know the technical terms for it. I know it works in bash which is what I use on a daily basis.
Learn how to create directories without having to issue multiple mkdir commands. This is a pretty simple one.
mkdir -p /path/to/some/vhost/docs
This is fairly straight forward. Make the parent directories if they don’t exist. So if /path/to/some/vhost/docs is what you want, and /path or any of it’s children don’t exist, then it will get created. It’s a fairly safe command because it intelligently checks before creating directories.
This example is a bit more complicated but it’s awesomeness will blow you away. I used this when I was making virtualhosts for multiple domains or even sub domains of the same parent.
mkdir -p {sub1,sub2,sub3}.domain.com/{docs,logs,cgi-bin}
or
mkdir -p www.{domain1,domain2,domain3}.com/{docs,logs,cgi-bin}
that command will create all your directories in the appropriate hierarchy so that you don’t need to issue multiple mkdir commands. It will create the parent directories for you as well. Here is what it does.
$ find .
.
./www.domain1.com
./www.domain1.com/cgi-bin
./www.domain1.com/docs
./www.domain1.com/logs
./www.domain2.com
./www.domain2.com/cgi-bin
./www.domain2.com/docs
./www.domain2.com/logs
./www.domain3.com
./www.domain3.com/cgi-bin
./www.domain3.com/docs
./www.domain3.com/logs
You can also do that with ls.
$ ls -d www.domain{1,2}*
www.domain1.com www.domain2.com
Or even with rm
$ rm -vr www.domain{1,3}*
www.domain1.com/cgi-bin
www.domain1.com/docs
www.domain1.com/logs
www.domain1.com
www.domain3.com/cgi-bin
www.domain3.com/docs
www.domain3.com/logs
www.domain3.com
Enjoy!
