Tuesday, March 16, 2010

Tidbits Part One

While studying for an interview, I found the following little snippets of code that I didn't know, namely brace expansion in bash:
anthony@calcifer:~/Transfer$ mkdir -pv testcode/{one,two,three}
mkdir: created directory `testcode'
mkdir: created directory `testcode/one'
mkdir: created directory `testcode/two'
mkdir: created directory `testcode/three'
That's a little heavy for most day to day shell activities, but it could be useful in a script. An example would be where you want to operate on a large number of text files and create 'in', 'out' and 'log' directories automatically then copy the text file into the script/filename/in directory:
for i in $(ls -1 *.txt); do mkdir -p script/$i/{in,out,log}; cp $i script/$i/in; done
Brace expansion is explained in a little more detail at Eric Bergen's blog.

No comments:

Post a Comment