Unix shell commands I use frequently and accumulated over the years ...

Author: Sakthivel Kathirvel
Created: Apr 2006
Last Updated: Jan 2015

[grep]
    grep -rain
    grep -Pain
    grep -inverse
    grep -PairHana

    cat -EAT
    ls -god
    ls -dog */  [dir only]
    ls -A .* | grep '^\.'  [ list only the hidden files]
    ls -got ;
    ls -gothic
    ls -halt
 
    less -JIN
    less -JINSR
 
    zip -rum
    unzip -Call
    unzip -Cap
    find  -follow -type -name -maxdepth  [ "-follow" the "-type" by "-name" for "-maxdepth" ]
    
    tree -t | grep *log* | less -JIS

    top -bd 5 -n 9419
    grep -Pvi samplesize\|apache\|log4\|factory\|debug  logs.1 | less -JIS
    grep no file path ; only file names.
        grep -HanaPair object  . --include=*.java | awk 'sub(/^.*\//, "")' | less -JIS
    grep partial  file path names ; only file names.
    $ grep -HanaPair object  . --include=*.java | awk 'sub(/^.*\/src\/org\/apache\/log4j\/builder\//, "")' | less -JIS


[solaris]
    HISTTIMEFORMAT="%h-%d-%y - %H:%M:%S "
    shopt -s histappend
    PS1="\033[1;34m\]\u@\033[1;0m\]\h:\033[1;46m\$(pwd 2)\033[0m\]\n$ "
    scd(){  pushd . > /dev/null;  builtin cd "$@";}
    alias cd="scd"
    alias dt="/usr/openpkg/bin/find ./ -maxdepth 2 -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
    alias l="less -JIS"
    alias ls="ls -haltF"

[linux]
    HISTTIMEFORMAT="%h-%d-%y - %H:%M:%S "
    shopt -s histappend
    PS1="\033[1;34m\]\u@\033[1;0m\]\h:\033[1;46m\$(pwd 2)\033[0m\]\n$ "
    scd(){  pushd . > /dev/null;  builtin cd "$@";}
    alias cd="scd"
    alias dt="find ./ -maxdepth 2 -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
    alias l="less -JIS"
    alias ls="ls -haltF"
    alias li="less -JIS" ; alias l="ls -haltF" ; alias hi='history | li' ; alias di='dirs -v' ; alias pu='pushd `pwd`'; 
    function _dt() { find ./ -maxdepth "$1" -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' ;}; alias dt=_dt
    function scd(){  pushd . > /dev/null;  builtin cd "$@";} ; alias cd="scd"
    function _g() { grep -Pair "$1"; } ; alias g=_g
    function _fi() { find -iname "$1"; }; alias fi=_fi

[cygwin]
    HISTTIMEFORMAT="%h-%d-%y - %H:%M:%S "
    shopt -s histappendec
    PS1="\033[1;34m\]\u@\033[1;0m\]\h:\033[1;46m\$(pwd 2)\033[0m\]\n$ "
    scd(){  pushd . > /dev/null;  builtin cd "$@";}
    alias cd="scd"
    alias dt="find ./ -maxdepth 2 -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
    alias l="less -JIS"
    alias ls="ls -haltF"
    alias dhs='echo `date` | tee -a c:/history-`date +%F`.txt  ; dirs -v |  tee -a c:/history-`date +%F`.txt'

[bashrc]
    $ less -JIS ~/.bashrc
    # .bashrc

    # Source global definitions
    if [ -f /etc/bashrc ]; then
         . /etc/bashrc
    fi

    # User specific aliases and functions
    alias pu='pushd .'
    alias di='dirs -v  | tee  ~/dirs.txt'
    alias li='less -JIS'
    alias ls='ls -halt'
    HISTTIMEFORMAT="%h-%d-%y - %H:%M:%S "
    shopt -s histappend
    PS1="\033[1;34m\]\u@\033[1;0m\]\h:\033[1;46m\$(pwd 2)\033[0m\]\n$ "
    #alias cd="pushd . > /dev/null; cd $1"
    scd(){
    pushd . > /dev/null;
    builtin cd "$@";
    }
    alias cd="scd"

#more bash shell stuff http://www.hypexr.org/bash_tutorial.php#emacs

[multi file renames]
    for f in *txt ; do mv $f `basename $f txt`txt.backup; done

[p4v] p4commands
    p4 clients -u   
    p4 clients -e * [ nameFilter flag limits output to clients whose name matches the nameFilter pattern  e.g. -e 'svr-dev-rel*']

    p4 clients -u  | grep -Pai dcs | cut -d' ' -f 2 | xargs -t -n 1 p4 client -o | grep -Pai ^Access\|^Client
    [solaris]
    p4 clients -u  | cut -d' ' -f 2 | xargs -t -n 1 p4 client -o | /usr/openpkg/bin/grep -Pai ^Host
    [linux]
    p4 clients -u  | cut -d' ' -f 2 | xargs -t -n 1 p4 client -o | grep -Pai ^Host

    p4 describe -s 204945

[where core files ]
    /app/corefiles/%h.%e.%p

[less]
    less [example search pattern = /58=.{10} ; meaning= pattern starting with 58=and any 10 chars that follow. ]

[date]
    date +%Y-%m-%d-%H-%M-%S
     `date  +%F-%I-%M-%S`.txt
    echo `hostname`@`date +%m-%d-%Y-%H-%M`
       if (profiler) profiler->iTraceStamp("OUT320");
    `date  +%F-%I-%M-%S`.txt
[.bash_history]
    shopt -s histappend ; history -a
    # User specific aliases and functions
    alias pu='pushd .'
    alias di='dirs -v  | tee  ~/dirs.txt'
    alias li='less -JIS'
    alias ls='ls -halt'
    pushd ~
    alias cd="pushd . > /dev/null; cd $1"

[solaris]
    HISTTIMEFORMAT="%h-%d-%y - %H:%M:%S "
    shopt -s histappend
    PS1="\033[1;34m\]\u@\033[1;0m\]\h:\033[1;46m\$(pwd 2)\033[0m\]\n$ "
    scd(){  pushd . > /dev/null;  builtin cd "$@";}
    alias cd="scd"
    alias dt="/usr/openpkg/bin/find ./ -maxdepth 2 -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
    alias l="less -JIS"
    alias ls="ls -haltF"
    [linux]
    HISTTIMEFORMAT="%h-%d-%y - %H:%M:%S "
    shopt -s histappend
    PS1="\033[1;34m\]\u@\033[1;0m\]\h:\033[1;46m\$(pwd 2)\033[0m\]\n$ "
    scd(){  pushd . > /dev/null;  builtin cd "$@";}
    alias cd="scd"
    alias dt="find ./ -maxdepth 2 -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
    alias l="less -JIS"
    alias ls="ls -haltF"

[xserver]
    /usr/X11R6/bin/startxwin.sh
    XWin -multiwindow -clipboard -silent-dup-error -dpi 100 2>&1 1>/dev/null & ; DISPLAY=:0.0 ; export DISPLAY
    XWin  -clipboard -silent-dup-error -dpi 100 -scrollbars -multimonitors  2>&1 1>/dev/null &
    XWin  -clipboard -silent-dup-error -dpi 100 -scrollbars -multimonitors  2>&1 1>/dev/null &
    xhost +
[win 7 ; cygwin ; ]
    528  XWin -multiwindow -clipboard -silent-dup-error -dpi 100 2>&1 1>/dev/null &
    529  DISPLAY=:0.0 ; export DISPLAY
    530  xhost +

[unix process by tree view]
    [solaris]
        ps  -o pid args -H -U 1616
        ps  -TF  -p 
        ptree -a
        /usr/ucb/ps with args ww [ very wide format - show the complete command]
        /usr/ucb/ps -ejH

 [show threads of process - linux]
     [linux]
         pstree -hcpaAlG  | grep -rainPA10 java.* [ shows process tree]
         ps axjf  [ shows nice process tree ]
         pstree -hcpaAl  | grep -rainPA10 gdb/|java.*
         pstree -hcpaAl  | grep -rainPA10 p4v.*
         ps -eLf | grep `id -u` | grep -rain java
         ps -wwf   [ very wide format - show the complete command]
         ps ux

[unix machine info]
    [linux]
        cat /proc/cpuinfo
        cat /proc/meminfo
        dmesg
        lspci
         The newer version of cpuinfo as reflected in this post  seems to have the necessary components to differentiate between multi-core cpus.
         cpu cores  physical id  core id.
         Physical ID assigns a Unique ID to each CPU.
         CPU cores reflects the number of Cores making up the CPU
         core Id: identifies the core represented.

         cat /proc/cpuinfo  | grep physical.*id
         cat /proc/cpuinfo  | grep core.*id

[solaris]

[netstat]
   netstat -pan  [ show [aA]ll  [n]umeric ports w/ [p]rocess id]

[netcat]
    nc -vv -l -p 6080 < "C:\manifest.txtnetcat.bin"

[xterm]
    xterm wrap turnon turnoff
    To turn auto-wrap off:
     to OFF=>
     echo -e "/033[?7l"
    to turn it on:
     to ON=>echo -e "/033[?7h"
    xterm start
    export DISPLAY=10.212.161.80:0.0; xterm -aw -fg black -bg white -fn -adobe-courier-bold-r-normal--14-140-75-75-m-90-iso8859-1 -sl 3000 -title "`echo $HOSTNAME` DEV. BUILD.SERVER  -  " -e /bin/bash &
    [cygwin window title ]
    printf \\033]0\;\%s\\007 "cygwin-left"
    printf \\033]0\;\%s\\007 "cygwin-right"

[diff/sdiff ]
    sdiff -Bibas -w 100 old.file.txt new.file.txt
    diff -Bad -wib -Easy --suppress-common-lines  old.file.txt new.file.txt | less -JIS 

[win-emacs]
    ~/xEmacs/XEmacs-21.4.21/i586-pc-win32/xemacs.exe
    ~/GNu-emacs-21.3/bin/runemacs.exe
    C:/Documents and Settings/skathirvel/Application Data/.emacs
    "C:\Program Files (x86)\Xming\Xming.exe" :0 -clipboard -multiwindow  -dpi 108 -fp c:/windows/fonts

[unix-emacs]
    emacs -fn -adobe-courier-medium-r-normal--18-180-75-75-m-110-iso8859-1 &
    xlsfonts.exe *adobe-courier-medium-r-normal* | less -JIN
     ;; disable color crap
    (setq-default global-font-lock-mode nil)
    set cscope on the PATH
    export DISPLAY=10.212.161.80:0.0;export PATH=/app/skathirvel:$PATH;emacs -fn -adobe-courier-medium-r-normal--18-180-75-75-m-110-iso8859-1 &

[make]
    make -f ./makefile.simple install | awk '/g/+/+|Installing|cp/{ print $0 }'

[cscope]
 >rm -fr  cscope.files cscope.out
 >find `pwd`  /( -iname '*.cpp' -o -iname '*.cc' -o -iname '*.c'  -o -iname '*.h' -o -iname '*.hh' -o -iname '*.hpp' /) -follow  -print > cscope.files
 >/cygdrive/c/users/skathirvel/cscope.exe  -b -q -k
  [in emacs] M-x cscope-set-initial-directory 
  [hotkey combinations]
  [cscope]
           C-c s s         Find symbol.
       ;;      C-c s d         Find global definition.
       ;;      C-c s g         Find global definition (alternate binding).
       ;;      C-c s G         Find global definition without prompting.
       ;;      C-c s c         Find functions calling a function.
       ;;      C-c s C         Find called functions (list functions called
       ;;                      from a function).
       ;;      C-c s t         Find text string.
       ;;      C-c s e         Find egrep pattern.
       ;;      C-c s f         Find a file.
       ;;      C-c s i         Find files #including a file.



find \( -iname  *.a  -o -iname *.o \) | xargs -t -n 1 rm -fr ;
find -type f   | xargs md5sum > md5sum.txt.`date +%Y%m-%d-%H-%M-%S`

[nm] w/ obj(.o) file]
nm -gl -fposix --demangle  some_object_file.o | gawk '{ gsub("/some/directory/path/toafile"  ""); print } '  | less -JIN

[nm w/ archive (.a) file]
nm -gopPl -fposix --demangle  some_static_lib.a | grep -Pair  | less -JIS

ar tv ./some/directory/path/toafile/some_static_lib.a| less -JIS

[g++]
    g++ -print-search-dirs | tr ':' '\n'
    gcc -E file.c
    or
    g++ -E file.cpp
    will do this for you. The -E switch forces the compiler to stop after the preprocessing phase  spitting all it’s got at the moment to standard output.
g++ -E $1 |\
    grep '^# [0-9]* "[^"]*"' |\
    grep -o '"[^"]*"' |\
    grep -o '[^"<>]*' |\
    grep -v '^/usr/' |\
    sort |\
    uniq

[find]
    find . -maxdepth 2 -type d -follow
    find . -maxdepth 4 -type f -follow -iname *.jar
    find  -iname * -printf `pwd`/%h/%f//n
    find  /( -type f -iname  /) -printf `pwd`/%h/%f//n | xargs ls -tog | less -JI
    find  /( -type f -iname *tnt*.in*ing /)  -printf `pwd`/%h/%f//n | xargs ls -tog | less -JI
    /usr/openpkg/bin/find  /( -type f -iname *apache*log4j*in*ing.gz  /) -printf `pwd`/%h/%f//n | xargs ls -tog | less -JI
    /usr/openpkg/bin/find  /( -type f -iname *apache*apacheout*ing.gz  /) -printf `pwd`/%h/%f//n | xargs ls -tog | less -JI
    find -perm 644 -iname *.cc  -type f -printf `pwd`/%h/%f//n
    find `pwd` -size +40k -exec wc -l "{}" /; | sort -g -r | awk '{ print "| " $2 " | " $1 " |" }'
    /usr/openpkg/bin/find -iname *apache*.log -follow | xargs ls -goth
    find /app/skathirvel/2006 -iname *.yml -type f -exec ls -halt "{}" +;  < find and sort by time >
    find /app/skathirvel/2006 -iname *.yml -type f -exec ls -rhalt "{}" +;  < find and reverse sort by time >
    find gateway -type f | egrep '\.(c|cc|cpp|h|hh|H|C)$' | grep log4j
    find gateway -type f | egrep '\.(c|cc|cpp|h|hh|H|C)$' | grep log4j


[zgrep]
    /usr/openpkg/bin/zgrep -ain
    grep -Pain 35=d  /some/directory/path/toafile/app.log | tr '/01' '|' | less -JI
    sed -n '1661928 1662928p' /some/directory/path/toafile/app.log
    p4 describe -s 2516943 | sed 's/\(.*\)\(google.com\)\(...\)\(.*\)/\2\3/'

[tree]
    ls -goth | grep ^- [ file only]  sorted by time
    ls -godh */  | grep ^d [dir only]
    ls -goSh [ sorted by size]
    ls -goth | grep ^- [ file only]  sorted by time
    [reverse]
    ls -gothr | grep ^- [ file only]  sorted by time
    ls -godhr */  | grep ^d [dir only]
    ls -goShr [ sorted by size]
    ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^//]*///--/g' -e 's/^/ /' -e 's/-/|/' [ tree display]
    find /skathirvel -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'  [ tree display ]

[windows search]
    from:sakthivel to:foo date:>12/01/08 date:<12/30/2008 msft
    from:sakthivel to:foo date:>01/01/09 date:<02/01/2009 msft
    date:>01/01/09 date:<02/01/2009 subject:(gateway rm) folder:inbox
    from:foo date:11/2009..12/2009
    from:foo date:11/2009..12/2009
    from:skathirvel date:11/2009..12/2009
    ext:doc MSFT 128
    ext:pdf msft 128

[eclipse]
    [java] C:\eclipse-standard-kepler-R-win32-installed-sep17-2013\eclipse\eclipse.exe  -vm "C:\Program Files (x86)\Java\jre7\bin\java.exe"  -data C:/SHAK_JAVA_ECLIPSE_Solution-Echo &
    [java] C:\eclipse-jee-kepler-SR2-win32-x86_64\eclipse\eclipse.exe -vm c:\Java\jdk1.7.0_67\bin\java.exe -data C:/SHAK_JAVA_ECLIPSE_Solution-Echo &
    [deprecated] C:/eclipse-3.7-java-indigo-win32-installed-jul-12-2011/eclipse/eclipse.exe  -vm C:/jdk1.6.0_26/bin/java.exe  -data C:/SHAK_JAVA_ECLIPSE_Solution-Echo &
    [deprecated] c:/eclipse-java-helios-SR2-win32-installed-May312011/eclipse/eclipse.exe  -vm C:/jdk1.6.0_14/bin/java.exe  -data C:/SHAK_JAVA_ECLIPSE_Solution-Echo &
    [deprecated] C:/eclipse-SDK-3.5.2-win32-mar-2010/eclipse/eclipse.exe -vm C:/jdk1.6.0_14/bin/java.exe  -data C:/SHAK_JAVA_ECLIPSE_Solution-Echo &
    [deprecated] C:/eclipse-java-galileo-SR1-win32-2009-dec/eclipse/eclipse.exe -vm C:/jdk1.6.0_14/bin/java.exe  -data C:/SHAK_JAVA_ECLIPSE_Solution-Echo &
    [deprecated]C:/eclipse-jee-ganymede-SR1-win32/eclipse/eclipse.exe -vm c:/jdk1.5.0_15/bin/java.exe -data C:/SHAK_JAVA_ECLIPSE_Solution-Echo &
    [cpp]
    C:\eclipse-cpp-kepler-R-win32--installed-sep17-2013\eclipse\eclipse.exe  -vm "C:\Program Files (x86)\Java\jre7\bin\java.exe" -data C:/SHAK_CPP_ECLIPSE_Solution-Echo &
    [deprecated] C:/eclipse-cpp-kepler-R-win32-installed-jul-08-2013/eclipse/eclipse.exe -vm C:/jdk1.7.0_25/bin/java.exe  -data  C:/SHAK_CPP_ECLIPSE_Solution-Echo &
    [deprecated ] C:/eclipse-3.7-cpp-indigo-incubation-win32-installed-jul-12-2011/eclipse/eclipse.exe -vm C:/jdk1.6.0_26/bin/java.exe  -data  C:/SHAK_CPP_ECLIPSE_Solution-Echo &
    [deprecated ] C:/eclipse-cpp-helios-SR2-win32-installed-May312011/eclipse/eclipse.exe -vm C:/jdk1.6.0_14/bin/java.exe  -data C:/SHAK_CPP_ECLIPSE_Solution-Echo &
    [deprecated ] C:/eclipse-cpp-galileo-SR1-win32-2009-dec/eclipse/eclipse.exe -vm C:/jdk1.6.0_14/bin/java.exe  -data C:/SHAK_CPP_ECLIPSE_Solution-Echo &
    [deprecated] C:/eclipse-cpp-galileo-win32.zip.dir/eclipse/eclipse.exe -vm C:/jdk1.6.0_14/bin/java.exe  -data C:/SHAK_CPP_ECLIPSE_Solution-Echo &


[zip_unzip]
    zip -mu -rv9 2009.mar.10.zip * -i  *20090310*  [ for persist file archive]
    zip -mu -rv9 jan.29.09.zip * -i  *01*29* [ gateway log files; create a zip file and delete files in the dir]
    zip -d  oct.10.17.08.zip *10*21*  [ delete files inside the zip file]
    unzip -Cal  oct.10.17.08.zip  /*10/*17/* [ list files inside a zip file]
    unzip -Cal  oct.10.17.08.zip  /*10/*17/*.err [ list files inside a zip file]
    unzip -Cap  oct.10.17.08.zip  runtime.err
    unzip -qq -d apache-log4j  "c:\lib\runtime.jar" [ extract .jar to a directory  very quietly]

[tar]
    tar cvf ~/apache-log4j.tar /app/ --exclude  "/var/log" --exclude "/samples"

[grep]
    grep -rain 201=1.*202=30.*541=30.*occ=tgt app.log
    grep -rainPA1 class.*Interaface * --include=*.{c cc cpp hh h}
    grep -rainPC2 shakdebug  *  --include=*.{c cc cpp h hh} | less -JINS
    $  pstree -hcpaAl  | grep -rainPA10 /(gdb/|java.*/)
    173:  | | |-grep 13345 -rainPA10 (gdb|java.*)
    174-  | | `-pstree 13344 -hcpaAl


[awk]
    $ awk -F'/t' '{ print $29  }'  sample.txt  [ parse the tab delimited file and print the fields]

[du]
    $ du -m --max-depth=1 ~/ | sort -n -k 1
    $ du -h --max-depth=1 `pwd`

[locate]
$ /cygdrive/c/2006/locate32_x64-3.1.11.7100/locate configdb | grep apache  | grep java
/cygdrive/c/2006/locate32_x64-3.1.11.7100/locate -lDa30 history | li

skathirvel@achdv-skathirev /cygdrive/e/2006
$ updatedb --output=2006.locate.db --localpaths='/cygdrive/e/2006'
skathirvel@achdv-skathirev /cygdrive/e/2007
$ updatedb --output=2007.locate.db --localpaths='e:/2007/'
updatedb --output=/cygdrive/c/2007/2007.locate.db --localpaths='/cygdrive/c/2007 &

$ locate -i --database=/cygdrive/e/2007/2007.locate.db */.pst


No comments: