Random stuff and opinions
My Dev Journal V3
And here’s a reminder on how important it is to document: https://jvns.ca/blog/brag-documents/
Dev journal v2 has done a pretty good job for me. I recently also added a functionality on my windows laptop to sync it up to a cloud storage (onedrive) via a windows automation scheduler called “Task Scheduler” at regular intervals of the day. I gotta say it’s pretty neat feature which I didn’t know about.
…LC: 1004
I was trying to solve this leetcode problem and had to refer the solutions after some minutes attempting it. Then came across this super simple ingenious solution. Took a lot of time trying to wrap my head around it. So I thought I’d just share my thought process was like trying to digest this super simple looking yet miraculous code:
Max consecutive ones iii
Link: https://leetcode.com/problems/max-consecutive-ones-iii
Note: you’ll need basic understanding of what this problem is asking for and may a preliminary very high level overview of the code flow
…
Common Dev Tooling Quickref
Some useful commands for some common dev tools. Assumption is that the reader is already familiar with the tools.
Vim
- Replace all instances:
%s/orignal/replacement/g
:w !sudo tee %
- See filename
ctrl + g
- Delete lines matching pattern
:g/<pattern>/d` (remove /d if only to show patterns)
- Highlight multiple patterns
:/\vpattern1|pattern2|…
- Go to bottom of match
GN
- Got to top of match
ggn
- Set and unset line numbers and relative numbers inside vim
:set nu rnu
:set nonu nornu
- Show x out of y count for searches
:set shormess-=S
Terminal
- save output to file/code
$ <command> 2>&1 | tee file.txt
- dig, whatis, whois, whoami
- TOTRY: tldr, exa, btop, lsd, duff
- show process tree
$ pstree -p (linux only)
- run command in background
$ nohup <other command> &
$ fg #to bring it back
$ jobs #to list process in background
- switch user
$ su - <username>
$ sudo curl -s https://cht.sh/:cht.sh > [cht.sh](http://cht.sh/)
$ sudo mv [cht.sh](http://cht.sh/) /usr/local/bin
$ sudo chmod +x /usr/local/bin/cht.sh
- set up cronjob
crontab -e
#<schedule_pattern> <path_to_executable_script>
#http://crontab.guru/
- ssh with agent forwarding
ssh -A <host>
#or just modify the .ssh/config file with `ForwardAgent=yes`
- Keeping keys in memory
$ eval `ssh-agent`
$ ssh-add <*secret_keys*>
$ ssh-add --apple-use-keychain .ssh/id_rsa #for mac
$ `ssh-add -L #list keys added
- Remove keys for a host
$ ssh-keygen -R <host>
- Resolving “REMOTE HOST IDENTIFICATION HAS CHANGED!” warning when ssh-ing
$ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null <host>
- access a website from terminal
$ curl -i <url>
- get active ssh sessions
$ who
- get all python versions
$ ls -ls /usr/bin/python* | awk ‘{ print $10 }’
- get IP of laptop
$ ipconfig getifaddr en0
- get environment variables
$ env #current session level
$ set #system wide level
- decompress gzipped files (.gz)
$ gzip -d <file>
- extract tar file
$ tar -xf <file>
- get md5 diffs
$ md5sum <files...>
- get permissions of current user
$ id
$ sudo -l
- copy all modified git files into a folder
$ git status -s | grep M | awk '{print $2}' | xargs -I {} cp --parents {} diff/
- find all files in directory tree and move them
$ find . -type f | xargs -I {} cp {} u18/
- Find filename pattern
$ find . -name "regex_pattern"
- Find directory
$ find . -type d -name "regex_pattern"
- Change default shell to zsh
$ chsh -s $(which zsh)
$ export LC_ALL=C.UTF-8
- Debug core dumps
$ gdb program_file core_file
- View core size limit
ulimit -c
- Grep around a pattern
$ grep -B 4 -A 4 foo_pattern README.txt` #4 lines Before and After pattern
$ grep -C 4 pattern #simpler
$ grep "^pattern" #should start with pattern
$ grep "pattern$" #should end with pattern
$ grep "..ttern" #matches any two chars followed by pattern eg: pattern, sottern, kettern
$ grep "p[aou]ttern" #pattern should match one of the chars in brackets
$ grep 'pattern1\|pattern2' #OR condition
- Grep header row as well
$ <command> | grep -E 'column|pattern'
- Live grep
$ tail -f <file> | grep <pattern>
- Number of lines in a gzipped file
$ zcat <file> | wc -l
- To know if your machine is virtual machine or bare-metal
$ virt-what #returns nothing if bare-metal
- Block device info
$ blkid or lsblk
- View boot logs
$ journalctl -b -1
- Get time in epoch
$ date +%s
- Change timezone
$ sudo timedatectl set-timezone UTC
- Sort lines in a file
$ sort -o file file
- View kernel configs
$ zcat /proc/config.gz | grep CONFIG_FRAME_WARN
- Dedupe hourly log filenames to get unique filename in directory:
$ ls | sed 's/-[0-9]\{10\}\(\.tar\)\?\.\(gz\|zst\)//' | sort | uniq
# eg: hello.log-2024022115.gz ⇒ hello.log
- Create symlink
$ ln -s <source> <symlink>
Tmux
- Swap panes
: switch-pane -s<pane#> -t<pane#>
- Sync all panes (set and unset)
: setw synchronize-pane
Git
- Auto add upstream
$ git config --global --add --bool push.autoSetupRemote true
- Show commit by a sha
$ git show d41ed690882c2345a249e17a1908232f92163a
- Rename branch
$ git branch -m old new
- Create branch from source and new
$ git checkout -b new old
- Diff file between two branch
$ git diff branch1 branch2 -- filename
- Show commits from one author
$ git log --author='email'
- Show which branch contains commit
$ git branch --contains 086062f79c5 #add -r to search remote
- Show branch matching name
$ git branch -rl ‘*pattern*’
- Show changes by commit
$ git diff commit~ commit
- Push a branch without checking out
$ git push --set-upstream origin <branch>
- Show commits matching string
$ git log --grep "<pattern>"
- Replace local branch with remote (first checkout )
$ git reset --hard origin/<branch>
- Sync files by not copying unnecessarily
rsync -avzhe ssh --checksum user@machine1:/remote_source/ /local_destination/ | grep -q 'Number of files transferred: 0'
Other useful resources
…Newbie Tax Traps
Taxes! Something you learn on the field often after it’s too late. Here are some things I wish someone told me.
⚠️Disclaimer: These are just words cooked up from my head and not definitive or exact. More like recalling stories from personal experience. The details I’ll leave it to you and google or CA.
1) Reporting your mid FY job change
TL;DR report your previous employer as soon as you join new company
…
My Dev Journal V2
Since the first blog on a dev journal here, I’ve made some changes.
With the onset of LLMs I’ve gotten help from chatGPT to upgrade my dev journal workflow.
Some things that have not changed are the log markdown file(daily_log.md) where your logs go into and the alias names.
Things that have changed:
1) Shell script
Instead of having a cron job update the daily dates and month headers on a set time, which can and will fail to run if your machine is not ON at the time of cron schedule. So instead this script below will be used to do regex checks on the log file and update dates and months as required.
…Util: CLI Dictionary
I end up having to lookup certain words which I’m not familiar with often and even though spotlight(macOS) provides quick lookup sometimes it mixes up with other files or OS related applications.
So if you are someone who finds yourself in the terminal most of your days this will be helpful.
What you’ll need
- Terminal
curl
CLI tool (which is inbuilt in most cases)jq
CLI command (google “how to install jq in your_os)
That’s it!
…My Dev Journal (could be yours too)
If you read tech blogs you’ll come across articles stating how important how important it is to maintain a dev journal or bragging doc or what have you. Basically it’s just a doc where you track all that you have done, read, watched, etc.
Your brain being a finite resource can only hold so much information. Over time you lose track of the code change you made, the valuable life advice you gave to that junior, some quote someone said over a meeting. Okay that’s going overboard but you get the gist.
…Product Review: AirPod Pro vs Nothing ear(1)
I’ve owned the AirPod Pros for a little over a year and the Nothing ear(1) for about 2 weeks as of this writing. Below I share some personal experiences and opinions which may help in deciding if caught in a dillema between the two amazing products.
Official product links:
Let’s get started…
Active Noise Cancellation (ANC)
This is a close one at least in my ears. They both keep out noise pretty well. For any normal human out there, you will not be able to pick any difference.
…My Dotfile Favorites
Dotfiles are used to customize your system. The “dotfiles” name is derived from the configuration files in Unix-like systems that start with a dot (e.g. .bash_profile
and .gitconfig
). In this post I will share some of my most used configurations for some common dotfiles.
.zshrc
greph () {
fc -ln 0 | grep --color=auto $1 | awk '!seen[$0]++' | sed -e 's/^/>> /'
}
I love this one. Probably my favorite in this dotfile. Usually I find myself having to type a very long CLI command to execute something. Then execute some other commands. If I have to repeat that same command, I’ll have to press up arrow key to go back the entire command execution history and find the command or re-type it from stratch or copy it in a notepad and paste it back. With this I can simply greph substring_of_command
and get every matching command that was recently executed.
TIL 2
jq
command
A command to manipulate json files. At work we usually have a bunch json serialized ouputs in S3 or some other datastore, which are absurdly many lines long and deeply nested. Often, if I need to grep something out of it, I’ll just copy and paste it to a company internal json prettifier to make it human readable and get what I need. There must be some out on the web too. Something like this.
…