Unix Commands cheat-sheet 2

This page lists down common unix commands I use in my day to day work.

Disk space Troubleshooting


back to top

How to see free and used space

df -h .

df -h /users/dir

Sort the underlying directories by space usage

du -k | sort -nr | more

du -sh *

du --si
ls -lh

Find 10 largest files in /user/hadoop

du -ak /user/hadoop | sort -nrk 1 | head
ls -lrt | grep ^l

How to find disk usage information for a directory in unix

df -h DIRECTORYNAME
df -h /users/android

df -H

Process Troubleshooting


back to top

top

Ps -ef 

Ps -ef | grep username

Find PID for a process and Kill The Process

ps -ef | grep sonidevName

kill -9 18244

How to see all Environment variables for a process

ps -ef | grep 'process_name'
cat /proc/$PID/environ

cat /proc/112222/environ | tr '\0' '\n'

Showing environment variables for a process

ps -eo cmd e

Find Out Current Working Directory Of a Process

pwdx 11728
ls -l /proc/11728/cwd

Find Out Owner Of a Process

ps aux | grep 11728
cat /proc/3813/environ
ps -eo pid,user,group,args,etime,lstart | grep '11728'

Find service for a given port

grep port /etc/services
grep 2600 /etc/services

lsof -i :8080

Find executable from given process Id

ls -l /proc/3813/exe
galaxy@solara-dev[~]$ ls -l /proc/11728/exe
lrwxrwxrwx 1 galaxy galaxy 0 May 18 12:15 /proc/11728/exe -> /home/galaxy/environment/jre1.8.0_20/bin/java*

Network Troubleshooting


back to top

Display IP address

Ifconfig iface_name

List all open ports / Active internet Connections

lsof -i
lsof -i:8800
ps pid

export PORT=8080
lsof -i :$PORT
lsof -i tcp:$PORT
lsof -i udp:$PORT
lsof -i :80
lsof -i :80 | grep LISTEN

Find PID for given port

Find out the processes PID that opened tcp port 7000, enter:
fuser 7000/tcp
3813

Find service for a given port

grep port /etc/services

grep 2600 /etc/services

Find executable from given process Id

	ls -l /proc/3813/exe

Opened Ports from current machine

	lsof -i | grep ":[0-9]\+->" -o | grep "[0-9]\+" -o | sort | uniq

Admin Commands


back to top

Add new user

sudo adduser Mukesh

-- force user to change password
sudo passwd -e Mukesh

-- Allow login without password
sudo passwd -d Mukesh

-- Provide user sudo access
sudo adduser Mukesh sudo

Find CPU Info

-- displays information about all cores and processors
cat /proc/cpuinfo

Find shared library dependency

ldd ./p4

List all users

cat /etc/passwd

Create New Group

$ sudo groupadd webappadmin
	

$ sudo usermod -a -G webappadmin jobcpe
$ sudo usermod -a -G webappadmin nginx
$ sudo usermod -a -G webappadmin jenkins

Make changes for all the users

/etc/profile.d

http://bash.cyberciti.biz/guide//etc/profile.d

Run commands at regular intervals / frequency

watch command is used to run any designated command at regular intervals.

watch -d 'COMMANDS'

watch -n 5 free -m

Find files containing given pattern

grep -rnw . -e "java.protocol.handler.pkgs"

Version History


  Date Description
2015-08-15    Initial Version  
2016-03-04    Added “Create Branch commands”