Tuesday, January 20, 2009

More Solaris tips

  • Create a directory and move into it at the same time

Question: How often do you create a new directory and then move into that directory in your next command? Answer: Almost always.

I realized this trend in my own work habits, so I created a simple shell function to do the hard work for me.

md () {
mkdir -p $1 && cd $1
}

This is a Bourne shell function named "md" that works for Bourne and Korn shell users. It can be easily adapted for C shell users.

Taking advantage of the -p option of the mkdir command, the function easily creates multi-level subdirectories, and moves you into the lowest level of the directory structure. You can use the command to create one subdirectory like this:

/home/al> md docs
/home/al/docs> _

or you can create an entire directory tree and move right into the new directory like this:

/home/al> md docs/memos/internal/solaris8
/home/al/docs/memos/internal/solaris8>

  • Easily convert man pages to text documents

Have you ever wanted to convert a man page into a plain text document?
I do this occasionally when I want to share information via an email or other document format.

I used to think this was difficult, but then I discovered a simple way to do it. Here's the wrong way to write the man page for the ls command into a text file named ls.bad:

man ls > ls.bad

This keeps all of the formatting characters in your document, which is generally not what you want. Here's a better way that eliminates those formatting characters:

man ls | col -b > man.txt

The col command with the -b option removes the undesirable backspace characters from the text stream, so the only thing left in your document is the text you want, in the format you want.


  • How to page more than one command at a time

Have you ever wanted to group a bunch of commands into a paging program like "page" or "more", but didn't know how?

As a system administrator, I always worry about certain things, like who's doing what, what processes are running, what the network traffic looks like, etc. One day I decided to create a simple alias that would combine all the commands I wanted into one big chunk of information. Then I realized that it wouldn't all fit into one screen.

Fortunately I knew how to group all of the commands together, so the "more" command could handle them as one set of input.

First, here's the wrong way to try to page a sequence of four commands:

date; netstat -i; whodo; ps -ef | more

The only command that gets paged properly here is the "ps -ef" command
--the rest of them scroll off the screen before you can read them.

Here's the correct way to page four commands so they're all controlled
by "more":

(date; netstat -i; whodo; ps -ef) | more

Once you find the commands you want to group together, you can combine them into an alias or shell program. I recommend a shell program for this, because a few "echo" statements sure make it easier to see where one command ends and the next command begins!


  • Changing a file's owner and group at the same time

Did you know that in Solaris 2.x you can change both the user and group ownership on a file at the same time by running /usr/ucb/chown?

For example, suppose you have a file called /export/home/grahamc/myfile that is owned by user 'grahamc' and group 'users'. To change the ownerships, you could type the following as root:

/usr/ucb/chown grahamc:users /export/home/grahamc/myfile

rather than typing:

/usr/bin/chown grahamc /export/home/grahamc/myfile
/usr/bin/chgrp users /export/home/grahamc/myfile

You can also recursively change ownerships with the -R option.

For more information, try: man -s 1b chown

0 comments:

Post a Comment