Skip to content

Instantly share code, notes, and snippets.

@ryansechrest
Last active April 20, 2020 18:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryansechrest/7531965 to your computer and use it in GitHub Desktop.
Save ryansechrest/7531965 to your computer and use it in GitHub Desktop.
New and enhanced bash commands for developers on Mac OS X.
#!/bin/bash
# ---------------------------------------------------------------------------
#
# File: .bash_commands
#
# Author: Ryan Sechrest
# Website: ryansechrest.com
#
# Description: New and enhanced bash commands for developers on Mac OS X.
#
# ---------------------------------------------------------------------------
# Aliases
# ---------------------------------------------------------------------------
# Bash commands
# ---------------------------------------------------------------------------
function ryse_bash {
case $1 in
help)
echo 'List of commands:'
echo ' ~ Move into home directory'
echo ' .. Move into parent directory'
echo ' finder Open current directory in Finder'
echo ' flushdns Flush DNS cache'
echo ' hide Hide dot files in Finder'
echo ' ios Open iOS Simulator'
echo ' ip Display your local and public IP address'
echo ' ll Display detailed directory contents'
echo ' password [length] Generate random password and copy to clipboard'
echo ' ql <file> Preview file with Quick Look'
echo ' rmds Remove .DS_Store files recursively starting from current directory'
echo ' show Show dot files in Finder'
;;
reload)
source ~/.bash_profile
;;
update)
curl https://gist.githubusercontent.com/ryansechrest/7531965/raw/ -o ~/.bash_commands
ryse bash reload
;;
*)
echo 'Usage: ryse bash <command>'
echo ' help View list of commands'
echo ' reload Reload .bash_profile'
echo ' update Update .bash_commands'
;;
esac
}
function ryse_wp {
case $1 in
install)
# Clone WordPress configuration files
git clone https://gist.github.com/8430868.git .
rm -rf .git
# Initialize Git
git init
# Clone WordPress
git submodule add https://github.com/WordPress/WordPress.git wordpress
# Checkout selected WordPress version
cd wordpress && git checkout $2 && cd ..
# Create default directories
mkdir -p {wp-content/mu-plugins,wp-content/plugins,wp-content/themes}
# Copy themes of interest from WordPress repo
cp -R wordpress/wp-content/themes/twentytwelve wp-content/themes/
cp -R wordpress/wp-content/themes/twentythirteen wp-content/themes/
;;
*)
echo 'Usage: ryse wp <command>'
echo ' install <version> Install WordPress environment'
;;
esac
}
function ryse {
case $1 in
bash)
ryse_bash $2
;;
wp)
ryse_wp $2
;;
*)
echo 'Usage: ryse <command>'
echo ' bash Bash commands'
echo ' wp WordPress commands'
;;
esac
}
# Modify Terminal prompt to display Git branch and status
# ---------------------------------------------------------------------------
export PS1='\u@\h:\[\033[1;34m\] \w\[\033[0;33m\]$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$([[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "*")]/")\[\e[0m\]$ '
# Move into home directory
# ---------------------------------------------------------------------------
alias ~='cd ~'
# Move into parent directory
# ---------------------------------------------------------------------------
alias ..='cd ../'
# Display detailed directory contents
# ---------------------------------------------------------------------------
# a -> Include directory entries whose names begin with a dot.
# G -> Enable colorized output.
# h -> Use unit suffixes.
# l -> List in long format.
# p -> Write a slash after each directory.
alias ll='ls -aGhlp'
# Open iOS Simulator
# ---------------------------------------------------------------------------
alias ios='open -a /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app'
# Open current directory in Finder
# ---------------------------------------------------------------------------
alias finder='open -a Finder ./'
# Show/hide dot files in Finder
# ---------------------------------------------------------------------------
alias show='defaults write com.apple.finder AppleShowAllFiles TRUE && killall Finder'
alias hide='defaults write com.apple.finder AppleShowAllFiles FALSE && killall Finder'
# Remove .DS_Store files recursively starting from current directory
# ---------------------------------------------------------------------------
alias rmds='find . -type f -name ".DS_Store" -ls -delete'
# Preview file with Quick Look
# ---------------------------------------------------------------------------
alias ql='qlmanage -p "$*" > /dev/null 2>&1'
# Flush DNS cache
# ---------------------------------------------------------------------------
alias flushdns="dscacheutil -flushcache"
# Display your local and public IP address
# ---------------------------------------------------------------------------
function ip {
ifconfig | sed -En "s/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p"
dig +short myip.opendns.com @resolver1.opendns.com
}
# Generate random password and copy to clipboard
# ---------------------------------------------------------------------------
function password {
password=''
current_length=0
target_length=32
characters=(
q w e r t y u i o p a s d f g h j k l z x c v b n m Q W E R T Y U I O P A S D
F G H J K L Z X C V B N M 1 2 3 4 5 6 7 8 9 0
\! \@ \$ \% \^ \& \* \! \@ \$ \% \^ \& \* \@ \$ \% \^ \& \*
)
modulus=${#characters[*]}
if [ ! -z $1 ]; then
target_length=$1
fi
while [ $current_length -lt $target_length ]
do
index=$(($RANDOM%$modulus))
password+="${characters[$index]}"
((current_length++))
done
echo -n $password | pbcopy
}
#!/bin/bash
if [[ -e ~/.bash_commands ]]; then
source ~/.bash_commands
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment