Skip to main content

Essential Unix Commands

Essential Unix commands, every developer should keep handy. Download pdf version from here

To find hardware details of linux box: 

dmesg |less

To find linux version: 

cat /proc/version

To find cpu info: 

cat /proc/cpuinfo

To unzip gzip file: 

gunzip something.tar.gz

To untar tar file: 

tar xvf something.tar

To unzip and untar in one step: 

tar xvzf something.tar.gz

To install a software from rpm: 

rpm -iv myapp_someversion.rpm

To edit your bash shell environment 

edit ~/.bashrc file

To set the java and ant class path add the following lines in your bashrc file at home directory with appropriate java and ant directories:
export JAVA_HOME=/usr/java/jdk1.5.0_09
export ANT_HOME=/home/oracle/apache-ant-1.7.0
export PATH=$PATH:${JAVA_HOME}/bin:${ANT_HOME}/bin
export CLASSPATH=./:${JAVA_HOME}/lib/tools.jar:${JAVA_HOME}/lib/rt.jar:${ANT_HOME}/lib/ant.jar

To list all the background running oracle process in the system: 

ps -ef|grep "ora_"|grep -v grep

To give a short name to big command: 

alias listoraprocess="ps -ef|grep "ora_"|grep -v grep"

To kill all oracle process: 

ps -ef|grep "ora_"|grep -v grep|grep $ORACLE_SID|awk '{print $2}'|xargs kill -9

To list all files in current directory: 

find . -print

To search for string "error" in all files in current directory: 

find . -print|xargs grep -i error

To print time of execution for the bash script:


#!/usr/bin/env bash
curtime=`date +"%s"`
#Your bash script statements here
echo "Time of execution is `expr $(date +'%s') - $curtime` seconds"

To reboot linux in 10 minutes with broadcast message: 

shutdown -h +10 "scheduled maintenance"

To find number of logged users: 

who | wc -l

To check the type of a command: 

type cd , type who

To echo System variables: 

echo $DISPLAY $HOME $MAIL $OLDPWD $PATH $PWD $SHELL $TERM $USER

To print shell's environment variables: 

env or printenv

To set command line editing using vi: 

set -o vi

To execute the last command which was executed begining with p: !p


To execute a command as background job use &: 

ls -al &

To list all pending jobs: 

jobs

To create softlink (shortcut, deleting shoftlink doesn't delete original file): 

ln -s myfile softlink

To create hardlink (deleting hardlink deletes original file):

ln -s myfile softlink

To find where the link is pointing to: 

ls -l linkname

To simply view files use one of these commands:
cat - View files in their entirety
less - View files one page at a time
head - View the first lines of a file
tail - View the last lines of a file
nl - View files with their lines numbered


To continuously tail a log file: 

tail -f mylog.log

To update timestamp of a file ot create a empty file: 

touch filename

To change the default editor set the following environment variables
$EDITOR=emacs
$VISUAL=emacs
$export EDITOR VISUAL

To count number of byte words and lines in a file: 

wc filename

To count only number of lines in a file: 

wc -l filename

To list important attributes of a file like size device access : 

stat filename

To print size of files: 

du file1 file2

To print all filenames begining with myfile: 

find / -type f -name myfile* -print

To print all directories begining from current: 

find . -type d -print

To print lines containg myword in a file myfile: 

grep myword myfile

To print lines which doesn't contain myword in myfile: 

grep -v myword myfile

To display only unique lines in sorted order from a file: 

sort myfile | uniq

To count duplicate lines: 

sort myfile | uniq -c

To display command out and also write into a file simultaneously use tee command: 

who | tee who_output | sort

To print 2nd and 4th word in each line in myfile: 

awk '{print $2, $4}' myfile

To print all lines shorter than 50 characters in myfile: 

awk '{length($0) < 50}' myfile 

To print contents of myfile with all occurance or red converted to blue: 
sed 's/red/blue/g' myfile 

To print contents of myfile skipping lines 2 to 5:
sed '2,3d' myfile 


To substitute all occurance of NAME with Kris and AGE with 31 in myfile: 
m4 -DNAME=Kris -DAGE=31 myfile 

To find size, used space, and free space on all disk partition: 
df -h

To find size, used space, and free space current directory: 

df . 

To compare two directories recursively: 
diff -r dir1 dir2 

To compare two file ignoring case: 
diff -i file1 file2 

To compare tabular sorted file skipping 2nd column : 
comm -2 file1 file2 

To point out the first diffrence in similar files: 
cmp file1 file2 

To print 32 byte checksum, useful to check downloaded files: md5sum file1 

To print name of the terminal device associated with the current shell: 
tty 

To format output display string: 
printf "User %s is %d years old.\n" kris 31 

To enter "y" to a interactive shell program by default: 
yes | my_interactive_command 

To print current year business calendar with day number: 
cal -y -m 

To print current year business calendar with sequential numbers instead of date: 
cat -y -m -j 

To print epoch seconds: 
date - %s 

To print last modified timestamp of a file: 
date -r myfile 

To print Hello every 10 seconds: 
sleep 10 && echo 'Hello'

To check which files are newly added to current directory ever 20 seconds: 
watch -n 20 ls -l 

To view your own process: 
ps -ux 

To view process started by user1: 
ps -U user1 

To view all occurance of a program name prg: 
ps -C prg 

To view all process in screen width with full command line: 
ps -efww 

To view all process in screen width with threads: 
ps -efH 

To find how long the system is running since last boot: 
uptime 

To display current process of all logged users: 


To monitor most active processes with full command line: 
top -c [press q to exit from top] 

To graphical display of system load (similar to task manager in windows) : 
xload 

To display memory usage in megabyte: 
free -mt [free -t for kilobytes] 

To run system intensive process with controlled priority 7: 
nice -7 jobname jobargs > outfile [10 is default]
To change the priority of a process id 1834 previously executed with nice: 
nice -5 -p 1834

To change the priority of a process previously executed by user usr1 with nice: 

nice -5 -u usr1

To find details about user environment like login id, last login etc: 

logname 

To find current login name :
whoami : 

To find details about user, if su is used to switch user
finger 


To find details about last login details
last


To see current environment details
printenv

To find details about your computer: 

uname -a

To print your host name: 

hostname

To print details of computer's default network interfaces: 

ifconfig

To display details of computer's all network interfaces: 

ifconfig -a

To display details of a hostname from internet directory: 

whois hotmail.com

To print network path from your machine to a host: 

traceroute yahoo.com

To copy myfile from current machine to othermachine using id otherid: 

scp myfile otherid@remote.example.com:/home/otherhome/
To copy otherfile from other machine with id other id to current machine: 
scp otherid@remote.example.com:/home/otherhome/otherfile .
To download a file from internet: 
wget http://www.yahoo.com/index.htm

To display useful prompt for oracle box, add the following in .profile or .bashrc file

PS1="`hostname`*\${ORACLE_SID}-\${PWD}

To display memory segments: 

ipcs

To display all cpu status: 

mpstat -P ALL

To display cpu utilization every 5 seconds, 5 times: 

sar -u 5 5

To run sar command non interactively to check later: 

nohup sar -o output.file 12 8 >/dev/null 2>&1 &

To deplay highest CPU utilizing process and user: 

ps -eo pcpu,pid,user,args | sort -k 1 -r | head -5

To display i/o status every 5 seconds 5 times: 

iostat -xtc 5 5

To display virtual memory status every 5 seconds: 

vmstat 5

To find files larger than 10KB: 

find . -size +10240 –print

To create patch :

diff -Naur prestinedir modifieddir > patchfile

To apply patch :
cd prestine directory

patch -p1 < patch file path 

Vi editor tips: 

To delete all occurance of DEBUG. 
ESC :%s/* DEBUG //g 

To delete all occurance of "DEBUG ".

ESC :%s/* DEBUG //g 

To delete everything between <> including <> in line numbers 1 to 8. 
ESC :1,8s/<[^>]*>//g

To substitute all occurences of foo on the current line with bar. 
ESC :.s/foo/bar/g 

To subsitute all matches of the word foo in the file with bar. Ignore case in searches. 

ESC :%s/foo/bar/gi

To subsitute all matches of the microsoft with the linux. 

ESC :%s;microsoft/;linux;g 

To delete all occurance of 

ESC :%s/* DEBUG //g 

To delete everything between [] INCLUDIN brackets :

ESC :%s/\[[^\]]*\]//g

To delete everything between <> including angular brackets in line numbers 1 TO 8 :

ESC : 1,8s/<[^>]*>//g

To Substitute all occurences of foo on the current line with bar :

ESC :.s/foo/bar/g 

To Subsitute all matches of the word foo in the file with bar. Ignore case in searches. 
ESC :%s/foo/bar/gi 

To Subsitute all matches of the word foo in the file with bar. Ignore case in searches.

ESC : %s/foo/bar/gi

To Subsitute all matches of the URL http://www.microsoft.com with the URL http://www.linux.org. 

ESC :%s;http://www.microsoft.com/;http://www.linux.org;g

Some essential super user commands
To add user smith : 

useradd -d /home/smith -s /bin/bash -g users smith

To delete user smith: 

userdel smith

To change user home directory: 

usermod -d /u01/smith smith

To change smith password: 

passwd smith
To change name and other finger details of smith: 

chfn smith

To change shell for smith: 

chsh smith

To switch to super user with directory change to /root: 

su -l or su -

To switch to super user with current shell and current directory: 

su

To print group information of smith: 

groups smith

To add new group: 

groupadd -f newgroup

To delete group: 

groupdel group1

To change group name: 

groupmod -n newgroupname oldgroupname

To set hostname: hostname me.myhost.com