Category Archives: linux

Check Disk usage in Linux

Here are a few important commands you would need to check the disk usage on a linux machine

A simple df (disk filesystem) command can help us get important info on linux file system. -h will make the data more human readable.

$ df
$ df -h

More variations of df -h command

Another important command would be du (disk usage) for a particular folder. du -h would give same data in human readable form.

Other useful variation of du command

du -sh
du- sh *
du -Pshx /* 2>/dev/null

How to check if a port is open on a server?

I wanted to check if the service I am trying to access on a server is actually listening on the port I am hitting. I tried to look for a ping variant which could tell me if the port is listening and required service is up and running.

Found nmap command as answer.

nmap -p 80

Starting Nmap 6.40 ( ) at 2016-03-02 16:05 IST
Nmap scan report for (
Host is up (0.050s latency).
rDNS record for
80/tcp open http

It checks the port state and also what service is listening at the port.

Search and replace in linux vi editor


This command will replace all the occurences for oldstring with newstring after confirming for each existence (c in the end tells vim to ask for confirmation).

More on search replace –

Some other useful commands are

/ for search 
n find next while search (after /)
N find previous while searcg (after /)

:$ last line of file
:0 first line of file

Controlling System Access with IPTables

There might be times when you want to control outgoing or incoming traffic from a linux machine. Iptables is answer to that.

To check current settings

sudo iptables -L

To Add a rule

iptables -A OUTPUT -p tcp –dport 8000 -j DROP

Lets get into details

iptables: command

-A: Add the rule

OUTPUT: Type of rule, OUTPUT or INPUT

-p: protocol tcp/ udp

–dport: port number (8000 here)


So Above command tell system to not allow any outgoing traffic on port 8000.

iptables -A OUTPUT -p tcp –dport 1935 -s -j ACCEPT

-s: source

-d: destination

The above rule states to allow outgoing packets on port 1935 to a specific IP.

If we have centos based system

Edit rules

sudo vi /etc/sysconfig/iptables


sudo /etc/init.d/iptables restart

Kill a process in linux

There can be times that a process becomes unresposive and you want to kill it forcefully.

In linux, we will need to first find out the process id

ps command- it gives a list of all processes running.

ps aux- gives all process details with, a = show processes for all users, u = display the process’s user/owner, x = also show processes not attached to a terminal

to fetch only required process- use grep, ps aux| grep ‘name’

Then kill it forcefully- kill -9 PID

Putting it all together.

Lets say I want to kill eclipse

kamalmeet@System:~$ ps aux | grep eclipse

1101 6070 1.3 1.8 46584 5500 ? Sl 12:34 1:38 /usr/bin/java -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx512m -jar /home/kamalmeet/eclipse//plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar

kamalmeet@System:~$ kill -9 6070

Setting File permission in Linux

There are three type of groups who can access a file in linux.

User: who created the file
Group: Group to which user belongs
Others: Other users (not belonging to above 2 categories).

To add/ modify permissions use following format

chmod u=rwx,g=rw,o=rx abc.txt

or a simpler option

chmod 765 abc.txt

Both of the above means same.

How to interpret these numbers


hence Read+Write+Exceite=4+2+1=7 or Read+ execute=4+1=5 and so on.

Cron to run every 30 minutes

How will you set up a job to run every 30 minutes, from midnight till 10 in morning, Monday to Friday?

*/30 0-10 * * 1-5 MyJob

Understanding the syntax

* * * * * *
| | | | | |
| | | | | +– Year (range: 1900-3000)
| | | | +—- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +—— Month of the Year (range: 1-12)
| | +——– Day of the Month (range: 1-31)
| +———- Hour (range: 0-23)
+———— Minute (range: 0-59)

More on Cron Jobs

Accessing localhost from virtual machine or Android Emulator

Two problems- one solution i.e.

Sometime back, I faced an issue that I had installed a virtual machine on mu linux machine. I had a server/ website running which I wanted to access from virtual machine. http://localhost/site simply refused to work. A little googling helped with an answer to use

Again faced similar issue. Was trying to access local website from within android emulator. Again localhost refused to work. And guess who was the rescuer? yep again 🙂