Archive for the Category » Linux Commands «

Friday, October 17th, 2008 | Author: Angelo

Command grep is used for printing lines which matches to a pattern you have defined.
Exercise ::
For example there is one file name is test contains following lines.
is it test file ?
is it test file

Now pattern you defined is ? means you are trying to print all lines which contain ? (question mark) from file test.
Usually we try follow command.
cat test | grep ?

But output will show both lines because grep treat ? as single character both lines contains more than single character. Solution for grep lines containing ? (symbol) is define ?(special character) in []
cat test | grep [?]
is it test file ?

Same in case of all special character *, . , ‘ , ” , }, ) . This trick will help you when you need to search all lines containing defined pattern from log files. Also useful in shell scripting.

Wednesday, October 08th, 2008 | Author: Angelo

Hello,

Below is command to find out number of connections from each ip to server using netstat , sort, uniq, awk.

netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

Here is description of Grep used in this command :: Grep is used for sorting lines which are matching for defined pattern but if you want to sort such lines which are matching two or more different pattern then simply define all patterns in single quote and separate them using \| .

Tuesday, October 07th, 2008 | Author: Angelo

Here is example of rsync command which is used to to delete all files from receiver server which are not exist on sender server.

rsync -e 'ssh -p 4000' -av --delete --progress ./path-to-sender-directory ip-of-remote-server:/path-of-receiver-directory/

Below is description of all parameters used in this command. Rsync stands for remote synchronization, -e to specify remote shell here remote shell we have used is ssh (secure shell running on port 4000), 'ssh -p 4000' consider secure shell ssh is configured on port 4000 on remote server, -a (stands for rpogDtH) & -v for verbose if you are using this command in script then you don’t need to define v, –delete to delete files that does not exist on sender.

./path-to-sender-directory path to sender directory, path to receiver directory username@ip-of-remote-server:/path-of-receiver-directory/ . Also you can use –progress it will show you progress during transfer(don’t use this parameter if you are using this command in shell script).

Also you can use –delete-after instead of –delete so files will be deleted from receiver after transfer not before but it would be better to use –delete because –delete does file deletions on the receiving side before transferring files to try to ensure that there is sufficient space on the receiving filesystem.

In addition there is one more option you can use –delete-excluded this will delete excluded files on receiver, this tells rsync to also delete any files on the receiving side that are excluded (In short you can use this parameter instead of –delete & –excluded).

Thursday, October 02nd, 2008 | Author: Angelo

Below is command to find out ports which are in use using netstat & cut.

netstat -na | grep 'tcp\|udp' | awk '{print $4}' | cut -d: -f2 | sort | uniq -c | awk '{print $2}'

Below is description of each commands :: Netstat command is used to check all incoming and outgoing connections on linux server. Using Grep command you can sort lines which are matching pattern you defined. AWk is very important command generally used for scanning pattern and process it. It is powerful tool for shell scripting. Sort is used to sort output and sort -n is for sorting output in numeric order. Uniq -c this help to get uniq output by deleting duplicate lines from it.

Category: exim, Linux Commands  | 19 Comments
Thursday, October 02nd, 2008 | Author: Angelo

Hello,

Below is command to find out number of connections to each port using netstat, cut, awk ,uniq.

netstat -na | grep 'tcp\|udp' | awk '{print $4}' | cut -d: -f2 | sort | uniq -c | awk '{print $2}'

Here, is description for this command so many times you may face trouble because of dos attack in such case you need to find port number (service) on which dos attack is going on above command will help you to search number of connections to each port which is in use.