Skip to content

Instantly share code, notes, and snippets.

@atika
Last active May 31, 2018 22:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atika/4bca2820b21a71858a06 to your computer and use it in GitHub Desktop.
Save atika/4bca2820b21a71858a06 to your computer and use it in GitHub Desktop.
Some useful Shell aliases and functions for using on Mac OS X. Compatible with Bash/Zsh shells.
# Some useful Shell aliases and function for using on Mac OS X
# https://gist.github.com/atika/4bca2820b21a71858a06
# Regular Expressions
ipregx="[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}" # IP Regular Expression
mailregx="[a-zA-Z0-9_-\.]+@[a-zA-Z0-9_-\.]+\.[a-z]{2,3}" # Mail Regular Expression
# ql : Show a "Quick Look" view of files
ql () { /usr/bin/qlmanage -p "$@" >& /dev/null & }
# mcd : Make a directory and enter into
mcd() { mkdir -p $1; cd $1; }
# Add a blank space on the dock
osx_add_dock_space_right() { defaults write com.apple.dock persistent-others -array-add '{tile-data={}; tile-type="spacer-tile";}'; killall Dock; }
osx_add_dock_space_left() { defaults write com.apple.dock persistent-apps -array-add '{tile-data={}; tile-type="spacer-tile";}'; killall Dock; }
# osx_locaterecent : locate recent created files with Spotlight
osx_locaterecent() { mdfind 'kMDItemFSCreationDate >= $time.yesterday'; }
# osx_list_all_apps : list all applications with Spotlight
osx_list_all_apps() { mdfind 'kMDItemContentTypeTree == "com.apple.application"c'; }
# osx_hidden_toogle : Toogle visibility of hidden files
osx_hidden_toogle() { [ $(defaults read com.apple.finder AppleShowAllFiles) -eq 1 ] && {state=FALSE && echo "Hide hidden files."} || {state=TRUE && echo "Show hidden files."}; echo $STATE; defaults write com.apple.finder AppleShowAllFiles -bool $state && killall Finder; }
# osx_desktop_toogle : Toogle Mac OS X Desktop visibility
osx_desktop_toogle() { [ $(defaults read com.apple.finder CreateDesktop) -eq 1 ] && {state=FALSE && echo "Hide desktop."} || {state=TRUE && echo "Show desktop."}; defaults write com.apple.finder CreateDesktop -bool $state && killall Finder;}
# osx_delete_DS_Store_files : Remove recursively all .DS_Store files
osx_delete_DS_Store_files() { ask "Delete all .DS_Store files recursively?" && sudo find . -name .DS_Store -exec rm {} \; ; }
# osx_bundleid_ofapp : Get application bundle identifier
osx_bundleid_ofapp() { osascript -e "id of app \"$1\""; }
# finderComment: show the SpotLight comment of a file
finderComment () { mdls "$1" | grep kMDItemFinderComment ; }
# pdfman : open a PDF generated from a man page
pdfman() { man $1 -t | open -f -a Preview; }
# git_update_all_repo: Update all subdirectories Git Repositories
git_update_all_repo() { ask "Do you want to \\033[31mupdate all subdirectories GIT project\\033[0m?" && find . -maxdepth 1 -type d -exec bash -c '(cd "{}" && cdir="`pwd` : "; [ -d .git ] && echo -ne "\\0033[32m$cdir\\0033[0m" && git pull || echo -e "\\0033[1;30m$cdir Not a git directory\\0033[0m")' ';'; }
# apachelogmonitor: monitor logs for development
# www.npmjs.org/package/fsmonitor
apachelogmonitor() { fsmonitor -s -p -d "/Applications/MAMP/logs" '+*.log' sh -c "clear; tail -q /Applications/MAMP/logs/*.log"; }
# phpdoc : Search PHP Documentation
phpdoc() { local y="$@"; open "http://fr2.php.net/manual-lookup.php?pattern=${y// /+}&lang=fr"; }
# jquerydoc : Search Jquery Documentation
jquerydoc() { local y="$@"; open "http://api.jquery.com/?ns0=1&s=${y// /+}"; }
# ff: Find a file under the current directory
ff () { /usr/bin/find . -name "$@" 2>/dev/null; }
# ffi : Find a file with a pattern in name:
ffi() { /usr/bin/find . -type f -iname '*'"$*"'*' 2>/dev/null; }
# ffs: Find a file whose name starts with a given string
ffs () { /usr/bin/find . -name "$@"'*' 2>/dev/null; }
# ffe: Find a file whose name ends with a given string
ffe () { /usr/bin/find . -name '*'"$@" 2>/dev/null; }
# findPid: Find out the pid of a specified process and return a number
findPid () { sudo /usr/sbin/lsof -t -c "$@"; }
# findPidl: Find out the pid of a matched process and display a list
findPidl () { local psearch="$@"; while read fpid; do echo -ne " - "$(basename `ps -p $fpid -o comm=`); echo -e " : $fpid"; done < <(ps aux | grep -e ".*$psearch.*" | grep -v 'grep' | awk '{print $2}'; ); }
# debug_http: Download a web page and show info on what took time
debug_http () { /usr/bin/curl $@ -o /dev/null -w "dns: %{time_namelookup} connect: %{time_connect} pretransfer: %{time_pretransfer} starttransfer: %{time_starttransfer} total: %{time_total}\n"; }
# http_headers: Get just the HTTP headers from a web page (and its redirects)
http_headers () { /usr/bin/curl -I -L $@; }
# checkisup: Check until host is Up
checkisup () { while (true) do if ping -t 2 -c 1 $1 &>/dev/null; then echo -e " $(date +%T) \\033[1;36m Host $1 is up! \\033[0m\n $(ping -c 1 $1 | head -n2 | tail -n1)"; return 0; else echo -e " $(date +%T) \\033[31m Host $1 is down...\\033[0m"; sleep 1; fi; done; }
# showTimes: Show the modification, metadata-change, and access times of a file
showTimes () { stat -f "%N: %m %c %a" "$@"; }
# history_tops: Show Top 10 used command
history_top() { history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head; }
# rm: Remove prompt to delete files
alias rm="rm -i"
# myip: Retrieve external IP adress
alias myip="dig +short myip.opendns.com @resolver1.opendns.com"
# geoip: Retrieve IP localization (need MaxMind GeoLiteCity)
alias geoip="geoiplookup -f /usr/local/share/GeoIP/GeoLiteCity.dat"
# mem_hogs_top: Find memory hogs with top
alias mem_hogs_top='top -l 1 -o rsize -n 10'
# mem_hogs_ps: Find memory hogs with ps
alias mem_hogs_ps='ps wwaxm -o pid,stat,vsize,rss,time,command | head -10'
# cpu_hogs: Find CPU hogs
alias cpu_hogs='ps wwaxr -o pid,stat,%cpu,time,command | head -10'
# topforever: Continual 'top' listing (every 10 seconds) showing top 15 CPU consumers
alias topforever='top -l 0 -s 10 -o cpu -n 15'
# diskwho: Show processes reading/writing to disk
alias diskwho='sudo iotop'
# pdfconcat : Concat PDF files
alias pdfconcat="/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py"
# osx_netstat : Netstat on OSX
alias osx_netstat="sudo lsof -i -P"
# gitweb : Launch integrated GIT web interface
alias gitweb="git instaweb --httpd=webrick"
# browse_bonjour: Browse services advertised via Bonjour
alias browse_bonjour='dns-sd -B'
# pkt_trace: For use in the following aliases
alias pkt_trace='sudo tcpdump -i en0 '
# smtp_trace: Show all SMTP packets
alias smtp_trace='pkt_trace port smtp'
# http_trace: Show all HTTP packets
alias http_trace='pkt_trace port 80'
# tcp_trace: Show all TCP packets
alias tcp_trace='pkt_trace tcp'
# udp_trace: Show all UDP packets
alias udp_trace='pkt_trace udp'
# ip_trace: Show all IP packets
alias ip_trace='pkt_trace ip'
# aspire_web : Download entire website
alias aspire_web='wget -r -k -np'
# json : JSON Pretty print
alias json="python -mjson.tool"
# extract : Extract an archive
function extract()
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
# zipf: to create a ZIP archive of a file or folder excluding dot files
zipf () {
if [[ $# -eq 0 ]]; then echo "No files to ZIP."; return; fi
if [[ $# -eq 1 ]]; then
zipname=$(echo $1 | perl -pi -E "s/(\ |\/)+/-/g" | perl -pi -E "s/-$//g");
if [[ -f "$zipname"".zip" ]]; then
ask "Filename \\033[1;31m$zipname.zip already exists\\0033[0;39m, add elements to it ?" N
[ "$rep" != "y" ] && zipname="$zipname""_"$(date +"%s")
fi
else
zipname="Untitled_$(date +"%d_%h_%Hh%M")"
fi
echo -e "Compressing files to archive \\033[1;32m$zipname.zip\\0033[0;39m (skip hidden dot files)."
zip -r "$zipname".zip "$@" -x "*/\.*" ;
}
@atika
Copy link
Author

atika commented Nov 3, 2014

Some functions use this to ask a question (https://gist.github.com/atika/655e9a2fca3efaca6b56), read command does not have the same use between bash/zsh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment