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.


s3 move folder / rename folder -- 17.03.2022 %

Working on my Oblastle game today. In an effort to standardize the way I store images for the game in S3 I needed to move all the files with key image-service/images/oblastle/flags/ to /image-service/images/oblastle/flag/.

Here is how I did it:

$ aws s3 mv s3://travisshears.images/image-service/images/oblastle/flags/ s3://travisshears.images/image-service/images/oblastle/flag/ --recursive --profile personal

source

\- [ s3, aws ]

s3 copy recursive -- 03.03.2022 %

Where were you –recursive when I was hacking around with for file * ; do aws s3 cp $file…..

$ aws s3 cp ./ s3://travisshears.images/image-service/images/oblastle/context/ --profile personal --recursive
upload: ./us-ak.jpg to s3://travisshears.images/image-service/images/oblastle/context/us-ak.jpg
upload: ./us-co.jpg to s3://travisshears.images/image-service/images/oblastle/context/us-co.jpg
upload: ./us-ar.jpg to s3://travisshears.images/image-service/images/oblastle/context/us-ar.jpg
upload: ./us-ca.jpg to s3://travisshears.images/image-service/images/oblastle/context/us-ca.jpg
upload: ./us-al.jpg to s3://travisshears.images/image-service/images/oblastle/context/us-al.jpg
upload: ./us-ct.jpg to s3://travisshears.images/image-service/images/oblastle/context/us-ct.jpg
...

–recursive is an easy win to upload a bunch of files to s3.

\- [ aws, s3 ]

imagemagik .png to .jpg with white background -- 03.03.2022 %

Working on some .png map files today. I needed to covert them to small .jpg’s for uses in Oblastle. Problem being by default the transparent part fills into to black. Here is how to make it white:

$ ls
us-ak.png
us-al.png
us-ar.png
us-az.png
us-ca.png

$ for file in *.png ; do magick mogrify -format jpg -resize '500' -background white -flatten  $file; done

$ ls
us-ak.png
us-ak.jpg
us-al.png
us-al.jpg
us-ar.png
us-ar.jpg
us-az.png
us-az.jpg
us-ca.png

$ rm ./*.png

The important bit being -background white -flatten.

source

\- [ imagemagik ]

lower case string -- 13.02.2022 %

TR is a Unix until to translate characters I recently learned about as part of this awk snippet.

$ echo "TRAVIS" | tr '[:upper:]' '[:lower:]'
travis

source

\- [ tr ]

awk last column -- 13.02.2022 %

My first awk snippet! Today I needed to get all the file extensions in directory for a blog post I’m writing.

I solved it with:

$ fd . --type f | awk -F"." '{print $(NF)}' |  tr  '[:upper:]' '[:lower:]' | sort | uniq | pbcopy

Break down

fd . –type f, lists all the files in a directory recursively.

awk -F"." ‘{print $(NF)}’, the -F"." tells awk to split columns on “.”. The ’{print $(NF)’} tells awk to print the last column. Normally you do something like ’{print $2}’ to print the second column.

tr ‘[:upper:]’ ‘[:lower:]’, tr is a Unix until to translate characters. In this case all upper case letters will be translated to lower case. I’ve created a seprate snippet for it as well.

sort | uniq, a classic combo sorts the results then gets rid of duplicates.

pbcopy, anther common one for me pipes the result into the clipboard.

source

\- [ awk ]

close the garden -- 24.01.2022 %

Started playing with garden cli today. After playing around with the local kubernetes deployments I found it annoying it left some system containers running when I was finished. To get rid of these run the following from the project directory (the dir with project.garden.yml)

$ garden delete env

$ garden plugins local-kubernetes uninstall-garden-services
\- [ garden-cli ]

remove brew packages -- 18.01.2022 %

Trying to clean up my laptop a bit today by removing some unused brew packages.

Normally I would use brew list but this also list packages that are dependicies. Here is a way to list the top level packages:

$ brew leaves
asciinema
aspell
autoconf
awscli
bat
bumpversion
...

$ brew uninstall neomutt newsboat travisshears/tap/deploy_tool weechat
...

source

\- [ brew ]

wipe git commit times -- 14.01.2022 %

Some time you don’t want everyone to know exactly when you committed. This command will wipe the time stamps and set them all to today at 0:00h.

git filter-branch --env-filter '
  GIT_AUTHOR_DATE="$(date +%Y-%m-%d) 00:00:00+0000"
  GIT_COMMITTER_DATE="$(date +%Y-%m-%d) 00:00:00+0000"
  ' -- --all

It works best on fresh repos just be for pushing to remote for the first timee.

source

\- [ git ]

re export -- 05.01.2022 %

Today when writing a React hook I imported an enum type from another section of code. This enum was used as an argument to the hook. Any components that used this hook would also need to import that type enum. So just to use the hook at a minimum the component would need to import two things, the hook itself, and the type enum. To many imports cluttering stuff us!

import useCoolHook from 'Client/hooks/CoolHoook';
import HatStyleEnum from 'Client/hats/styles';

...
cool = useCoolHook(HatStyleEnum.cowboy);

What I found is the ability to re-export the type from the hook to keep the enum bundled with the hook and more easily accessible.

export {HatStyleEnum} from 'Client/hats/styles';

export const useCoolHook = (hatId: HatStyleEnum) => {
...

This way the components only have to have one import, saving space.

import useCoolHook, {HatStyleEnum} from 'Client/hooks/CoolHoook';

Also see similar snippet for js

source: stackoverflow

\- [ typescript ]

destructuring an array in javascript -- 29.11.2021 %

How I use to destructure Arrays:

const nums = [1,2,3];
const [a, _, c];
(a === 1) // true
(c === 3) // true

Problem is this _ is not needed and will cause problems with some ESLint setups. For example they might not allow unused variables. Turnes out you can just leave that spot blank!

const nums = [1,2,3];
const [a, , c];
(a === 1) // true
(c === 3) // true
\- [ js ]