Friday, January 13, 2006

unix tip: ps - tree hierarchy view

linux ps command provides a nice process-tree hierarchy view when invoked with the -f or --forest option.
eg:

$ ps auxw --forest | less
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 3 0.0 0.0 0 0 ? SW 2005 0:00 [migration/1]
root 2 0.0 0.0 0 0 ? SW 2005 0:00 [migration/0]
root 1 0.0 0.0 1508 508 ? S 2005 0:38 init [3]
root 4 0.0 0.0 0 0 ? SW 2005 0:00 [keventd]
root 5 0.0 0.0 0 0 ? SWN 2005 0:00 [ksoftirqd/0]
root 6 0.0 0.0 0 0 ? SWN 2005 0:00 [ksoftirqd/1]
root 9 0.0 0.0 0 0 ? SW 2005 0:00 [bdflush]
root 7 0.0 0.0 0 0 ? SW 2005 0:30 [kswapd]
root 8 0.0 0.0 0 0 ? SW 2005 0:32 [kscand]
root 10 0.0 0.0 0 0 ? SW 2005 1:26 [kupdated]
root 11 0.0 0.0 0 0 ? SW 2005 0:00 [mdrecoveryd]
root 21 0.0 0.0 0 0 ? SW 2005 2:29 [kjournald]
root 86 0.0 0.0 0 0 ? SW 2005 0:00 [khubd]
root 3677 0.0 0.0 0 0 ? SW 2005 0:00 [kjournald]
root 3893 0.0 0.0 3972 1556 ? S 2005 0:29 /usr/sbin/sshd
root 25443 0.0 0.0 8744 3148 ? S 2005 0:00 \_ sshd: usercchx [priv]
usercchx 25529 0.0 0.0 8744 3360 ? S 2005 0:02 | \_ sshd: usercchx@pts/1
usercchx 25532 0.0 0.0 1812 728 pts/1 S 2005 0:00 | \_ -ksh
root 4540 0.0 0.0 8756 3184 ? S 2005 0:00 \_ sshd: userdju0 [priv]
userdju0 4648 0.0 0.0 8904 3484 ? S 2005 0:02 | \_ sshd: userdju0@pts/6
userdju0 4660 0.0 0.0 1808 716 pts/6 S 2005 0:00 | \_ -ksh
root 30124 0.0 0.0 8756 3184 ? S 2005 0:00 \_ sshd: userkyo1 [priv]
userkyo1 30321 0.0 0.0 8756 3404 ? S 2005 0:01 | \_ sshd: userkyo1@pts/12
userkyo1 30342 0.0 0.0 1808 700 pts/12 S 2005 0:00 | \_ -ksh
root 28994 0.0 0.0 8756 3160 ? S 2005 0:00 \_ sshd: user3 [priv]
user3 29237 0.0 0.0 8756 3376 ? S 2005 0:00 | \_ sshd: user3@pts/7
user3 29240 0.0 0.0 1804 696 pts/7 S 2005 0:00 | \_ -ksh
root 23316 0.0 0.0 8516 2500 ? S 2005 0:00 \_ sshd: root@pts/20
root 23322 0.0 0.0 4348 1348 pts/20 S 2005 0:00 | \_ -bash
root 13179 0.0 0.0 8676 2572 ? S 2005 0:00 \_ sshd: root@notty
root 7398 0.0 0.0 8748 3184 ? S 2005 0:00 \_ sshd: userdba5 [priv]
userdba5 7446 0.0 0.0 8896 3424 ? S 2005 0:01 | \_ sshd: userdba5@pts/28
userdba5 7454 0.0 0.0 1808 704 pts/28 S 2005 0:00 | \_ -ksh

or just use:
$ ps auxwf | less

but this option not available for other unix ps.

Monday, December 26, 2005

unix tip: find - limit the depth

by default find will search all subdirectories under the parent directory. but you can limit the depth by using maxdepth option.
eg:

- to limit it only to the current directory and not search through any subdirectories, use the -maxdepth 1 option.
$ find . -maxdepth 1 -name test
./test

- to search one level below of the directory use the -maxdepth 2 option.
$ find . -maxdepth 2 -name test
./scripts/test
./test
./tmp/test

- if not using -maxdepth option.
$ find . -name test
./scripts/test
./test
./tmp/test
./.snapshot/hourly.2/test
./.snapshot/hourly.2/tmp/test
./.snapshot/hourly.1/test
./.snapshot/hourly.1/tmp/test
./.snapshot/hourly.0/test
./.snapshot/hourly.0/tmp/test


Wednesday, December 21, 2005

unix tip: one-liner find files with different regex

instead of we type find a few times, we can use -o option.
eg:

$ find . -name "*ksh" -o -name "*txt"
./.mozilla/default/nxdv20h2.slt/cookies.txt
./scripts/sham.ksh
./scripts/java.txt
./scripts/ii.ksh
./scripts/aa.ksh
./scripts/input.txt
./scripts/output.txt
./scripts/test.ksh
./scripts/tiktok.ksh
./scripts/file.txt
./download/firefox-installer/license.txt
./sample.txt

Tuesday, November 29, 2005

unix tip: one-liner find & replace string

normally people use sed to find & replace string.
eg:

$ cat test
abc def xyz 123 456 789
$ sed 's/abc/123/g' test
123 def xyz 123 456 789

but the output is sent to stdout (the screen) not to the file unless you redirect it to new file.
you can use perl to do that.
eg:

$ cat test
abc def xyz 123 456 789
$ perl -pi -e 's|abc|123|g' test
$ cat test
123 def xyz 123 456 789

to backup the original file use -pi.bak option & your original file will be saved as .bak

Wednesday, November 16, 2005

unix tip: ps - full listing of the processes name

to get full listing of the processes name when using ps
for linux, you can use -w option.
eg:

$ ps -auxww

for aix, you can use -l option.
$ ps -elf

but for solaris, the processes name get truncated when you use -l option, so it is sufficient to use ps -ef only.