Table of Contents

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

Bash profile

user profile located at :

~/.bash_profile

edit user profile environment variable in the bash profile:

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

My linux bash profile code

# User specific aliases and functions
alias fx='cd ~/App/firefox;./firefox'
# refresh fx installation
alias fxnew='cd ~/App;rm -r firefox;tar xjvf firefox-91.9.1esr.tar.bz2'

Bash tips

shortcut
ctrl + z suspend
ctrl + a goto cmd line start
ctrl + e goto cmd line end

Full bash terminal shortcuts: http://www.shell-tips.com/2006/10/29/working-quickly-with-some-usefull-bash-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
wifi(){
	#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;
ka(){
for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
  kill $X;
done
}
alias ki='ka itunes'

bash - file operation

#<---------------------------------->
# ls
 
lss(){
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
}
 
lsss(){
ls -trR
}
 
# ls -file tools
# ref: http://www.thegeekstuff.com/2009/07/linux-ls-command-examples/
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-------------------------->
md5(){
openssl md5 "$1"
# tip: quote $1
}
 
# print directory structure
 
graph(){
set `pwd`
set `basename $1`
ls -FR>~/Desktop/$1.txt
}
 
# clean ._mac file
#find . -name ._* -exec rm '{}' 
 
# advanced sys function
 
 
uflat(){
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]
urep(){
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
}
urep2(){
rename 's/$2/$3/' $1
}
 
# rename prefix; usage: upre/upost \[array of file] [pre/post]
upre(){
for i in $1; do mv "$i" $2."$i"; done
}
upost(){
for i in $1; do mv "$i" "$i".$2; done
}
 
# switch ext; usage: uext \[array of file] [old ext] [new ext]
uext(){
for i in $1; do mv "$i" "${i/.$2}".$3; done
}
uext2(){
for i in $1; do mv "$i" "`basename $i .$2`".$3; done
}
uext3(){
rename 's/\.$2/\.$3/' $1
}
 
# rename case switch; usage: ucase \[array of file]
ucaseUnix(){
rename 'y/A-Z/a-z/' $1
}
 
# clean space in name; usage: uclean \[array of file]
ucleanUnix(){
rename 's/ //' $1
}
 
# working (test)
# add date to pre/post; usage: udate \[array of file] [pre/post] [ext]
udate(){
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
		done	
	fi
else
	for i in $1; do mv "$i" `date '+%Y-%m-%d'`."$i";done
fi
}

Get running script directory

#!/bin/bash
echo "The script you are running has basename `basename $0`, dirname `dirname $0`"
echo "The present working directory is `pwd`"

bash - utility

grep

ref:http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/

sed

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/#.*//'

ref: http://www.grymoire.com/Unix/Sed.html#uh-0

awk

awk is a words grabber, like grep for lines grabber

ref: http://www.grymoire.com/Unix/Awk.html

Vi

# 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 : http://ss64.com/osx/

enable root on mac : http://support.apple.com/kb/HT1528

mac specific bash cmd - others

# convert to pdf
alias pdf2="/System/Library/Printers/Libraries/convert"
pdf(){
pdf2 -f "$1" -o "$1.pdf";
}
#<---------------------------------->
# man
function wman() {
   url="man -w ${1} | sed 's#.*\(${1}.\)\([[:digit:]]\).*\$#http://developer.apple.com/documentation/Darwin/Reference/ManPages/man\2/\1\2.html#'"
   open `eval $url`
}
 
xman() {
   url="http://bashcurescancer.com/man/cmd/"
   open $url$1
}

Linux specific binary tool

Other 3rd Party tool