Holiday Giveaway: Win Learning Nagios 3.0 Book

It’s Holiday Season and I have a gift for you.
As part of the first contest at The Geek Stuff, I will be giving away three copies of Learning Nagios 3.0 PDF version of the book written by Wojciech Kocjan. All you have to do is let us know your favorite Unix command with a sample usage.
For those who’ve been following this blog for a while already know that I love Nagios and I have written few article on Nagios earlier.
For beginners, Learning Nagios 3.0 book will give you an introduction to Nagios and a jumpstart on how to setup and configure Nagios 3.0. For advanced users, this book explains about the Nagios 3.0 new features and details of advanced Nagios configuration.

How to Enter the Contest?

Three winners will be selected randomly from the comments and will receive PDF version of Learning Nagios 3.0 Book. The contest is open to everybody. Contest End Date: Tue, Dec 23, 2008 at Midnight. Update: Contest is closed now. Three random winners was selected from the comments.

This command will list all log files that contain the IP address 192.168.0.1

find /var/log -type f -exec grep -l “192.168.0.1” {} \;

Even more fun if you wrap it in a for loop. This command will output the name of the log file that contains the IP address then the log entries from said file containing the IP address and dump it all into a text file (IP_in_logs.txt) for later review.

find is a marvelous command with far to many options to list, but it’s worth learning in detail.

Here’s one that’s sometimes useful on file servers at work, it finds all directories in home with the word “music” in them.
[root]# find /home -type d -iname “*music*”
Of course if music on work servers is a real no-no, you could always do this, but some may think you’re mean for doing it… it finds all files that end with “.mp3”, and then removes them.
[root]# find /home -type f -iname “*.mp3” -exec rm -f {} \;

Of course the -type f (only find files), -type d (only find directories) and -exec options are only the tip of the iceberg! find is a treasure trove all on it’s own.

Here’s a VI tip I use a lot. I’m no guru, so my tip may be of little help to others.
Sometimes I need to make an edit, but only withing a certain parameter of lines — not in the whole text file. I tend to use this if I’m editing a http.conf or vhost.conf file.This VI command helps:

:32,56s:originalstring:newstring:g

The numbers at the beginning of the string represent the line numbers in the text file where I want to make the changes. So, the changes will only be made between line 32 and 56 and nowhere else. The “s” after the second line stands for “substitute”. The “g” at the end is for global — yet it’s only global within the line parameters which were set at the beginning of the command string.

To edit two files at the same time with vim you can split the screen.
Start by editing the first file: $ vim file1
Press Ctr + w then n (Opens new window)
Read a file into this session by typing :r /path/file2
To switch between the top and bottom press Ctl + w then w
Use standard vim commands like yy to cut p to paste.
To save the file you read in, type :w followed by the filename.
Ctr + w then q will also attempt to quit the window.

Optional:
To turn line numbering on :set nu
To turn text from wrapping off :set nowrap
To turn syntax highlighting on :syntax on

ls
Because I think I use “ls” more than any other command. I know that’s as boring as saying the word “the” is my favorite word in english, but “ls” is only 2 letters, very effective and simple to use with very predictable results …elegance.

The default ‘ps’ command only give you the starting timestamp of the processes which run at the same day when you issued ‘ps’; Older processes can just would shown as starting date but no time. Aliasing the ‘ps’ command with the following argument would give you the starting timestamp. Sometime it is useful to find some dead processes run over a period of time.

alias psStartTime=’ps -eo pid,%cpu,%mem,user,args,lstart’

In vi, I would like to share find and replace, in command mode, try
:1,$s/word1/word2/gc
will try to find from line 1 to the end of file with match ‘word1’ replace to ‘word2’ after your confirmation.
:n1,n2s/word1/word2/gc
do the same as above but between line number ‘n1’ to ‘n2’.

my most favorite command is grep which is the most powerful command,
we can search for whatever pattern in a file, it will give you the lines from a file which is matched for the given pattern.
– you can get the matched line’s line number.
– count of the matched pattern, etc.

My favorite is: Ctrl + R and type the keyword to search the commands that i used previously.

Another favorite “history” is !$ which use the parameter in the last command in the current command.
ex: $vim /very/long/path/to/the/file.txt
$tail -n 15 !$ (!$ will be /very/long/path/to/the/file.txt)

In vim, you can mark the particular location in a file.
If you are navigating through a huge file, you dont have to scroll up an down again again.
you can mark that place using ‘m’ followed by name. (For ex: ma ).
if you press `(backtick) a, the cursor will point that marked location.

VIM tip
global mark – which enables you to come to the marked spot from anywhere.

Explanation.
Do mA in a place inside a file, from where ever you are press (backtick A ) `A,
it will take you to the file, and the position where you marked it.

Unix command

Finding the 5 biggest file in my home directory.
Sometime there will be a dump or a unwanted file occupies the space, and kills me for violating the quota, so i will use this command…. ( change the number in head command to see different number of output ).

With this command, you can substitute a word/etc into a text.
The firsts parameters are the firts line and the last line (in this case from line number 1 up to the last line into file). Second part is the word you would like to substitute. Third part is the new word you would put into file.

Most of the system related announcement/Error/Information are printed on Linux server’s first Virtual console and if you logged in from remote system through putty/some other remote login client, you can still view the same through the below mentioned command:

netstat is my friend on a new setup machine to check the programs that are listening on the interfaces.

netstat -nap|grep LISTEN
-p works only in linux. in debian you can use netstat -n4lp to see what ipv4 progs are listening. be warned, ssh is listening on ipv6 addr.
in freebsd I use sockstat -l ( or -4l )
in freebsd to check out the routing table I use
netstat -nr

🙂
in vi if you want to go at the begining of the line you press ^ at the end $ i you want to go a word after this w and you can also 8w or d8w (deletes 8 words)
basiccly you are moving from word to word with w and b 🙂

My favorite is “top”.
It is used by most to actually see what is going on in the processes area and also to see what memory is being used.
Put it on desktop and keep track of all things going on in real time.

The command I tend to use the most is “tail -f” I suspect most people will know about this already, however for people transitioning from windows to Linux this is a great command to watch log files in real time. To use the command simply type “tail -f logfilename” to end the tail type CRTL-C. You can also use the command to see just the last n number of lines “tail -n logfilename”

My favorite command is … ok there are a lot of them, so I’ll just pick alias.

alias lsl=”ls -aC –color=yes | more”

This shows all directory files including hidden files in color and in columns, one page at a time.
An argument could be made for using less rather than more, because less is more, but I get weird video effects when I do, so I just stick with more.

Stuff your alias’s into bashrc (by whatever specific name) and they will be active when you log in.

II like the nmap Command as this help me scan a Subnet and lets me know what hosts are in this subnet and what ports are open on these hosts. nmap is extremely powerful command as you can scan with different options (example below for more options use Man or –help)

I like using screen to multiplex my terminals. This is also how I share terminals with customers, and nothing more is needed other than SSH and screen itself:

screen -h 30000 -S arbitrary.title -T vt100

This starts screen with a scrollback buffer of 30,000 lines, sets the screen title to “arbitrary.title” so you differentiate between multiple screen sessions, and sets the terminal type to a baseline VT100 terminal. You can then use screen to split the terminal window, so you don’t need to access other tabs or windows to see man pages, source files, etc.

After all the operating systems I have used I have come across one command that simplifies everything from confusion to light. Some might take it lightly but these simply comand will make all linux jargon come to light.

apropos *anything*

this will give you a quick discription of the command or instruction you want to know. sub *anything* with any command eg. man , shell, gcc, shutdown. enjoy

We needed a script that had to remove from a directory all files except for a specific number of file names. This directory was filled by another script that extracted attachments from emails that were received on a dedicated email address. Finally a print script toke the valid attachments and printed them, afterwards removed them. Since it was a public email address a lot of garbage was unavoidable and as a result also a lot of garbage attachments. To remove the garbage attachments I used a “remove except for …” command:

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.

Contact Us

Email Me :
Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You can also simply drop me a line to say hello!.