My CLI cheat-sheet

← Back to list
Posted on 02.06.2020
Image by David Travis on Unsplash

There are some things that can be useful to me, but I totally keep forgetting the syntax. And I am tired of googling this each time.

Well, not any longer!


My bash_profile

First, let me post my typical .bash_profile file I use on every system:

👉 📃  ~/.bash_profile
export PS1='[\t]:\u@\W\$ '
export PATH=${PATH}:${HOME}/bin:${HOME}/.node/bin:${HOME}
alias ll="ls -alFh"
alias cp="cp -r"
alias gitst="git status"
alias gitar="git add -A"
alias gitbr="git branch"
alias gitcom="git commit -m"
alias gitcomw="git commie -m wip"
alias gituncom="git reset HEAD~1 --soft"
alias gitchp="git cherry-pick -n"
alias k="kubectl"
alias kn="k -n <OUR_PROJECT>"
alias restart_pod='function __restartPod(){ kn scale deploy $1 --replicas=0; kn scale deploy $1 --replicas=$2; };__restartPod'
alias docker_stop_all='docker stop $(docker ps -aq)'
alias who_uses_port='function __whoUsesPort(){ lsof -n -i :"$1" | grep LISTEN; };__whoUsesPort'
alias tfplan="terraform plan -out=/tmp/plan -no-color -detailed-exitcode"
alias tfshowplan="terraform show -json /tmp/plan | jq '.resource_changes[] | select((.change.actions[] | contains(\"create\")) or (.change.actions[] | contains(\"delete\"))) | (.change.actions | join(\",\")) + \": \" + .address'"
The code is licensed under the MIT license



# display branching tree
git log --graph --full-history --all --pretty=format:"%h%x09%d%x20%s";
git log --graph --decorate --oneline;
# display history of commits for a specific branch
git log --walk-reflogs master;
The code is licensed under the MIT license

Manipulations with current changes

# remove last commit locally
git reset --hard HEAD~1
# un-stage all uncommitted changes
git reset HEAD
# un-stage all uncommitted changes and erase them
git reset HEAD --hard
# remove un-tracked files
git clean -fd
git clean -fX
# get the amount of lines to be changed
git diff --cached --shortstat <BASE_BRANCH>
# reset unmerged changes
git reset file.txt
git checkout -- file.txt
# abort the current unsuccessful merge
git merge --abort
# cherry pick without committing
git cherry-pick -n <COMMIT_HASH>
The code is licensed under the MIT license

Work with tags

# tag the current commit
git tag -a "tag_name"
# push the tag
git push origin "tag_name"
# get all remote tags
git fetch --all --tags
# kill remote tag
git push --delete origin v1.2.0
# kill local tag
git tag -d v1.2.0
The code is licensed under the MIT license

Work with submodules

# add a submodule
# init (clone) all submodules
git submodule update --init --recursive
The code is licensed under the MIT license


# do shallow clone
git clone <REPOSITORY> --depth 1
# get has of the current commit
git rev-parse HEAD
The code is licensed under the MIT license


# parametrized alias
alias dssh='function __sampleAlias(){ docker exec -it $1 /bin/bash; };__sampleAlias'
# get console command by its PID
ps -p <PID> -o args
# find where a binary is installed
# list all files sorting by date, human readable size
ls -ltah
# ... or reverse
ls -ltahr
# ... or by size
ls -lSah
# ... or by size reverse
ls -lSahr
# copy a file from a remote host over ssh working on port 55332
scp -P 55332 admin@ ./
# zip a folder
zip -r folder.zip folder/
# get file info
file ./file-name
# find out PID of a process that opened a port
lsof -n -i :<PORT> | grep LISTEN
# show sub-folder sizes in the current folder
du -sh *
# filter files by name against a wildcard, and delete
find . -name 'A*[0-9][0-9]' -delete
# filter files by name against a wildcard, revert selection and delete
find . -not -name 'A*[0-9][0-9]' -delete
# make group rights the same as the owner rights
chmod -R g=u ./
# redirect stdout and stderr to the same filie
cmd > ./file.txt 2>&1
# replace a string with another string in a file
sed -e "s/one/another/g" -e "s/string1/string2/g" file.txt > altered_file.txt
# list env vars
# traverse json
docker context inspect colima | jq -r '.[0].Endpoints.docker.Host'
The code is licensed under the MIT license


curl --request POST --header 'Authorization: token [TOKEN]' --url https://api.github.com/repos/[ACCOUNT]/[REPOSITORY_NAME]/actions/workflows/cd.yml/dispatches --data '{"ref":"master"}'
The code is licensed under the MIT license


# make a total package upgrade
yarn upgrade <PACKAGE_NAME> --major
# find out why the package was installed
yarn why <PACKAGE_NAME>
# get the latest version of a package that exists on NPM
yarn info <PACKAGE_NAME> | grep version:
# install a package only for a current user
yarn global add <PACKAGE_NAME> --prefix ~/.node
# upgrade a chosen version of a package
yarn upgrade <PACKAGE_NAME>@^
The code is licensed under the MIT license


# build an image
docker build -t [vendor_name]/[image_name]:[version] .
# run an image
docker run [vendor_name]/[image_name]:[version]
# run an image in daemon mode
docker run -d [vendor_name]/[image_name]:[version]
# run an image of a web application interactively, and forward a port
docker run -it -p [host_port]:[image_port] [vendor_name]/[image_name]:[version]
# push an image to dockerhub
docker login -u [username]
docker push [vendor_name]/[image_name]:[version]
# "ssh" into a container with ID=[container_id]
docker exec -it [container_id] bash
# see all running containers
docker ps
# see logs of a container with ID=[container_id]
docker logs [container_id]
# copy file "config.txt" into a container with ID=[container_id] into /root folder
docker cp ./config.txt [container_id]:/root/
# remove all exited containers
docker rm $(docker ps -a -f status=exited -q)
# remove all working containers
docker rm $(docker ps -a -q)
# remove all images
docker rmi $(docker images -a -q)
# remove volumes, networks
docker system prune
The code is licensed under the MIT license


# run a command inside of a container without ssh into it
docker-compose exec [CONTAINER_NAME] [COMMANDS]
The code is licensed under the MIT license


# list pods
kubectl get pods
# exec a command on a pod
kubectl exec -it [pod_name] [cmd]
# get logs of a pod
kubectl logs [pod_name]
# delete a pod
kubectl delete pod [pod_name]
# apply a config file
kubectl apply -f [config_file]
# get information about a pod
kubectl describe pod [pod_name]
# list deployments
kubectl get deployments
# get information about a deployment
kubectl describe deployment [deployment_name]
# delete a deployment
kubectl delete deployment [deployment_name]
# apply rolling update on a deployment
kubectl rollout restart deployment [deployment_name]
# list all namespaces
kubectl get namespace
# list services in a particular namespace
kubectl get services -n [namespace]
# forward a port from a pod to the host
kubectl port-forward [pod_name] [host_port]:[container_port]
The code is licensed under the MIT license


# dump a database (will create database_name/[collections])
mongodump --db database_name
# import a database (will import all collections in database_name/ folder)
mongorestore -d database_name database_name
# mongo shell
# commands:
use [DB_NAME] # use a database
show collections # list collections
db.[COLLECTION_NAME].find() # query elements
exit # quit
The code is licensed under the MIT license


# install psql
sudo apt install postgresql-client-common postgresql-client
# connect to a database
psql postgresql://[USERNAME]:[PASSWORD]@localhost/[DB_NAME]
# commands:
\l # list all databases
\c [DB_NAME] # use a database
\dt+ # list tables
\q # quit psql
# dump a database
pg_dump --user test --port=5432 --host= [DB_NAME] > dump.sql
# create a new database
psql --user=test --port=5432 --host= template1 -c 'create database "[DB_NAME]" with owner test'
# import a database
psql --user=test --port=5432 --host= [DB_NAME] < dump.sql
The code is licensed under the MIT license


# pull the remote state and save it to a file
terraform state pull > state.tfstate
# push the remote state back
terraform state push state.tfstate
# to unlock the remote state
terraform force-unlock [LOCK-ID]
# to get the names of the resources to be created or deleted
terraform plan -out=plan -no-color -detailed-exitcode
terraform show -json plan | jq '.resource_changes[] | select((.change.actions[] | contains("create")) or (.change.actions[] | contains("delete"))) | (.change.actions | join(",")) + ": " + .address'
# to plan for destruction
terraform plan -out=plan -no-color -detailed-exitcode -destroy
# fix formatting
terraform fmt -recursive
# show formatting issues without fixing
terraform fmt -check -recursive —diff
The code is licensed under the MIT license


# launch chrome from the command line in Mac
alias chrome="/Applications/Google\\ \\Chrome.app/Contents/MacOS/Google\\ \\Chrome"
# drop the "Cannot open an app from unidentified developer" restriction for a particular application
sudo xattr -rd com.apple.quarantine /Applications/SomeApp.app
# format a usb flash stick
diskutil list external
diskutil eraseDisk MS-DOS "<label>" MBR <identifier>
# mount an iso image as an external drive
hdiutil mount <path_to_image.iso>
diskutil unmount /dev/<identifier>
The code is licensed under the MIT license


Sergei Gannochenko

Business-oriented fullstack engineer, in ❤️ with Tech.
Golang, React, TypeScript, Docker, AWS, Jamstack.
15+ years in dev.