Most things today can be done by googling. Do you need to generate a random string? Google for a generator. Need to parse a JWT? Google for a parser. The problem with both of those cases is that it can be unsuitable to trust random internet pages. So in many cases it’s better to use CLI-tools as a default. So in this page I will try to collect ways of easily doing things in the CLI, preferrably without installing a lot of junk in your system.

Deleting file with unprintable characters

One example would be if you open vim and then type :w^Z, where ^Z is created by holding CTRL and clicking Z.

1. Run ls -b to check their escaped name (in my case this will be \032)

$ ls -b
\032  policy.rb  upp1.rb

2. Let echo -e print out the name for you in your command

rm $(echo -e "\032")

Dependabot patch bump node version

old_version="$(jq -r .version package.json)"
new_version="$(echo "$old_version" | awk -F. '{$NF += 1;print}' OFS=.)"
sed -i "s/\(\"version\": \"\)${old_version}\(\"\,\)/\1${new_version}\2/" package.json
git config --global user.name 'dependabot[bot]'
git config --global user.email '49699333+dependabot[bot]@users.noreply.github.com'
git fetch
git checkout "$GITHUB_HEAD_REF"
git add package.json
git commit -m 'bump version'
git push origin "$GITHUB_HEAD_REF"

Fake running program name

(exec -a 'vim thesis.tex' nethack)

Generating random strings

## Use OpenSSL as a randomizer
openssl rand -hex 32

Reading JWT content

If you have jq installed you can finish off by piping it to that.

jwt=eyJ...

## Using only default tools
cut -d. -f2  <<<"$jwt" | base64 -d

## Using jq
echo "$jwt" | jq -R 'split(".") | .[0:2] | map(@base64d) | map(fromjson)'

Rename multiple files

I have multiple directories with the following format:
directory.name [word123]
directory.name1 [word123]
directory.name2 [word123]
directory.name3 [word123]
etc.
I would like to remove the [word123] from each directory name. It is unreasonable to perform a mv for each as there are simply too many!

for r in ./*\ \[word123\]; do mv "$r" "${r%\ \[word123\]}"; done

Sort lines by counting occurances

| sort | uniq -c | sort -nr

Switch to a fake bash login-shell

exec -l bash           # If you're in bash:
bash -c 'exec -l bash' # If you're in another shell