Skip to content

Instantly share code, notes, and snippets.

@andfaulkner
Last active November 29, 2017 14:12
Show Gist options
  • Save andfaulkner/b396b1869c9e1392f109 to your computer and use it in GitHub Desktop.
Save andfaulkner/b396b1869c9e1392f109 to your computer and use it in GitHub Desktop.
Useful Unix commands (including postgres & redis related)
################################################################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROJECT INFO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
################################################################################
#display name of current git branch [TOADD]
alias curbranch="git branch | grep '\*'"
#output a divider to the cli [TOADD]
alias clidivider="echo '======================================'"
#provides the current directory name - without the full path [TOADD]
alias curdir="pwd | rev | cut -d'/' -f1 | rev"
#info about the current project: current git branch, project folder [TOADD]
alias curproj="echo ''; clidivider; echo 'CURRENT PROJECT:'; curdir; clidivider; echo ''; clidivider; echo 'CURRENT GIT BRANCH:'; curbranch; clidivider"
#################################################################################
################################################################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SYSTEM UTILS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
################################################################################
#count number of results a search returns
alias countf="find . | wc -l"
#trim whitespace before terminal output & condense large spaces into small ones [TOADD]
function trim {
awk '{$1=$1};1'
}
#make a file accessible to everyone
alias mkshared="sudo chmod -R a+rwxXst"
#find process running on a given port [TOADD]
alias pidofport="sudo netstat -nlp | grep" #port number here
#get size of a file, or total size of a directory + all subdirectories
alias fsize='du -chs'
alias lsa="ls -all"
alias lsd="lsa | grep '^d' | grep -v '\s\.\.\?$'"
alias lsf="lsa | grep -v '^d'"
## [see **]
alias ..="cd .."
alias ...="cd ..; cd .."
alias ....="cd ..; cd ..; cd .."
alias .....="cd ..; cd ..; cd ..; cd .."
alias ......='cd ..; cd ..; cd ..; cd ..; cd ..'
alias .......='cd ..; cd ..; cd ..; cd ..; cd ..; cd ..'
################################################################################
################################################################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ NODEJS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
################################################################################
#enables completion for npm commands in cli - place as is in .bash_profile file
. <(npm completion)
#kill all node processes
alias killnode="ps axu | grep node | cut -d' ' -f3 | grep '$' | sudo xargs kill -9"
#kill all gulp processes
alias killgulp="ps axu | grep gulp | cut -d' ' -f3 | grep '$' | sudo xargs kill -9"
#kill all nodemon processes
alias killnodemon="ps axu | grep nodemon | cut -d' ' -f3 | grep '$' | sudo xargs kill -9"
#kill all npm processes
alias killnpm="ps axu | grep npm | cut -d' ' -f3 | grep '$' | sudo xargs kill -9"
#kill all node-related processes
alias nukenode="killnode; killgulp; killnodemon; killnpm"
#list all node modules currently installed, and the location they're stored
alias nodelist="ls /usr/local/lib/node_modules/; echo '-------------------------------------'; echo 'global node modules location:'; echo '/usr/local/lib/node_modules/'; echo '-------------------------------------';"
#find version of globally installed npm package
alias npmpv="npm list -g | grep"
#information about npm and node modules [TOADD]
alias npminfo="npm config list"
#list all globally installed node modules [TOADD]
alias npmglist="npm list -g --depth=0"
#output the npm info I care the most about [TOADD]
alias npmkeyinfo="npm config ls -l | grep 'userconfig\|loglevel\|init-module\|cache\s\|^prefix\|shell\|node\-version\|globalignorefile\|globalconfig\|editor\|color' | rev | cut -d';' -f1 | rev | trim; npm config list | grep 'HOME\|cwd\|bin' | rev | cut -d';' -f1 | rev | trim"
################################################################################
################################################################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROCESSES ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
################################################################################
#EXAMPLES
#
#find all postgres processes running
ps axu | grep postgres
#find all redis processes running
ps axu | grep redis
#find all terminal processes running
ps axu | grep redis
#return pids of all processes running (pids, and only pids - nothing else)
ps axu | grep '\n' | awk '{ print $2 }'
#kill every process remotely related to postgres
ps axu | grep postgres | cut -d' ' -f3 | grep '$' | sudo xargs kill -15
#kill every process remotely related to redis
ps axu | grep redis | cut -d' ' -f3 | grep '$' | sudo xargs kill -15
#find all unique ids of port 3002 processes, with a name containing "node"
lsof | grep '3002' | grep node | cut -d' ' -f3 | grep -v '^$' | uniq | sudo xargs kill -9
#
################################################################################
# this magic command::: sed -e 's/^\s*//' -e '/^$/d' ::: removes whitespace from the start of the output
# this magic command::: awk '{ print $2 }' ::: only outputs the second column of a table
# this magic command::: awk '{ print $1" "$2 }' ::: outputs 1st & 2nd columns of a table, with a single space in between
#find all directories a postgres executable can be found
whereis postgres | cut -d' ' -f2- --output-delimiter=$'\n'
#install XTerm2 from command line only (this one === OSX, not Unix per se; more of a "model" than useful command on its own)
mkdir ~/temp_dwl; curl https://iterm2.com/downloads/stable/iTerm2-2_1_1.zip > ~/temp_dwl/iTerm2.zip; mkdir ~/temp_dwl/iTerm2dir; unzip -l ~/temp_dwl/iTerm2.zip -d ~/temp_dwl/iTerm2; sudo mv iTerm2/iTerm.app /Applications/
################################################################################
#~~~~~~~~~~~~~~~~~~~~~~~~ CLEVER BUT POSSIBLY DANGEROUS ~~~~~~~~~~~~~~~~~~~~~~~~
################################################################################
#create a permanent new path variable. example usage: new-path-var-2 /asdf <-- adds /asdf to PATH
function new-path-var-2 {
rm ~/.bashrc_tmp2 2>/dev/null
cat .bashrc | grep -v "^PATH=" | grep -v "^export PATH" > ~/.bashrc_tmp2;
echo 'export PATH='$PATH':'${1} >> ~/.bashrc_tmp2
mkdir -p ~/temp_bk
rm ~/temp_bk/.bashrc3 2>/dev/null
mv ~/temp_bk/.bashrc2 ~/temp_bk/.bashrc3 2>/dev/null
mv ~/temp_bk/.bashrc ~/temp_bk/.bashrc2 2>/dev/null
mv ~/.bashrc ~/temp_bk/.bashrc 2>/dev/null
mv ~/.bashrc_tmp2 ~/.bashrc
. ~/.bashrc
echo "new path:\n"
# $(echo 'export PATH='$PATH':'${1})
echo 'export PATH='$PATH':'${1}
}
#[**] - regarding the '..', '...', '....', '.....' etc. aliases:
#
# Save 3-18 key presses 1000X / day, and if it takes 0.01s/press...
# = ~100s/day saved.
# 100s * 300 days of typing in a year = 30,000s / year
# 50 years of typing = 30,000 * 50 = 150,000s total
# 150,000 / 60 / 60 = ~41.7hrs
# ~41.7hrs minus 15min (to come up with, code, write, & post this) = ~41.45hrs
# === ~2.5 (waking hour) days saved in a lifetime
# This excludes the 'train of thought' factor (more time typing
# after coming up with thought = less fluid train of thought)
# ...also it was fun.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment