CLI Snippets
Published:
This page is a collection of various easy to forget, but useful, CLI snippets. It is mainly for me to remember how to do some task or why I did something in a particular way.
git
The only sane way to use git is magit.
However, some things are easier in a CLI.
When using github as a central repository, the remote repository has to be an ssh repo for pushing.
We can use the following to set origin to point to github:
git remote set-url origin git@github.com:user/repo.git
Then, to push the current branch to a remote repository:
git push -u origin `git branch --show-current`
The -u is needed for git pull to work correctly.
gpg
Use gpg --full-generate-key to generate a gpg key.
It is recommended that you create separate subkeys for day-to-day encryption and signing.
A subkey for encryption is created automatically.
Use gpg --edit-key [email id] and addkey to create additional signing keys.
The following commands obtain key fingerprints:
$ gpg --list-keys
# shows fingerprints of the main keys.
$ gpg --list-keys --with-subkey-fingerprints [main key fingerprint]
# shows fingerprints of subkeys
It is safer to only keep the main secret key marked [SC] in offline storage.
Use gpg --armor --export-secret-key [email id] to create a backup.
After that, delete the main secret key using gpg --delete-secret-key [email id].
Verify that the deletion was successful using gpg --list-secret-keys [email id].
It should show sec# near the main key.
You can still sign or decrypt using the subkeys.
The following snippet creates a detached signature using a subkey and verifies it:
$ gpg -s -b -a -u [subkey fingerprint] plain.txt
$ gpg --verify plain.txt.asc
The detached signature will be written to plain.txt.asc. To verify, we need both the data file and the signature file in the same directory.
Use gpg --import [key file] to import a public/private key.
less
For me, less is an alias for:
less -QcN --line-num-width=3 --no-vbell
The options QcN are for quiet, clear, show line number.
The default width of line number column is too large, I make it 3.
If we disable audible bell, less notifies end of file using a visual bell, a flashing screen, I disable that too.
rsync
rsync is a command-line tool to synchronize files between local/remote locations. Here's how I use it to publish my website:
rsync -rDv -f'- _*' -f'- *~' . user@ip:/path/to/www/dir
The options rDv are recurse, delete (from server), and verbose.
The draft HTML files have a leading underscore in their name.
The option -f'- _*' is a filter rule to exclude them.
The -n option may be used for a dry run.
tr
We can use POSIX tr to rot13 encode text.
I use it to encode hints to questions in timed exams.
For evaluated exams, if hints are given as is, a student gains no advantage by not reading hints.
Since it takes a bit of time to decode rot13, this scheme provides a small advantage for students who do not need the hint.
tr 'a-zA-Z' 'n-za-mN-ZA-M'
Although I use emacs' M-x rot13-region almost always.