shell

Run incognito/private commands for Bash and Zsh

Bash


Simply put a space before the command and it won't be saved to `~/.bash_history`. E.g. ` qrencode -o out.png "Some secret text"`

Zsh


Enable the following option to be able to run private commands by adding a space before and prevent it from being saved to `~/.zsh_history`.

setopt HIST_IGNORE_SPACE
4
Hicham

Upgrade postgres database with homebrew

To upgrade postgres you can run 

brew upgrade postgresql

After you've done that your existing databases will have data that is incompatible with the your new postgres binary. Normally you would have to use pg_update and pass in the correct binaries to migrate the data, however, brew includes an upgrade script to update the data for you.

brew postgresql-upgrade-database

6
Joe

Angry Docker Purge

If you receive No Space left error on Docker this the brute force solution:

It would stop and delete ALL containers, images and volumes.

docker stop $(docker ps -aq); docker rm $(docker ps -aq); docker volume rm $(docker volume ls -qf dangling=true);docker system prune -af


4
Babar

Run same command on different input/inputs

If you have to run the same command repetitively over different input/inputs.

Example input:

c595270372e3
c04094382ae1
498a6505a1f7
4ac0e1872789
170a7f2ec51a
930743cb4e19


Command:

pbpaste | xargs -I id docker image rm id


Note: pbpaste is clipboard paste command for Mac, would be different for operating systems.

1
Babar

Check for IKEA item availability

A bit of a hack, but we want to be notified if an item becomes available at IKEA.

This is just a shell script that should work on any *NIX that has curl installed.

This is how their API response looks like (replace th with your country code): https://www.ikea.com/th/en/iows/catalog/availability/S99067497

There are only two stores in Thailand, both of which are close enough to the Oozou office, we only care that it's available in one of those stores.

#!/usr/bin/env bash

url="https://www.ikea.com/th/en/iows/catalog/availability/$1"

# This is the string that we want to see changed
string="0"

while true; do
  # Get the API response and return as string
  text=$(curl -s "$url")

  # Get a count of how many times the substring matches
  count=`grep -o "$string" <<< "$text" | wc -l`

  # If less than 2, then it's available!
  if (( $count < 2 )); then
    echo "AVAILABLE, yay.";
    # Notify us via Slack
    curl -X POST --data-urlencode "payload={\"channel\": \"…\", \"username\": \"ikea\", \"text\": \"Item $1 is now available at IKEA.\", \"icon_emoji\": \":shooping_bags:\"}" https://hooks.slack.com/services/…
    break
  else
    # If not available, sleep for 30s
    echo "$1 not in stock"
    sleep 30
  fi
done



Pass in your item ID to start:

$ ./check.sh S99067497


5
Const

Go through a file Git history (GUI)

$ gitk --follow file


Ali

Kill unresponsive SSH session

Occasionally an SSH session may become unresponsive. If I put my laptop to sleep this happens fairly often. Rather than killing the tab you can use SSH Escape Characters to kill the session

$ ~.


To view all escape characters check man ssh and search for ESCAPE CHARACTERS $ /ESCAPE CHARACTERS

Joe

Kill rails that got stuck

I'd like to kill rails running w/specific port. (My laptop running several rails servers. )

kill -9 $(lsof -i :3000|grep ruby|head -1|awk '{print $2}')

Alyson

Using Git's aliases feature

Git allows you to create aliases internally through the config. Your global config should be located here ~/.gitconfig

[alias]
  aliases = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /' | sort
  count = shortlog -sn
  l = log
  co = checkout
  ca = commit -C HEAD --amend
  c  = commit
  cm = commit -m
  a  = add
  s  = status
  b  = branch
  p  = push
  mt = mergetool
  dt = difftool
  o  = origin
  rc = rebase --continue
  rs = rebase --skip
  pd  = !git checkout develop && git pull origin develop && git branch --merged | grep -v '*' | grep -v 'master' | xargs git branch -d && git fetch --prune origin
  pm  = !git checkout master && git pull origin master && git branch --merged | grep -v '*' | grep -v 'develop' | xargs git branch -d && git fetch --prune origin
  pr = pull-request -c -m


Most are self explanatory...

git pd (prune develop) - checks out to develop, fetches latest changes, deletes all stale branches except develop (will delete master too)

git pm (prune master) - does the same as prune develop for master

Joe

See deleted files in previous commits [git]

$ git log --diff-filter=D --summary --oneline


Ali

Find a branch based on a commit

$ git log --oneline --all --grep=pretty

c198bb715 Make default expired description logic pretty


$ git branch --contains c198bb715

* deploy/staging
  feature/ROC-3042


2
Ali

Check if command exists with shell script

if ! type gem > /dev/null; then
  echo 'gem command not found.'
fi


Tino

Delete merged git branches

Handy git command for deleting all local git branches that already merged git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

1
Stan

Watch CI status on Github

When you have Github's hub installed, you can get the ci-status of your current branch like this:

$ hub ci-status
pending


If you install watch (e.g. brew install watch) you can continuously see the state:

$ watch hub ci-status


Const

Bash aliases and magic

I have a seperate file in my user folder named ~/.aliases. I use this to store all of my bash aliases. I use aliases a lot and add new ones regularly so I created a bash function to add new aliases on the fly.

addalias() {
  echo 'alias '$1'="'$2'"' >> ~/.aliases
  source ~/.aliases
}


Now you can add an alias from the terminal which will be available to the current session and all new sessions... shell $ addalias foo 'echo "bar"'

If I find myself using commands over and over I will create aliases shell alias g='git' alias b='bundle' alias be='bundle exec' alias bu='bundle update' alias r='bin/rails' alias rr='bin/rails routes' alias migration='bin/rails generate migration' alias migrate='bin/rails db:migrate; bin/rails db:migrate RAILS_ENV=test' alias rollback="rake db:rollback; RAILS_ENV=test rake db:rollback" alias push='git push origin HEAD' alias pr='git pull-request -c -m' alias ppr="git push origin HEAD && git pull-request -m" alias pulls='git browse -- pulls' alias prs='git pr list' pull() { git browse -- pull/'$1' } alias la='ls -a' alias ll='ls -al'

2
Joe

Force push previous commit to repo

If you push some code and want to force push a previous commit you can do so with shell git push origin +commit_sha^:branch_name

Where git interprets sha^ as the parent of sha and + as a forced non-fastforward push.

You can also use HEAD^ where ^ is the parent of HEAD so HEAD^^^ would push the commit 3 commits before the current commit. e.g. shell git push origin +HEAD^^^:feature/i_did_an_oopsie

1
Joe

Get current git branch name

git rev-parse --abbrev-ref HEAD


Ali

Back up a file with same name + prefix

$ cp todo.txt !#:1.bak
cp todo.txt todo.txt.bak


NOTE: Actually you can use !#:1 in every command 'cause that's last argument passed to a command.

UPDATED: Now it works with zsh

Ali

Delete already-merged branchs in Git

$ git branch --merged | egrep -v 'master|develop' | xargs -n 1 git branch -d


You can safelist more branches in egrep -v 'master|develop|foo|bar'

828
Ali

Diff a local file with remote version in Git

Easily run the below command and get a diff version from the remote

$ git diff origin/master -- [local-path]


830
Ali

Remove unwanted commit from your branch

git rebase -p --onto SHA^ SHA


827
Tino

Send messages to Slack using cURL

Slack allows you to send messages via the Webhooks service

Create a webhook for any channel and then you can send notification using curl.

$ curl -X POST -H 'Content-type: application/json' --data '{"text": "Something important"}'  https://hooks.slack.com/services/YOUR/TOKENIZED/URL


23
Joe

List directory structure except one or more dirs

Ever wanted to display the structure of the current directory except or two directories?

#NPM or yarn project
tree -I node_modules


# Elixir Mix project
tree -I 'deps|test'


Ali

Use `host` to get more information about domains

You might be using dig to find out A/CNAME/MX etc records of a domain from DNS servers. But if you just want to get a quick overview, use host:

$ host oozou.com
oozou.com has address 52.89.55.136
oozou.com has address 35.165.206.160
oozou.com has address 34.212.209.65
oozou.com mail is handled by 1 aspmx.l.google.com.
oozou.com mail is handled by 5 alt1.aspmx.l.google.com.
oozou.com mail is handled by 5 alt2.aspmx.l.google.com.
oozou.com mail is handled by 10 aspmx2.googlemail.com.
oozou.com mail is handled by 10 aspmx3.googlemail.com.


2
Const

Zsh function to list file tree

Add this to your .zshrc

lst() {
    if [ "$1" != "" ] # or better, if [ -n "$1" ]
    then
        ls -R $1 | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
    else
        ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
    fi
}


Then you can run

$ lst spec

It should list all sub directory in spec folder in tree format.

You could change spec to any path you want.

or just use tree

<3

Tino

Create your own git commands (aliases)

Ever wanted to shorten a git command? You can go with shell alilas for sure but you have to always include context so that you don't forget what each ailas stands for but you can define your own commands in git using aliases.

git config --global alias.  


For example; I'm too lazy to type git status so I want to shorten this to git s

git config --global alias.s status


And here we go now you can just type git s and get the status of current repository.

Ali

Amazon AWS S3 CLI copy with set perms

Open term (or equivalent) and paste the following:

S3 copy

  • to upload a world-readable file:
aws s3 cp source.file s3://bucket/path/dest.file --acl public-read


Options for --acl parameters are:

  • private - visibility to owner only
  • public-read - readable by all
  • public-read-write - readable and writable by all
Jeff

Restore a deleted file in Git

# Find in which commit it's been deleted
$ git rev-list -n 1 HEAD -- 
4058ef780d2f6c4c6d57cfd7fd4ebe14c9dc28b3
# Restore it back
$ git checkout 4058ef780d2f6c4c6d57cfd7fd4ebe14c9dc28b3^ -- 


828
Ali

make a pr from command line using hub

install hub first with brew: brew install hub then use a simple command, like at the example below: hub pull-request -m "best pr ever" -b oozou:develop -h feature/awesome-improvements

or even smarter:

hub pull-request -m "best pr ever" -b oozou:develop -h $(git rev-parse --abbrev-ref HEAD)


for more detailed info how to make pull requests see at hub pull-request --help

827
Stan

Shallow clone a old repo

If you don't need the full git history for a repo you will be working on you can supply a clone depth to only clone x revisions of the repo.

git clone --depth=1 https://github.com/my/repo.git

829
Joe