Avatar

My CLI cheat-sheet

Photo by David Travis on Unsplash
Refill!

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!

Simpsons

Git

History

$
# 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;
# 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

Work with tags

$
# 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

Bash

$
# parametrized alias
alias dssh='function __sampleAlias(){ docker exec -it $1 /bin/bash; };__sampleAlias'
# launch chrome from the command line in Mac
alias chrome="/Applications/Google\\ \\Chrome.app/Contents/MacOS/Google\\ \\Chrome"
# get console command by its PID
ps -p [PID] -o args
# find where a binary is installed
which [CLI_COMMAND]
# 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 123.123.123.123 over ssh working on port 55332
scp -P 55332 admin@123.123.123.123:/home/remote/path/to/file.gz ./
# 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

Yarn

$
# 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

Docker

$
# build an image
docker build -t [vendor_name]/[image_name] .
# run an image
docker run [vendor_name]/[image_name]
# run an image in daemon mode
docker run -d [vendor_name]/[image_name]
# run an image of a web application interactively, and forward a port
docker run -it -p [host_port]:[image_port] [vendor_name]/[image_name]
# "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

Kubernetes

$
# 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]

Mongo

$
# dump a database (will create dump/[collections])
mongodump --db database_name
# import a database (will import all collections in dump/ folder)
mongorestore dump

Avatar

Sergei Gannochenko

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