Friday, August 23, 2013

Looking for a command? Use apropos command

If you are on a UNIX terminal and are looking for a command for a task but not sure if there exists one or may be you are looking for an alternative command to the one you know, one way is to google and look for it.

But, sometimes google may be blocked or is not accessible especially when you are on intranet which blocks traffic to the internet. In this situation you may use apropos command, which is available on most UNIX based OS.

For example, if you want to find the commands that deal with "checksum" in one way or the other, you can run the command below. apropos command will give you the list of commands that may be useful to you.

The best part of this command is that you may learn few more commands that you didn't know existed in UNIX.
# apropos checksum

cksum                (1)  - checksum and count the bytes in a file
cksum                (1p)  - write file checksums and sizes
innochecksum         (1)  - offline InnoDB file checksum utility
shasum               (1)  - Print or Check SHA Checksums
sum                  (1)  - checksum and count the blocks in a file
You can test the command here: http://www.compileonline.com/execute_ksh_online.php

Monday, August 9, 2010

Time in ps command

Today I learned something that I always thought was right when it was actually wrong.
It is the TIME in ps command.

$ ps ux
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
user1      991  0.0  2.4 19282 9076 ?        S    15:05   0:00 kdeinit: kded    
user1      780  0.1  1.0  5890 3044 ?        S    16:39   0:01 artsd -F 10 -S 40


The TIME in above output does not mean the total time since the process actually started. But it actually means the total time the process was processing. In other words, it is the sum of all durations the process was actually on the processor(s).

It was a true serendipity indeed!

Monday, March 1, 2010

Work like a UNIX Shell Scripting pro - Use AWK and Sed

Unix may seem little daunting at the beginning when you start writing a shell script to automate a task. But there are two UNIX tools that can save you from some real headache.

1. AWK
2. Sed

You don't have to learn them completely to start using them. But in case you are interested in learning more about them just follow the above links. There are couple of commands that you can start using right away to write good shell scripts. You will have to remember one rule in UNIX that you can pass the output of one command to another command by using a pipe "|"

Below are some of the AWK and Sed which I use quite often to accomplish various tasks:

Get a particular field from a line of text, one line at a time, till you reach the EOF:

ps -ef | awk '{print $2}'



Monday, January 4, 2010

Execute multiple remote commands using SSH

Suppose you want to execute multiple commands on a remote server without going to that server and actually typing the commands there.

For instance you can get the pid of a process and then kill it using ssh as under:

ssh admin@remote << HEREDOC
KILLPID=`ps -ef | grep firefox | awk '{print $2}'`
kill -9 $KILLPID
HEREDOC

Explaination:

First of all, you need to know what a heredoc is. Using a heredoc also takes away the complexity associated with escaping special characters in a command (ticks and quotes in our example).

KILLPID=`ps -ef | grep firefox | awk '{print $2}'`

will define a shell variable KILLPID and assign PID of firefox process to it.

kill -9 $KILLPID

ofcourse we could have accomplished this using xargs but above example demonstrates how to remotely fire multiple commands in single SSH session.

In case you are curious how xargs would have served the purpose:

ssh admin@remote << HEREDOC
KILLPID=`ps -ef | grep firefox | awk '{print $2}' | xargs kill -9`
HEREDOC

Once we have the PID in a variable, we can kill it using kill -9 command

Happy SSHing.

Wednesday, June 11, 2008

Delete a line that has a particular string

Suppose I want to delete all the lines that have rakesh in them. Sed can come handy.

sed '/.*rakesh.*/d' old_file > new_file


Simple isn't it?

Friday, February 8, 2008

Trip or chop a particular field from input

In this post you will learn now you can trim or chop-off a particular field (column) from the input text

To delete the 19th field from a 20 field text delimited by a pipe

awk '$18 = $19 FS $20 {
NF = 18
}
1' FS="|" OFS="|"


Assign the value $19 FS $20 to field 18,
so filed 18 becomes field 19 | field 20.
Then (this works with some awks like GNU Awk and New Awk)
make the record 18 fields (NF = 18, number of fields is 18).


Created and explained by radoulov

Monday, January 14, 2008

Grepping the history = save time and energy

It happens to me almost everyday that I type a long command (combination of commands) and then after executing million other commands I need the long old command back again. What I used to do is hit up and down arrow keys on my key board and keep staring on the screen so that I don't miss the long command. This process is usually very time consuming and frustrating. Today all of a sudden I thought of grepping the history command and aahaa!!!!


simply grep the history command as under:

# history | grep perl
235 perl -e "s/rakesh/root/g;" -pi $(find . -type f)
267 perl -e "s/rakesh/root/g;" -pi $(find . -type f)


then you can just type !267 to execute the command or just copy/paste it.

try it and you will save a lot of time for sure.

Thanks

--Rakesh