Snippets are one of my favorite parts of programming. The shear endless depth of command line tricks and customizations is so exciting! Here is a collection I maintain for myself to comeback to.

Also navigable by list of snippet types here.


how to recover deleted file with git -- 12.09.2023 %

I’d like to build a new nest.js command based off a file I deleted last week. Here is how I go about getting that old file back via git restore

Step 1, find the file:

$ git log --name-only
/command
fd8bc2a6 Merge branch 'XXX-1111-remove-search-queue' into 'main'
fd07ddc3 XXX-1111: Remove search queue
apps/cli/src/cli.module.ts
apps/cli/src/db/backfill-search-queue.command.ts
apps/cli/src/db/db.module.ts
...

Step 2, restore the file:

$ git restore --source fd07ddc3~1 apps/cli/src/db/backfill-search-queue.command.ts

Note the ~1. The file in question was deleted in commit fd07ddc3, so to restore it we need to go one commit before it was deleted. ~1 does exactly that.

\- [ git ]

remove common lines with comm -- 24.05.2023 %

With help of ChatGPT I discovered this unix gem today.

Given two files

main_deck.txt:

1 Abzan Charm
1 Arcane Bombardment
1 Arcane Sanctum
1 Arcane Signet
1 Archmage Emeritus
1 Bant Charm
1 Boros Charm
1 Bring to Light
1 Brokers Charm
1 Brokers Confluence

considering.txt:

1 Abzan Charm
1 Arcane Sanctum
1 Taigam, Ojutai Master
1 Archmage Emeritus
1 Boros Charm
1 Tamanoa
1 Time Wipe
1 Trap Essence
1 Brokers Confluence

running

$ comm -23 <(cat considering.txt | sort) <(cat main_deck.txt | sort)
1 Taigam, Ojutai Master
1 Tamanoa
1 Time Wipe
1 Trap Essence

Returns a list of cards that are unique to the considering.txt.

If your text files are sorted you can skip the <(cat ..) magic.

And yes. I’m using the cli two help build Magic the Gathering Decks!

\- [ comm ]

percol to pick lines -- 16.05.2023 %

Deep in a emac tutorial I discovered this hidden gem.

“percol adds flavor of interactive selection to the traditional pipe concept on UNIX.”

project github

You can simply place it in the middle of a pipe and pick the options you want.

Example:

$ BUCKET="s3://cool-bucket" aws s3 ls $BUCKET | awk '{print $4}' | percol |sed "s;^;${BUCKET}/;" | xargs aws s3 rm
delete: ...
asciicast

demo

\- [ percol ]

sort numerically -- 11.05.2023 %

Working with with sitemap xml files in AWS S3 today and the default sort is a bit hard to read.

Example:

$ aws s3 ls s3://cool-bucket | awk '{print $4}'
sitemap.brands.xml
sitemap.episode.0.xml
sitemap.episode.1.xml
sitemap.episode.10.xml
sitemap.episode.11.xml
sitemap.episode.2.xml
sitemap.episode.20.xml
sitemap.episode.21.xml
sitemap.episode.22.xml
sitemap.episode.23.xml
...

Using sort -V it sorts the lines numerically!

Working example:

$ aws s3 ls s3://cool-bucket | awk '{print $4}' | sort -V
sitemap.brands.xml
sitemap.xml
sitemap.episode.0.xml
sitemap.episode.1.xml
sitemap.episode.2.xml
sitemap.episode.3.xml
sitemap.episode.4.xml
sitemap.episode.5.xml
sitemap.episode.6.xml
sitemap.episode.7.xml
...
\- [ sort ]

auto nvm use -- 13.03.2023 %

Simply adding:

[ -f ./.nvmrc ] && nvm use

to your .bashrc or .zshrc

Will automatically run the nvm use command should you start the shell in a dir with a .nvmrc file.

\- [ nvm ]

git trim -- 23.02.2023 %

Easy clean up branches from local that are old or already merged on remote.

$ git trim -s -p
\- [ git ]

remove gps metadata from image -- 31.01.2023 %

Recently I noticed some of the images on my site had GPS metadata info. Thats not very private! So I did some googling and found this solution.

$ exiftool -GPS*= $(fd -e jpg -e png -e jpeg)

source

\- [ exiftool ]

update pleroma server -- 04.12.2022 %

I always forget how to upgrade my Pleroma servicer when a new version comes out so I’m writing it here. If you are unsure of what you are doing please consult the official docs.

My Pleroma instance: social.travisshears.xyz

Start by sshing into the server and switching to the pleroma user.

$ sudo su pleroma -s $SHELL 

Then stop the server request the update, migrate the db and start it again.

$ ./bin/pleroma stop
$ ./bin/pleroma_ctl update
$ ./bin/pleroma_ctl migrate
$ ./bin/pleroma daemon

Boom new version!

\- [ pleroma ]

discard unstaged changes -- 06.09.2022 %

Have a bunch of changes staged and want to drop the rest? Easy:

$ git restore .

source

\- [ git ]

diy git remote on nas storage -- 26.06.2022 %

Got a repo with sensitive data you don’t want to push to a remote server you don’t control? Have a NAS setup on our home network? Here is how to setup a folder on that NAS to act as a git remote.

*Step 1:

Change directorys to the NAS and clone the local folder with the –bare option.

$ cd /Volumes/travis/git
$ git clone --bare ~/.password-store

This creates /Volumes/travis/git/.password-store but without a working directory. Basically its just the /.git part of the repo.

Step 2:

Setup the NAS file path to be a git remote on the repo.

$ cd ~/.password-store
$ git remote add nas /Volumes/travis/git/travisshears.com.git
...

Step 3:

Done. Now jus push.

$ git push nas
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 10 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 1.30 KiB | 1.30 MiB/s, done.
\- [ git, nas ]