Archive

Archive for the ‘Linux/Unix’ Category

Creating a folder with todays date in Linux

June 3, 2012 Comments off
Categories: Linux/Unix

Command for creating the Virtual IP-Address in Linux

July 4, 2011 Comments off
Categories: General, Linux/Unix

Find command in Linux with examples

March 21, 2011 Comments off

10 usage of find command in UNIX
find is very versatile command in UNIX and I used it a lot in my day to day work. I believe having knowledge of find command in UNIX and understanding of its different usage will increase your productivity a lot in UNIX. If your works involve lots of searching stuff or if you are a java or C++ programmer and your code resides in UNIX, find can greatly help you to look for any word inside your source file in the absence of an IDE, find is the alternative way of searching things in UNIX. grep is another command which provides similar functionality like find but in my opinion find is much more powerful than grep in unix.

This article is in continuation of my earlier article Top 10 basic networking Commands in Unix and Top 10 most useful CVS command in Unix .

Here I am listing down some of the way I use find command regularly, I hope this would help some one who is new in UNIX and find command or any developer who has started working on UNIX environment. this list is by no means complete and just some of my favorites , if you have something to share please share via commenting.

1) Running the last executed find command in Unix:
!find
This will repeat the last find command executed. It saves lot of time if you re searching for something and you need to execute same command again and again. In fact “!” can be used with any command to invoke previous run of that command.

2) Finding files which has been modified less than one day in Unix:
find . -mtime -1

This is my favorite while looking out some issue just to check which files have been modified recently which could be likely cause of  issue, believe me it helps a lot and many a times gives you enough hint of any problem due to intended or unintended file change.

3) List all the files and directories in the box which holds the 777 permission in Unix?
find . -perm 777 –print

I use this command to find out all the executable files , you can also modify it to find all the read only files or files having write permission etc by changing permissions e.g. to find all read only files in current directory : find . –perm 555
Here “.” or period denotes current directory. You can replace it with any directory you want.

4) How to do case insensitive search using find command in Unix? Use option “-i” with name, by default find searches are case sensitive.
find . –iname “error” –print

5) How to delete temporary files using find command in Unix?
find . -name *.tmp -print | xargs rm –f

Use of xargs along with find gives you immense power to do whatever you want with each search result. See another example below

6) How to find all text file which contains word Exception using find command in Unix ?
find . –name *.txt –print | xargs grep “Exception”

find . –name *.java –print | xargs grep “MemoryCache”, this will search all java files starting from current directory for word “MemoryCache”.

7) Finding files only in current directory not searching on sub directories:
While using find command I realized that some time I only need to find files and directories that are new , only in the current directory so I modified the find command as follows.

find . -maxdepth 1 -type f -newer first_file

Another way of doing it is below:

find . -type f -cmin 15 -prune

Means type file, last modified 15 minutes ago, only look at the current directory. (No sub-directories)

8) Find all files in current directory and subdirectory, greater than some size using find command in Unix:
find . -size +1000c -exec ls -l {} \;

Always use a c after the number, and specify the size in bytes, otherwise you will get confuse because find -size list files based on size of disk block. to find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means “less than,” and the plus sign means “greater than.” Suppose if you want to find all the files within a range you can use find command as below

find . -size +10000c -size -50000c -print

This example lists all files that are greater than 10,000 bytes, but less than 50,000 bytes:

9) Find files which are some days old and greater than some size in Unix. Very common scenario where you want to delete some large old files to free some space in your machine. You can use combination of “-mtime” and “-size” to achieve this.

find . -mtime +10 -size +50000c -exec ls -l {} \;

This command will find which are more than 10 days old and size greater than 50K.

10) You can use “awk” in combination of find to print a formatted output e.g. next command will find all of the symbolic links in your home directory, and print the files your symbolic links points to:

find . -type l -print | xargs ls -ld | awk ‘{print $10}’

“.” says starts from current directory and include all sub directory
“-type l” says list all links

Hope you find this useful , please share how you are using find commands and we can benefit from each others experience and work more efficiently in UNIX.

Categories: Linux/Unix

UnRar files in ubuntu

February 7, 2011 Comments off
Categories: Linux/Unix

Commonly Used Network Monitoring Commands in Linux

January 31, 2011 Comments off

>

Flexibility and usability of networking commands is one of the major strengths of UNIX like Operating System. Network administrators can quickly identify and fix the problem using these commands very effectively. In this article I mentioned some of the basic commands available in most of the UNIX like operating systems, also I gave small brief about some of the commonly used options for each commands. More details  of each commands is available in Linux man page. I explained few commands here in Part – I and the remaining commands will be explained in the next parts of this article.

# ping

This command is used to find if the end system is reachable from the current system or not. It sends ICMP ECHO_REQUEST packet to the specified system/server/gateway/network element to get the ECHO_RESPONSE packet.
Note: ping uses 28 byte of data for header information.
In Linux it usually send infinite number of packets unless you specify -c option. To quit ping command press ctrl+c key.
Basic syntax for ping command is:
ping

for example      ping google.com

ping 0/ping localhost/ping 127.0.0.1 – checks if the local interface is working
commonly used options for ping are
-i wait n seconds between sending each packet.
ping -i 5 192.168.0.3 will send ping requests every 5 seconds
ping -i 0.1 192.168.0.3 will send 100 ms interval time.
Note: Only super user can send ping packets lesser than 200 ms

-c sends n packets and terminate.
ping -c 3 192.168.0.3

-f flood ping. If  interval is not given, it sets interval to zero and outputs packets as fast as they come back or  one  hundred  times  per second, whichever is more.  Only the super-user may use this option with zero interval.
-a Audible ping. Beep when the remote machine is reachable
Note: This option don’t work in gnome-terminals

-q print only summary. This option will not print ping details of every packet. This option will print only the summary at the end.
-s change the default packet size (56) to n byte.
-w terminate after n seconds. This tells how many seconds it should send the packets. After n seconds ping will terminate automatically. If you use both -c and -w whatever happens first will terminate the ping command.
ctrl + | (control key and pipe) Whenever you ctrl+i while ping is running it will print the summary. It will not terminate the ping, but just prints the summary and continue pinging till you press ctrl+c or -w or -c which comes first.
ping 192.168.0.1 10.0.0.1 62.1.2.89 will send ping request to 62.1.2.89 through 192.168.0.1 and 10.0.0.1. If one hop is not reachable in the middle the total result will be considered as failed ping.
-R record and print the route of the ping
-t specifies the Time to Live. If n is 5 ping will succeed only if the destination host is within 5 hops from the source.

# traceroute

This command prints the details of each host it passes through when it tries to reach the destination host
the basic syntax of the command is
traceroute 62.1.2.89.
-4 or -6 forcing IPv4 or IPv6 respectively.
-I uses ICMP ECHO when sends the packets
-T uses TCP SYN to send packets.
-U uses UDP for traceroute packets.
Note: Traceroute uses UDP Datagrams by default for probes.

-f tells TTL (Time to Live) to start. By default it is 1.
-i This option explicitly tells the interface to use when sending the packets. If this option is not mentioned, interface will be selected based on the routing table.
-m This option tells maximum number of hops to try before reaching the destination. Default value is 30
-N This option tells simultaneous probe packets to send. Default value is 15
-n is used to avoid a request to a name server. It will print just the ipaddress of the hops.
-w specifies the time to wait in seconds for each packet. Default value is 5 seconds
-q sends n packets to each hop. Default value is 3
-r option tells to bypass the routing table. If the destination host is not in the same network traceroute will not succeed.
-s uses an alternative source address.

Note: you must select the address of one of the interfaces

# tracepath

It discovers the MTU(Maximum Transmitting Unit) along with the path. Unlike traceroute even non super users can also run this command.

# host

This command is used for reverse lookup of an ipaddress.
-W wait time to get the response.
-t used to specify the query type. query type can be one of CNAME, NS, SOA, SIG, KEY, AXFR, etc. If query type is not mentioned then host automatically selects an appropriate query type. By default it looks for A records, if the -C option is specified queries look for SOA records.
Note: If name is an ipaddress then this will look for PTR records instead of A records.


-4 or -6 forces IPv4 or IPv6 respectively.
-a will make a query for type ANY
-T forces to use TCP when querying the name server. By default it uses UDP.
-R specifies the number of retries. Default value is 1
-d/-v verbose output

# host -v wiki.linuxquestions.org
Query about wiki.linuxquestions.org for record types A
Trying wiki.linuxquestions.org …
Query done, 1 answer, status: no error
The following answer is not authoritative:
wiki.linuxquestions.org 12857   IN      A       209.152.163.16
Authority information:
linuxquestions.org      12857   IN      NS      ns2.linuxquestions.org
linuxquestions.org      12857   IN      NS      ns1.linuxquestions.net
linuxquestions.org      12857   IN      NS      ns1.linuxquestions.org
Additional information:
ns1.linuxquestions.net  77279   IN      A       64.179.4.147

# nslookup

This command used to get the name servers for the specified server.
It runs in both interactive and non-interactive modes. If the first argument is – or no server name is mentioned it will go to interactive mode.
To get single data use non interactive mode as shown below.
nslookup 204.228.4.243

Use interactive mode if you want to get more than one data from the server.
use exit command to quit from the interactive mode
This command works both windows and linux.

More details : http://www.linuxmanpages.com/man1/nslookup.1.php

# dig

This command dig (domain information groper) is a great tool to get DNS name servers. This is one of the very commonly used tool by network administrators.
This command uses all servers listed in /etc/resolv.conf by default. It can be overridden by specifying them as an argument to dig command. Users can set their own configurations in ~/.digrc file. But command line arguments have more precedence than the .digrc file.
Common syntax
dig @server name type

server – name/ipaddress of the server where dig command to query. Default value will be taken from /etc/resolv.conf
name – is the name to be looked for.
type – query type options are ANY, A, MX, SIG, etc., default is A
-b source ip address, it should be one of the ipaddress of your server’s interfaces.
-f read names from
more details : http://www.linuxmanpages.com/man1/dig.1.php

# telnet

This command allows you to log in from one computer to another computer as you are sitting in that remote computer and working. Once the user enters correct username and password telnet will allow you to use linux shell. You can use all commands whatever you can use from your computer.
Syntax for telent
telnet [port]
By default telnet runs at port 23. So when you run the following command, it will connect to the port 23 from your client.
telnet 192.168.0.8
telnet command can be used to test other ports also. If you want to test the status of HTTP in port 80 just run
telnet 192.168.0.8 80

if the port is not opened you will get the following error.

# telnet 192.168.0.8 80
Trying 192.168.0.8…
telnet: connect to address 192.168.0.8: Connection refused

The same way you test other ports like SMTP, POP3, etc., also.
Note : telnet doesnt not encrypt the data when it sends across the internet.
For more options visit : http://www.linuxmanpages.com/man1/telnet.1.php

# ifconfig

One of the most commonly used command to get the ipaddress of the system
Basic syntax:
ifconfig
-a to list all active and inactive interfaces
ifconfig will print only the details of the specified ethernet

# ifconfig eth0 down will disable the ethernet eth0

# ifconfig eth0 192.168.0.8 [up] will assign ipaddess to eth0

# ifconfig eth0 netmask 255.255.255.0 will set netmask for eth0
# ifconfig eth0 broadcast 192.168.2.255 sets the broadcast address for eth0
# ifconfig eth0 192.168.2.2 netmask 255.255.255.0 broadcast 192.168.2.255 will do all at the same time
# ifconfig eth0 mtu N – changes the (MTU) Maximum Transmision Unit to N
# ifconfig eth0 promisc – sets promiscuous mode. Usually when network card receives the packet it will check if it is for that ethernet. If not it will drop it. In promiscuous mode it will accept all packets.
Note: Only super user can do this
# ifconfig eth0 –promisc – back to normal mode

# route

Prints/Add/Delete the routing table entries.
# route prints the routing table
-n prints 0.0.0.0 instead of * and prints ipaddress for names
route add default gw 10.0.0.2 sets the default gateway as 10.0.0.1
route del default gw 10.0.0.2 deletes the default gateway

# netstat

This prints variuos network related information like network connections, routing tables, interface statistics, masquerade connections, and multicast memberships, etc.
Basic syntax
# netstat
-a lists all ports both listening and non-listening.
-t consider only tcp ports
-u consider udp ports
-l lists all listerning sockets
-p display PID of the program. “PID/Program Name”

-an prints ipaddress, portnumber and userid instead of hostname, port name(using /etc/services) and username
–numeric-ports,  –numeric-hosts and –numeric-users to print only port number, host ip and userid respectively
-c will continuously print the data
-r prints the routing table
-i will print list of all network interfaces
-ie similar output as ifconfig
–protocol= separated list of address family keywords like inet, unix, ipx, ax25, netrom, and ddp. It will be same as –inet, –unix (-x), –ipx, –ax25, –netrom, and –ddp options
-s shows summary of all ports

# wall

This command sends message to all logged in users.
wall
-n prints the messae without banner
More details: http://www.linuxmanpages.com/man1/wall.1.php

# write

This command sends message to the specified user in the specified tty
write [tty]
Sends message to the specified username in the specified tty. If tty is not mentioned the most recently used tty will get the message.
After typing the message press enter to send it. To quit from write press ctrl+D
More details : http://www.linuxmanpages.com/man1/write.1.php

# talk

This command used for chatting with the specified user.
Syntax:
talk [tty]
For more details : http://www.linuxmanpages.com/man1/talk.1.php
Part -2:
In part – II i will discuss about the basic usage of the following commands.
# whois
# arp
# rarp
# mail
# fping
# named
# tcpdump
# iptraf
# iptables/ipchains
# ssh/rsync/nc
# wget/lynx etc.,

Categories: Linux/Unix

Command for Finding a File containing a particular text string

January 2, 2011 Comments off

>grep command form (syntax)

grep “text string to search” directory-path

Examples

For example search for a string called redeem reward in all text files located in /home/tom/*.txt directory, use

$ grep “redeem reward” /home/tom/*.txt

Task: Search all subdirectories recursively

You can search for a text string all files under each directory, recursively with -roption:

$ grep -r “redeem reward” /home/tom

Task: Only print filenames

By default, grep command prints the matching lines You can pass -H option to print the filename for each match.

$ grep -H -r “redeem reward” /home/tom

Categories: Linux/Unix

Installing MySql Query Browser in Ubuntu 9.10

January 9, 2010 Comments off

>Open your terminal and type.

$ sudo apt-get install mysql-admin mysql-query-browser

This command will download and install My-Sql Query Browser to /etc directory.

it will prompt for password.Enter it!

now every thing will be taken care automatically.

For running MySql-Query browser type the following in terminal

$cd /etc
$mysql-query-browser

now query browser will be launched.

—-Enjoy

Categories: Linux/Unix
%d bloggers like this: