Personal Knowledge Base

A personal knowledge base

Shell Snippets

#Terminal Markdown Viewer

This shell function (zsh) allows you to view a rendered version of a Markdown file in your terminal.

# shell
function mdv() {
  (echo '<html>'; pulldown-cmark < "$1" ; echo '</html>') | elinks
}

Requires pulldown-cmark (cargo install pulldown-cmark) and elinks.

#Dasherise Text

This converts text like 'Some sample story--name' to 'some-sample-story-name':

echo 'Some sample story--name' | tr '[:upper:]' '[:lower:]' | tr -s ' ' -

The first tr makes the text lowercase, the second tr replaces spaces with dashes. The -s option squeezes mutiple occurances of space or dash into a single space or dash.

#Send Raw Email

# shell
sendmail -t < mail.txt

#Show Process Running on Port

# shell
lsof -i :3000

#tar Pipe

Merge files from one location to another

# shell
tar cf - app skin | tar -C magento-dev -xf -

This will overwrite existing files and add new ones, respecting the directory hierarchy.

#xargs

list files returned by locate with spaces in the path (the -0 does the necessary magic)

# shell
locate -0 quotes.db | xargs -0 -L 1 ls -l

#Calculate Digests With OpenSSL

# shell
openssl sha -sha256 ~/Downloads/rust-1.3.0-x86_64-apple-darwin.pkg

#rsync With Progress Information

# shell
rsync -avhP

Add -z option to use compression.

#rsync to Host With Non-standard Port

rsync -avz -e "ssh -p PORT" user@host:/path/ /path/

#List Commands Used by Popularity

Works best if you have a long command history. I have HISTSIZE=25000 in my zsh config.

# shell
history | awk '{ print $2 }' |sort | uniq -c | sort -n -r | less

#Sample Output

1503 cd
1290 ls
1271 vim
1227 g
1100 ag
1075 gco
1016 ts
 873 t
 868 gs
 729 gp
 710 ssh
 702 rspec
 581 rake
 485 cargo
 431 cat
 362 brew
 â‹®

#Fast, Pretty ZSH Prompt

https://github.com/edkolev/promptline.vim

#Copy Sample Files

In Rails projects it isn't uncommon to have a bunch of sample configuration files, the non-sample versions of which are not tracked in git (such as database.yml). The following snippet will copy the samples to their destination:

for file in *.sample; do cp ${file} $(echo "$file" | sed 's/\.sample$//'); done

#Command Line QR Codes

Using qrencode (it looks better in a shell).

qrencode -m 2 -t utf8 -o - 'Hello QR Code!'
█████████████████████████
██ ▄▄▄▄▄ █▀█ █ █ ▄▄▄▄▄ ██
██ █   █ █▄█▄▄▀█ █   █ ██
██ █▄▄▄█ █   █▄█ █▄▄▄█ ██
██▄▄▄▄▄▄▄█ █▄█▄█▄▄▄▄▄▄▄██
██  █ █▀▄ █▀▄  ▀▄▄▄█▄ ▀██
██ ▀█▄▄▄▄▀▄▄▀▀▄ ▀▄█ ▄▀ ██
████▄█▄█▄▄ ▀   ███ █▀████
██ ▄▄▄▄▄ █▄ ▄▀▄██ ▄▄▄ ▀██
██ █   █ █▀█▄▄   ▀▀▀▀▀▀██
██ █▄▄▄█ █▀█▀▄  ██▄█▄█▄██
██▄▄▄▄▄▄▄█▄█████▄▄█▄█▄███
█████████████████████████
Last modified: 09 January 2023