This page contains Bash cmd info in general, and specific binary tool command in Mac and Linux.

Bash profile

user profile located at :


edit user profile environment variable in the bash profile:

  • display:
    echo $PATH
  • display with escape
    echo \$PATH
  • display raw text:
    echo '$PATH'
  • display with variable:
    echo "Path is $PATH"
  • display block of text:
    echo << EOF
    Block of text here
    Text for $PATH
  • display block of raw text:
    echo << "EOF"
    Block of text here
    Text Again but no $PATH
  • session based variable set:
  • permanently change to path in profile file:
    export PATH=/myPath:$PATH

Note: by adding your_Path_To_3rd_Party_Binary to the $PATH variable, you can directly call them in terminal.

Bash tips

  • find path to a command file
    which ls
  • escape by “\”
  • create a txt file
    echo "hello" > text.txt
  • create a empty file or update a file
    touch empty.txt
  • append to a txt file
    echo "\thome" >> /etc/hosts
  • create a symbolic link
    ln -s ~/real_dir ~/Desktop/alias_name
  • make folder accessible to all
    sudo chmod 777 /your_folder_name
  • batch file rename
    for i in *.JPG ; do mv $i ${i/-[0-9.]*.JPG/.JPG} ; done
  • Vi text editor
    # cmd mode
    #  i : enter text edit mode
    #  dd: delete line
    #  x : delete a char
    #  w : write file wq: save & quit
    #  q : quite q!: quit without save
    # edit mode
    # esc: back to cmd mode
    # ':':enter cmd mode
ctrl + z suspend
ctrl + a goto cmd line start
ctrl + e goto cmd line end

Full bash terminal shortcuts:

quick list
man manual, q:quit man; space: next
ls ls -la: long list directory; ls -sh: list size human read
cp cp -R: recursive copy directory
mv move file
rm rm -R: recursive delete directory
mkdir make directory
source source a shell script file
| pipe prev cmd to next cmd
cmd& run cmd background
clear clear screen

bash - computer management: user, admin, network

# check users: w, who, users
# ifconfig
	#os x version
	ifconfig en1 | grep 'inet ' | cut -d ' ' -f 2
	# linux version
	# ifconfig en1 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
# kill
# kill by name
alias kbn=ka;
for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
  kill $X;
alias ki='ka itunes'

bash - file operation

# ls
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
ls -trR
# ls -file tools
# ref:
alias a='ls'
alias aa='ls -AF'
alias aaa='ls -lF'
alias ll='ls -lF'
alias lt='ls -ltF' # by time
alias lt.='ls -ltrF' # by time reverse
alias lsB='ls -S'
alias lsS='ls -Sr'
alias lh='ls -lhF'
alias l='ls'
alias ld='ls -d */'
alias 1='ls -1F'
# find and remove all
# sudo find * -type d -name .svn -exec rm -rf {} \;
#<--------check md5-------------------------->
openssl md5 "$1"
# tip: quote $1
# print directory structure
set `pwd`
set `basename $1`
ls -FR>~/Desktop/$1.txt
# clean ._mac file
#find . -name ._* -exec rm '{}' 
# advanced sys function
find . -not -type d -print0 | xargs -0J % mv -f % .
find . -type d -depth 1 -print0 | xargs -0 rm -rf
# replace text; usage: urep \[array of file] [find] [replace]
for i in $1; do j=`echo $i | sed 's/$2/$3/g'`; mv "$i" "$j"; done
#for i in *.txt; do j=`echo $i | sed 's/2010/2011/g'`; mv "$i" "$j"; done
rename 's/$2/$3/' $1
# rename prefix; usage: upre/upost \[array of file] [pre/post]
for i in $1; do mv "$i" $2."$i"; done
for i in $1; do mv "$i" "$i".$2; done
# switch ext; usage: uext \[array of file] [old ext] [new ext]
for i in $1; do mv "$i" "${i/.$2}".$3; done
for i in $1; do mv "$i" "`basename $i .$2`".$3; done
rename 's/\.$2/\.$3/' $1
# rename case switch; usage: ucase \[array of file]
rename 'y/A-Z/a-z/' $1
# clean space in name; usage: uclean \[array of file]
rename 's/ //' $1
# working (test)
# add date to pre/post; usage: udate \[array of file] [pre/post] [ext]
if [ $2 ] && [ $3 ]; then
	if [ $2 == "e" ] || [ $2 == "b" ] || [ $2 == "p" ] || [ $2 == "post" ]; then
		for i in $1
			do mv "$i" "${i/.$3}".`date '+%Y-%m-%d'`.$3
	for i in $1; do mv "$i" `date '+%Y-%m-%d'`."$i";done

Get running script directory

echo "The script you are running has basename `basename $0`, dirname `dirname $0`"
echo "The present working directory is `pwd`"
  • get file list
    find *.jpg #find all jpg in fold
  • rename find list
    for i in `find *v10*.jpg`; do j=`echo $i | sed 's/v10/v010/g'`; mv "$i" "$j"; done 
    # for each file, generate new name, replace old name with new name

bash - utility

  • get lines contains word A
    grep wordA sample.txt
  • ignore case
    grep -i wordA sample.txt
  • match exact word
    grep -w wordA sample.txt
  • inverse selection, lines without word A
    grep -v wordA sample.txt
  • count
    grep -c wordA sample.txt
  • line number
    grep -n wordA sample.txt
  • list file
    grep -l wordA *.txt
  • Pattern
    • ^wordA: line starts with wordA
    • wordA$: line ends with wordA
    • ^$: blank line
    • [set]: match one from set [0-9][A-Za-z] [abc] [x-z]
    • [:class:]: match one from class [:alnum:] [:alpha:] [:blank:] [:digit:] [:lower:] [:upper:] [:space:]
    • escape: \
    • .: any char
    • \< \>: begin/end of word
    • wordA|wordB: wordA or wordB
    • {n}: how many times appera in line {min,max}
    • ?: at most once
    • *: none to unlimited
    • +: at least once
    • ^: inverse range; begin of line


line editor

sed '/range/s/find/replace/g' <input >output
# same with _
sed '/range/ s_find_replace_g' <input >output
#line range ^start, $end
sed '1,100 s/find/replace/'

remove all comments in bash script

sed '/start/,/stop/ s/#.*//'


awk is a words grabber, like grep for lines grabber


# vi
# --- change current line to lower case
# :s/.*/\L&/
# -- set file ending with DOS or Unix
# :set ff=dos or :set ff=unix
# -- mass converting dos<->unix
# vim +"argdo set ff=<format>" +wqa <files>

Mac specific binary tool

An A-Z Index of the Mac OS X command line :

enable root on mac :

  • osascript
    osascript theScript.scpt
  • open : open cmd opens every thing, like “start” in DOS
  • mount smb share folder (Windows,Linux,Mac share folder by SMB)
    if [ ! -d /Volumes/me ]; then mkdir /Volumes/me; mount_smbfs //username@PCNAME/shareFolderName /Volumes/me; fi
  • Dmg file creation and convertion
    # make an DMG image name $2 from folder called $1
    echo "CMD: hdiutil create -srcdir \"$1\" $2.dmg"
    hdiutil create -srcdir "$1" "$2.dmg"
    # make an ISO image named as $2 from folder called $1
    hdiutil makehybrid -iso -o "$2.iso" "$1"
    # burn DMG file $1
    hdiutil burn $1
    # create a DMG file with size $1 MB named as $2
    hdiutil create -size $1m $2.dmg -fs HFS+ -volname $2
    # convert DMG file of $1 to an ISO image file
    hdiutil convert $1.dmg -format UDTO -o $1.iso
  • other disk related cmds
    # force reject disk
    #drutil tray open
    #drutil eject
    cd /Volumes/"$1";.
    # dd function for disk dump
    # dd back cd
    # dd if=/dev/disk1 of=myDVDcopy.img bs=2048
    # dd if=/dev/disk1 of=dvd.iso
    # dd backup disk
    # dd bs=512 if=/dev/rXX# of=/some_dir/foo.dmg conv=noerror,sync
    # dd bs=512 if=/dev/rdisk2s3 conv=noerror,sync | gzip -9 > foo.dmg.gz
    # file rescue
    # dd bs=512 if=/Volumes/HD1/badfile.mpg of=/Volumes/HD2/newrecoveredfile.txt conv=noerror,sync
    # back and split
    # dd if=/dev/hda# | gzip -9 | split -b 4096m /dev/sda#/somedir/macosx
    # cat
    # cat /dev/sda#/somedir/macosx* | gzip -d | dd of=/dev/hda# 
    # list disk and device
    # disktool -l
    # df -k
    # diskutil list
    # disktool
    eject2 () {
    /usr/sbin/disktool -e $(df | grep $1 | awk '{print $1}' | sed 's/\/dev\///')
    eject2 /Volumes/$1
    # tar
    #create tar
    #tar -czvf MyArchive Source_file 
    #tar --create --gzip --verbose --file=MyArchive Source_file
    #extract tar
    #tar -xzvf MyArchive Source_file 
    #tar --extract --gunzip --verbose --file=MyArchive Source_file
    #tar xjvf filename.tar.bz2
    #tar -pvczf BackUpDirectory.tar.gz /path/to/directory
    #tar --exclude=".*" -pvczf BackUpDirectory.tar.gz /path/to/directory
    #tar -xvzf filename.tar.gz -C /desired/path
    tar -xvzf $1 -C $2
    tar czvf $1.$(date +%Y%m%d-%H%M%S).tgz $1
    exit $?
    #tar -czvf my_backup.$(date +%Y%m%d-%H%M%S).tgz source_file
    # unrar x test.part1.rar
# convert to pdf
alias pdf2="/System/Library/Printers/Libraries/convert"
pdf2 -f "$1" -o "$1.pdf";
# man
function wman() {
   url="man -w ${1} | sed 's#.*\(${1}.\)\([[:digit:]]\).*\$#\2/\1\2.html#'"
   open `eval $url`
xman() {
   open $url$1

Linux specific binary tool

  • convert
    • convert image sequence into gif
      convert -delay 50 -loop 1 render.*.jpg animation.gif
    • convert big image into small tile parts
      convert -crop 64x64 +repage big.tif small_%02d.tif
    • convert several images into one big image horizontally or vertically
      convert +append 1.jpg 2.jpg out.jpg # horizontally
      convert -append 1.jpg 2.jpg out.jpg # vertically
  • zip
    zip a folder into a zip file
    zip -9 -r myFileOrFolderName
  • xwininfo
    get window info
  • sha1sum
    get sha1 sum of a file like md5
  • 7zip
    7z file opener
    # install or
    sudo apt-get install p7zip
    # extract
    p7zip e example.7z
    # more ref:

Other 3rd Party tool

  • lame - convert aiff to mp3
    lame --r3mix -q0 --id3v2-only --tt \"[title]\" --ta \"[artist]\"
    \"123.aiff\" \"123.mp3\"
  • ffmpeg - convert image & video formats (ref)
    // convert image seq into avi
    ffmpeg -f image2 -i seq.%04d.jpg -r 30 -s 640x480 -vcodec mpeg4 test.avi
      // -f: format; -i: input, %04d means 0001 like pattern; -r: framerate; -b:v video bitrate; -fs: file size limite; -s resolution; -vcodec: encode format
    // get info
    ffmpeg -i video.avi
    // img seq to video
    ffmpeg -f image2 -i image.%04d.jpg video.mpg
    // ref:
    ffmpeg -r 8 -i frame.%04d.jpeg -vcodec libx264 -vpre medium -crf 24 -threads 0 -s 640x480 output.mp4
    // video to img seq
    ffmpeg -i video.mpg image.%04d.jpg
    // video to iphone mp4 or psp
    ffmpeg -i source_video.avi -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4
    ffmpeg -i source_video.avi -b 300 -s 320x240 -vcodec xvid -ab 32 -ar 24000 -acodec aac final_video.mp4
    // save sound
    ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3
    // wav to mp3
    ffmpeg -i son_origine.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 son_final.mp3
    // mpg to avi, avi to mpg
    ffmpeg -i video_origine.avi video_finale.mpg
    ffmpeg -i video_origine.mpg video_finale.avi
    // video to gif
    ffmpeg -i video_origine.avi gif_anime.gif
    // add souble
    ffmpeg -i son.wav -i video_origine.avi video_finale.mpg
    // video to flv
    ffmpeg -i video_origine.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv video_finale.flv
    // video to dvd
    ffmpeg -i source_video.avi -target pal-dvd -ps 2000000000 -aspect 16:9 finale_video.mpeg
    // ogm to mpg
    ffmpeg -i film_sortie_cinelerra.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 film.mpg