Skip to content

Instantly share code, notes, and snippets.

@oranblackwell
Last active March 22, 2018 22:24
Show Gist options
  • Save oranblackwell/f8578cddc50816174f2f to your computer and use it in GitHub Desktop.
Save oranblackwell/f8578cddc50816174f2f to your computer and use it in GitHub Desktop.
Terminal commands

Various Terminal Commands

Creating Aliases

Note on interpolation

Invoked as an interactive login shell, or with `--login'

# Interactive means you can enter commands. The shell is not running because 
#+ a script has been activated. A login shell means that you got the shell after
#+ authenticating to the system, usually by giving your user name and password.

Files read:

  • /etc/profile
  • ~/.bash_profile, ~/.bash_login or ~/.profile: 1st existing readable file is read
  • ~/.bash_logout upon logout.

Invoked as an interactive non-login shell

#  Non-login shell means that you did not have to authenticate to the system.
#+ For instance, when you open a terminal using an icon, or a menu item, that is a 
#+ non-login shell.

Files read:

  • ~/.bashrc

This file is usually referred to in ~/.bash_profile: if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

Invokation Notes taken from here

Terminal

sudo vim ~/.bash_profile

File:

# Needs to be re-written to standardize the single and double quotes.
# try variation of %B[%@]%b  for filenames with date/time
# PWD doesnt seem to work as well as the prompt directory list does???

# Open current folder with Finder -> "open ."

#!~/.bash_profile
# Register seperator.
alias separator='echo "=============================================="'

# Alias to open this file.
alias aliases="sudo nano ~/.bash_profile"

# Alias to reload this file without restart the session
alias reload-aliases=". ~/.bash_profile"

## Directory Shortcuts ##
alias go-web="cd ~/Web; separator; echo 'Switched to Web folder!'; separator;"
alias go-sites="cd ~/Web/Sites; separator; echo 'Switched to Sites folder!'; separator;"
alias go-logs="cd ~/Web/Logs;separator; echo 'Switched to Logs folder!'; separator;"

## Custom Directory Shortcuts ##
alias go-dev='cd ~/Web/Sites/deployment-dev/; separator; echo "Switched to $PWD"; separator;'

# Apache Virtual hosts etc
alias go-vhosts="cd /private/etc/apache2/extra;separator; echo 'Switched to folder with vhosts! ;)';separator;"

# Mac Virtual hosts etc
alias machosts="sudo nano /private/etc/hosts"
alias flushcache="dscacheutil -flushcache; separator;echo 'Flushed Cache!';separator;"

# Directory Listing
alias dir="separator; echo 'Showing custom display for $PWD -P'; ls -lphAGP"
# -l: sum all lines to terminal
# -p: adds ‘/’ after directory
# -h: show sizes in bytes, kb etc
# -A: Show all excluding . and  ..
# -G: Colour // Dev Server needs  --color=always
# -P: shows symlinks’ destination
 
# Install Composer setup global access via alias.
# curl -sS https://getcomposer.org/installer | php
alias composer="php ~/composer.phar"

# Add MAMP's PHP to $PATH so that WP-CLI can run without causing a MySQL error.
export MAMP_PHP=/Applications/MAMP/bin/php/php5.3.6/bin
export PATH="$MAMP_PHP:$PATH"

# Add tab completion for WP-CLI
source ~/Web/Dependencies/wp-completion.bash

# Add MySQL to Path
export PATH=/Applications/MAMP/Library/bin:$PATH

## Screengrab from VirtualBox instance - puts to current folder with timestamp in filename. ##
alias screen-ie8='VBoxManage controlvm "IE8 - WinXP" screenshotpng screen-IE8-$(date +%Y%m%d-%H%M%S).png; separator; echo "IE8 Screenshot taken."; separator;'

## PHP Settings ##
PHPINIDIR='/Applications/MAMP/bin/php/php5.3.6/conf'

alias edit-php-ini="vim ${PHPINIDIR}/php.ini"
alias go-php-ini="cd ${PHPINIDIR}"
alias show-php-ini="open ${PHPINIDIR}"
alias show-phpinfo='php -r "phpinfo();"'
 
## Replace the terminal prompt: ##
# Orans-iMac: oran$
# with full path:
# [oran@Orans-iMac][/full/path/to/folder/]$
# or just
# [/full/path/to/folder/] $
if [ "$PS1" ]; then
  #PS1='[\u@\h][\w]\$ '
  PS1='[ \w]\$ '
fi

Show all hidden files (eg .gitignore)

Terminal:

#Enable 
defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder

# disable
# defaults write com.apple.finder AppleShowAllFiles -boolean false ; killall Finder

Show all hidden folders by default

Terminal:

#Enable 
sudo chflags nohidden /

# disable
# sudo chflags nohidden /

Autostarts an instance of the SSH Agent on login, then loads the the identies so that the deployment scripts can operate via RSA keys

# Autostarts an instance of the SSH Agent on login, then loads the the identies so that the deployment scripts can operate via RSA keys
SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
  eval `$SSHAGENT $SSHAGENTARGS`
  trap "kill $SSH_AGENT_PID" 0
  ssh-add
fi

Probably better to add these to git global config.

## Git ##
# Git log Superiority
alias git-lg="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Searches for empty folder up to 2 levels deep from current directory.

find . -maxdepth 2 -type d -empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment