Friday, 18 January 2008

Great UNIXy tips

I've been using Linux and Unix for a long time and I am always trying to make people that aren't Linux users change and go to Linux. One of the things that I like about Linux is the command line and all the tools available to speed things up using just one line (or maybe a few lines.)

Here's an example:

You want to build a complex directory tree of ... I don't know, say 20 directories and subdirectories. Now instead of typing

mkdir first-dir/ ; mkdir first-dir/first-sub-dir ; first-dir/first-sub-dir/first-sub-sub-dir

You can do:

mkdir -p first-dir/first-sub-dir/first-sub-sub-dir

Nice... But how about those twenty directories in a more complex arrangement? That's easy too!

mkdir -p first-dir/{first-sub-dir/{first-sub-sub-dir,second-subs-sub-dir,\
third-sub-sub-dir},second-sub-dir/{another-dir,one-more},\
third-sub-dir/maybe-one-more-dir}/last-dir


Yes, that was one line! (Or multiple lines separated with a \ so that the command continues in a new line.)

What does that look like?

ls -R first-dir/
first-dir/:
first-sub-dir second-sub-dir third-sub-dir

first-dir/first-sub-dir:
first-sub-sub-dir second-subs-sub-dir third-sub-sub-dir

first-dir/first-sub-dir/first-sub-sub-dir:
last-dir

first-dir/first-sub-dir/first-sub-sub-dir/last-dir:

first-dir/first-sub-dir/second-subs-sub-dir:
last-dir

first-dir/first-sub-dir/second-subs-sub-dir/last-dir:

first-dir/first-sub-dir/third-sub-sub-dir:
last-dir

first-dir/first-sub-dir/third-sub-sub-dir/last-dir:

first-dir/second-sub-dir:
another-dir one-more

first-dir/second-sub-dir/another-dir:
last-dir

first-dir/second-sub-dir/another-dir/last-dir:

first-dir/second-sub-dir/one-more:
last-dir

first-dir/second-sub-dir/one-more/last-dir:

first-dir/third-sub-dir:
maybe-one-more-dir

first-dir/third-sub-dir/maybe-one-more-dir:
last-dir

first-dir/third-sub-dir/maybe-one-more-dir/last-dir:

So what do you think? Neat huh? Try doing that within your gui!

By the way, I found this on a page at IBM's site a long time ago. http://www.ibm.com/developerworks/aix/library/au-badunixhabits.html

There's tons of them, search the site for "Speaking unix" and Unix tips. There are tons of things that you can find. Some of them you know, some I'm sure you don't.

By the way, one thing of importance... remember not to put any spaces between directories. If you have a directory with a space in it, quote it!

mkdir -p test/{test ing} will not create a test directory with a test and ing subdirectory... ;)

-P

Post a Comment