Skip to content

Instantly share code, notes, and snippets.

@benyanke
Created May 10, 2018 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benyanke/e1dd3d0d9efbedeeaa47557e759c1aba to your computer and use it in GitHub Desktop.
Save benyanke/e1dd3d0d9efbedeeaa47557e759c1aba to your computer and use it in GitHub Desktop.
Useful Bash functions
# Syntax: "repeat [times to repeat] [command]"
# example: "repeat
function repeat()
{
local i max
max=$1; shift;
for ((i=1; i <= max ; i++)); do # --> C-like syntax
eval "$@";
done
}
# Useful LS shortcuts
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ls="ls -pB --color"
alias la="ls -pA --color"
alias ll="ls -phl --color" # long-form list
alias lla="ls -phlA --color"
alias lz="ls -pshS --color" # sort by file size
alias lza="ls -pshSA --color"
alias lt="ls -pghotr --color" # long-form, sort by modification date
alias lta="ls -pghotrA --color"
alias lx="ls -pghoX --color" # sort by file extention
alias lxa="ls -pghoXA --color"
alias lr="ls -ARpsh --color" # recursive list (watch out! It will fill your screen!)
# Make some of the file manipulation programs verbose
alias mv="mv -v"
alias cp="cp -v"
# Make grep show with color
alias grep="grep --color"
# Make nano show line numbers
alias nano="nano -c"
# Cuck Norris Development Advice - because why not
function chuck() {
curl https://api.chucknorris.io/jokes/random?category=dev 2> /dev/null | jq -r '.value'
}
# Make long directory trees on command
alias mkdir='mkdir -pv'
## a quick way to get out of current directory ##
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'
alias ......='cd ../../../../../'
alias .......='cd ../../../../../../'
alias ........='cd ../../../../../../../'
alias .........='cd ../../../../../../../../'
alias ..........='cd ../../../../../../../../../'
alias ...........='cd ../../../../../../../../../../'
alias ............='cd ../../../../../../../../../../../'
alias .............='cd ../../../../../../../../../../../../'
alias ..............='cd ../../../../../../../../../../../../../'
# For mac only: lock's screen
function lock() {
pmset displaysleepnow;
}
# List all EC2 boxes
# Ensure the AWS CLI is installed and
function ec2-boxes() {
aws ec2 describe-instances --query "Reservations[*].Instances[*].{name: Tags[?Key=='Name'] | [0].Value, PrivateIP: PrivateIpAddress, PublicIp: PublicIpAddress, InstID: InstanceId, AZ: Placement.AvailabilityZone$
}
# Simple shell examples
# example: 'cheat.sh curl' and it outputs a number of common curl examples
cheat.sh()
{
# replace native with the color scheme you want
# curl cheat.sh/:styles-demo to show the available color schemes
curl -s cheat.sh/"$1"?style=native
}
# Bash completion
_cheatsh_complete_cheatsh()
{
local cur opts #prev
_get_comp_words_by_ref -n : cur
COMPREPLY=()
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="$(curl -s cheat.sh/:list)"
#if [[ "${cur}" == ch ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
__ltrim_colon_completions "$cur"
return 0
#fi
}
complete -F _cheatsh_complete_cheatsh cheat.sh
# Set the default editor
export EDITOR=nano
# Json validator and formatter
# Example: 'curl example.com/file.json | json' - outputs formatted json
alias json="python -m json.tool"
alias jsonf="python -m json.tool"
# simple 'replace' tool for files
# Example: 'replace "oldstr" "newstr" file.txt' - replaces oldstr with newstr in file.txt
function replace {
sed -i "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g" $3
}
# Can't remember the syntax to open a tar?
alias untar="tar -zxvf"
function rr() {
curl -s -L https://raw.githubusercontent.com/keroserene/rickrollrc/master/roll.sh 2> /dev/null | bash
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment