Skip to content

Instantly share code, notes, and snippets.

@trvswgnr
Last active September 10, 2024 01:11
Show Gist options
  • Save trvswgnr/e0516edcd1dfa6a9ad14a064ce589fb9 to your computer and use it in GitHub Desktop.
Save trvswgnr/e0516edcd1dfa6a9ad14a064ce589fb9 to your computer and use it in GitHub Desktop.
My Bash Profile
# TAW Bash Profile
# @author: Travis A. Wagner
# @website: http://travisawagner.com
# -- BASE --#
# Set default blocksize for ls, df, du
# http://hints.macworld.com/comment.php?mode=view&cid=24491
export BLOCKSIZE=1k
# set PATH
export PATH="$PATH:/usr/local/bin/"
export PATH="/usr/local/git/bin:/sw/bin/:/usr/local/bin:/usr/local/:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
# -- MAKE TERMINAL BETTER -- #
alias ls='ls -Gp' # Preferred ‘ls’ implementation
alias cp='cp -iv' # Preferred 'cp' implementation
alias mv='mv -iv' # Preferred 'mv' implementation
alias mkdir='mkdir -pv' # Preferred 'mkdir' implementation
alias ll='ls -FGlAhp' # Preferred 'ls' implementation
cd() { builtin cd "$@"; ls; } # Always list directory contents upon 'cd'
alias cd..='cd ../' # Go back 1 directory level (for fast typers)
alias ..='cd ../' # Go back 1 directory level
alias ...='cd ../../' # Go back 2 directory levels
alias .3='cd ../../../' # Go back 3 directory levels
alias .4='cd ../../../../' # Go back 4 directory levels
alias .5='cd ../../../../../' # Go back 5 directory levels
alias .6='cd ../../../../../../' # Go back 6 directory levels
alias edit='brackets' # Opens file in Adobe Brackets
alias f='open -a Finder ./' # Opens current directory in MacOS Finder
alias htdocs='cd /Applications/MAMP/htdocs/' # Navigate to the MAMP websites folder
alias c='clear' # c: Clear terminal display
alias path='echo -e ${PATH//:/\\n}' # Echo all executable Paths
alias show_options='shopt' # display bash options settings
alias fix_stty='stty sane' # Restore terminal settings when screwed up
alias cic='set completion-ignore-case On' # Make tab-completion case-insensitive
mcd () { mkdir -p "$1" && cd "$1"; } # Makes new Dir and jumps inside
trash () { command mv "$@" ~/.Trash ; } # Moves a file to the MacOS trash
ql () { qlmanage -p "$*" >& /dev/null; } # Opens any file in MacOS Quicklook Preview
alias DT='tee ~/Desktop/terminalOut.txt' # Pipe content to file on MacOS Desktop
alias .='cd'
down() { cd ./"$*" ; }
go() { cd ./"$*" ; }
alias up="cd ../"
alias cellar="cd /usr/local/cellar"
alias edit_profile='edit ~/.bash_profile'
alias load_profile='source ~/.bash_profile' # source the current bash profile
alias gitaddall='echo -e "a\n*\nq\n"|git add -i'
# make an alias (shortcut file)
mkalias() {
if [ "$2" != "" ]; then
ln -s "$1" "$2"
else
ln -s "$1"
fi
}
# -- GIT -- #
alias git-change='git checkout'
alias cbranch='git checkout'
alias st="git status"
alias commit='git commit'
gcaa() { git add --all && git commit -m "$*" ; } # add and commit all files
# use custom nano
alias my="PATH=/usr/local/bin:$PATH"
alias nano="my nano"
# adds MongoDB to the PATH (user specific)
export PATH=/Users/travis/mongodb-osx-x86_64-3.2.6/bin:$PATH
# Full Recursive Directory Listing
alias lr='ls -R | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\'' | less'
# Search manpage given in agument '1' for term given in argument '2' (case insensitive)
# displays paginated result with colored search terms and two lines surrounding each hit.
# Example: mans mplayer codec
mans () {
man $1 | grep -iC2 --color=always $2 | less
}
# show the alias of some part (given some part of it)
showa () { /usr/bin/grep --color=always -i -a1 $@ ~/.bash_profile | grep -v '^\s*$' | less -FSRXc ; }
# -- FILE AND FOLDER MANAGEMENT --#
zipf () { zip -r "$1".zip "$1" ; } # zipf: To create a ZIP archive of a folder
alias numFiles='echo $(ls -1 | wc -l)' # numFiles: Count of non-hidden files in current dir
alias make1mb='mkfile 1m ./1MB.dat' # make1mb: Creates a file of 1mb size (all zeros)
alias make5mb='mkfile 5m ./5MB.dat' # make5mb: Creates a file of 5mb size (all zeros)
alias make10mb='mkfile 10m ./10MB.dat' # make10mb: Creates a file of 10mb size (all zeros)
# 'Cd's to frontmost window of MacOS Finder
cdf='cd `osascript -e "tell application \"Finder\" to if window 1 exists then if target of window 1 as string is not \":\" then get POSIX path of (target of window 1 as alias)"`'
# Extract most know archives with one command
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $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
}
# -- SEARCHING -- #
alias qfind="find . -name " # Quickly search for file
ff () { /usr/bin/find . -name "$@" ; } # Find file under the current directory
ffs () { /usr/bin/find . -name "$@"'*' ; } # Find file whose name starts with a given string
ffe () { /usr/bin/find . -name '*'"$@" ; } # Find file whose name ends with a given string
# spotlight: Search for a file using MacOS Spotlight's metadata
spotlight () {
mdfind "kMDItemDisplayName == '$@'wc";
}
# -- PROCESS MANAGEMENT -- #
# finds out the pid of a specified process
findPid () { lsof -t -c "$@" ; }
# Note that the command name can be specified via a regex
# E.g. findPid '/d$/' finds pids of all processes with names ending in 'd'
# Without the 'sudo' it will only find processes of the current user
# memHogsTop, memHogsPs: Find memory hogs
alias memHogsTop='top -l 1 -o rsize | head -20'
alias memHogsPs='ps wwaxm -o pid,stat,vsize,rss,time,command | head -10'
# Find CPU hogs
alias cpu_hogs='ps wwaxr -o pid,stat,%cpu,time,command | head -10'
# topForever: Continual 'top' listing (every 10 seconds)
alias topForever='top -l 9999999 -s 10 -o cpu'
# Recommended 'top' invocation to minimize resources
# http://www.macosxhints.com/article.php?story=20060816123853639
alias ttop="top -R -F -s 10 -o rsize"
# List processes owned by my user:
my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,start,time,bsdtime,command ; }
# -- NETWORKING -- #
alias myip='dig +short myip.opendns.com @resolver1.opendns.com' # myip: Public facing IP Address
alias netCons='lsof -i' # netCons: Show all open TCP/IP sockets
alias flushDNS='dscacheutil -flushcache' # flushDNS: Flush out the DNS Cache
alias lsock='sudo /usr/sbin/lsof -i -P' # lsock: Display open sockets
alias lsockU='sudo /usr/sbin/lsof -nP | grep UDP' # lsockU: Display only open UDP sockets
alias lsockT='sudo /usr/sbin/lsof -nP | grep TCP' # lsockT: Display only open TCP sockets
alias ipInfo0='ipconfig getpacket en0' # ipInfo0: Get info on connections for en0
alias ipInfo1='ipconfig getpacket en1' # ipInfo1: Get info on connections for en1
alias openPorts='sudo lsof -i | grep LISTEN' # openPorts: All listening connections
alias showBlocked='sudo ipfw list' # showBlocked: All ipfw rules inc/ blocked IPs
# display useful host related informaton
ii() {
echo -e "\nYou are logged on ${RED}$HOST"
echo -e "\nAdditionnal information:$NC " ; uname -a
echo -e "\n${RED}Users logged on:$NC " ; w -h
echo -e "\n${RED}Current date :$NC " ; date
echo -e "\n${RED}Machine stats :$NC " ; uptime
echo -e "\n${RED}Current network location :$NC " ; scselect
# echo -e "\n${RED}Public facing IP Address :$NC " ;myip
echo -e "\n${RED}DNS Configuration:$NC " ; scutil --dns
echo
}
# -- SYSTEMS OPERATIONS & INFORMATION -- #
alias mountReadWrite='/sbin/mount -uw /' # mountReadWrite: For use when booted into single-user
# Recursively delete .DS_Store files
alias deleteDS="find . -type f -name '*.DS_Store' -ls -delete"
# Show & Hide hidden files in Finder
alias showHidden='defaults write com.apple.finder ShowAllFiles TRUE'
alias hideHidden='defaults write com.apple.finder ShowAllFiles FALSE'
# Clean up LaunchServices to remove duplicates in the "Open With" menu
alias cleanupLS="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user && killall Finder"
# --WEB DEVELOPMENT -- #
alias apacheEdit='sudo edit /etc/httpd/httpd.conf' # Edit httpd.conf
alias apacheRestart='sudo apachectl graceful' # Restart Apache
alias editHosts='edit /etc/hosts' # Edit /etc/hosts file
alias herr='tail /var/log/httpd/error_log' # Tails HTTP error logs
alias apacheLogs="less +F /var/log/apache2/error_log" # Shows apache error logs
httpHeaders () { /usr/bin/curl -I -L $@ ; } # Grabs headers from web page
# Download a web page and show info on what took time
httpDebug () { /usr/bin/curl $@ -o /dev/null -w "dns: %{time_namelookup} connect: %{time_connect} pretransfer: %{time_pretransfer} starttransfer: %{time_starttransfer} total: %{time_total}\n" ; }
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Development
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
# source /usr/local/bin/virtualenvwrapper.sh
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
### Display current Git branch name in command prompt. ###
function parse_git_branch {
ref=$(command git symbolic-ref HEAD 2> /dev/null) || return
echo " ("${ref#refs/heads/}")"
}
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
SEAFOAM="\[\033[0;34m\]"
NO_COLOR="\[\033[0m\]"
MAGENTA="\[\033[0;31m\]"
GOLD="\[\033[0;32m\]"
BLUE="\[\033[0;33m\]"
export PS1="$NO_COLOR-------------------------------------------------------------------\n$MAGENTA\u$GOLD \W$BLUE\$(parse_git_branch)$SEAFOAM \$$NO_COLOR "
# -- Random Useful Things -- #
# Run a screensaver as the desktop background
alias screensaverBackground='/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background'
# -- REMINDERS & NOTES -- #
# remove_disk: spin down unneeded disk
# diskutil eject /dev/disk1s3
# # change the password on an encrypted disk image:
# hdiutil chpass /path/to/the/diskimage
# # mount a read-only disk image as read-write:
# hdiutil attach example.dmg -shadow /tmp/example.shadow -noverify
# mounting a removable drive (of type msdos or hfs)
# ---------------------------------------
# mkdir /Volumes/Foo
# ls /dev/disk* to find out the device to use in the mount command)
# mount -t msdos /dev/disk1s1 /Volumes/Foo
# mount -t hfs /dev/disk1s1 /Volumes/Foo
# create a file of a given size: /usr/sbin/mkfile or /usr/bin/hdiutil
# ---------------------------------------
# e.g.: mkfile 10m 10MB.dat
# e.g.: hdiutil create -size 10m 10MB.dmg
# the above create files that are almost all zeros - if random bytes are desired
# then use: ~/Dev/Perl/randBytes 1048576 > 10MB.dat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment