Unix Commands

Home
SOA
Spring-Hibernate
GOF Design Patterns
Java BP
Java Tutorial
Java Traps
Datawarehouse
C++ Tutorial
Unix Commands
Interview QnA
About MDC

This command bulletin is an unorganized list of command examples in Unix. These examples are not explained in detail. If you have some idea as how the command might look like, you can find it here easily using browser search (Ctrl+f). Some of these commands are bash shell specific.

 

  • 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
    • more - View files one page at a time
    • less - View files one page at a time. Ability to move backwards by pressing b.
    • 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: w
  • 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 change the priority of already running command by process id from PS: renice +15 23456
  • To find details about user environment like login id, last login etc:

logname : current login name
whoami : same as logname except if su is used to switch user
finger : user information
last : last login details
printenv : environment details

  • 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 scp myfile from current machine to othermachine using id otherid:

scp myfile otherid@remote.example.com:/home/otherhome/

  • To scp otherfile from other machine with id other id to current machine:

scp otherid@remote.example.com:/home/otherhome/otherfile .

Put 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 display 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 echo quotes: echo 'Single quotes "protect" double quotes' or echo "Well, isn't that \"special\"?"
  • To display command output in echo: echo "You have `ls | wc -l` files in `pwd`"
  • To display variable value in echo: echo "The value of \$x is $x"
  • To sort page and print a file: sort file | pr -3 | lp
  • To redirect standard output to standard error: echo "Usage error: see administrator" 1>&2
  • To send command output to one file and error to another file: find / -print > fileOne 2>fileTwo
  • To remember a directory and use it later:
    • pushd /var/dirme

    ----execute other commands even change directory

    • popd
  • To create a patch file by using diff between files: diff fileOld fileNew > patchfile
  • To apply patch to a file: patch fileOld patchfile
  • To create a patch file between directories: diff -Naur prestinedir modifieddir > patchfile
  • To apply patch to a directory:
    • cd prestinedir
    • patch -p1 < patchfile
  • To put the most recent command suspended using Ctrl+z in background: bg
  • To put a command in background using process number: bg %3
  • To find background job number: jobs
  • To bring a background process to foreground: fg %4
  • To prevent a process being killed when you exit the shell:
    • nohup  lpr -Phod /var/access.log & (command output goes to nohup.out)
  • To split a file based on lines:
    • split -l 100 long.txt piece
    • more piece*
  • To split a file based on size in KB or MB:
    • split -b 10k long piece
    • split -b 10m long piece
  • 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 find user details: finger smith
  • To change name and other finger details of smith: chfn smith
  • To change shell for smith: chsh smith
  • To find all the available shells in the system: chsh -l
  • 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
  • To display current time, number of users logged, how long the system is running and the amount of load into the system: uptime
  • To reset terminal when backspace and delete keys produce junk:
    • stty erase Delete
    • stty intr Ctrl+c
  • To change the group file belongs to: chgrp admingroup imp.tar
  • To log into new group you belong. This will set the group to new group without running chgrp:
    • newgrp admingrp



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/g

  • To subsitute all matches of the microsoft with the linux. ESC :%s;microsoft/;linux;g
  • To edit files found by grep: vi `grep -l ifdef *.java`
  • To edit previous command using vi: fc -e vi