Shell Snippets

Turn on pattern search for zsh history

bindkey "^R" history-incremental-pattern-search-backward
bindkey "^S" history-incremental-pattern-search-forward

Enables wildcard matches like * and ?.

Example

% history
…
46  ls
47  ssh isaacsu.com
48  echo "hello world"
49  ssh notes.isaac.su


<^R>
% ssh notes.isaac.su
bck-i-search: ss?_

<^R>
% ssh isaacsu.com
bck-i-search: ss*com_

Turn on NAT in linux using iptables

/sbin/iptables -A FORWARD -i tun0 -o wlan5 -m state --state RELATED,ESTABLISHED -j ACCEPT

Unzip files in lots of nested directories

for FILE in $(find . -type file); do \
  gunzip $FILE; \
done;

Cut a text file delimited by ":"

Given file.txt:

alice:female:melbourne
bob:male:sydney
cut -d':' -f1 file.txt

alice
bob
cut -d':' -f2 file.txt

female
male

Delete too many files

for FILE in $(ls too_many*); do rm $FILE; done;

Delete all Exited Docker Containers

docker ps -a | grep Exited |  awk '{print $1}' | xargs docker rm -f

Move a series of files

IMG_10040.jpg
IMG_10041.jpg
IMG_10042.jpg
IMG_10043.jpg
IMG_10044.jpg
IMG_10045.jpg
mv IMG_100{40..45}.jpg ../dest

Check free disk space on MacOS without app container excluding nullfs

df -h -T nonullfs