Linux CLI Tools


Command line interface tools are extremely powerful and scriptable but if you don’t know what commands exists they are useless. This is a listing of commands I find useful:

Basics

htop is so much better than regular top

atop is really useful when the system is lagging and you need to find the source, it’s not helpful if you don’t have a bottleneck

Files

ln -s /path/to/existing/file /path/to/create/symlink/at

stat <filename>

  File: index.html
  Size: 23              Blocks: 8          IO Block: 4096   regular file
Device: 10302h/66306d   Inode: 10496978    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   steve)   Gid: ( 1000/   steve)
Access: 2021-11-13 21:31:04.369244329 +0000
Modify: 2021-01-22 03:21:38.870678829 +0000
Change: 2021-01-22 03:21:38.870678829 +0000
 Birth: -

df -h
human-readable disk space free

Filesystem      Size  Used Avail Use% Mounted on
udev 24G 0 24G 0% /dev
tmpfs 4.7G 3.4M 4.7G 1% /run
/dev/nvme0n1p2 228G 21G 196G 10% /
/dev/nvme0n1p1 511M 7.8M 504M 2% /boot/efi
ninetb-pool 5.3T 2.7T 2.7T 51% /ninetb-pool

lsblk – (file systems to disk)

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 8G 0 disk
├─nvme0n1p1 259:1 0 8G 0 part /
└─nvme0n1p128 259:2 0 1M 0 part

lshw

cpuid

lsusb

file -i <filename>
get the mime type of the file

traefik.yml: text/plain; charset=us-ascii

ssh-add -K ~/.ssh/keyfile

ssh-keygen -t rsa -b 4096 -o -a 100 -m pem
(generate a more secure 4096-bit ssh key than the default ssh-keygen)

head -n 1000 hugefile.txt > first1000lines.txt

tail -f logfile.txt
shows last 15 lines and follows updates to the file

screen
screen -r
(reconnects to existing terminal that stays running after ssh session ends)

tar zcvf ~/source.tgz path/to/expand/at

truncate -s 0 filename #zero the file out
truncate -s 2M filename # cut the file to only 2mb

\ls
(runs a bare ls without trying to figure out which are folders/files, useful for large network directories that normal ls takes forever to output)

find . -mtime -1 -ls -xdev -type d
(finds files changed in the last 24h, skipping the proc and sys folders)

sudo usermod -a -G newGroup username

sudo chmod g+w file
(add group write permission)

traceroute -q 1 -m 50 -w 1 zazeski.com
fast lookup since so many routers discard ICMP traffic
(only 1 per hop with 1 sec timeout, stop at 50 hops)

gron https://api.checkssl.org/v1/ip
converts json into grep output, also good to figure out the json path (github)

json = {};
json.ip = "98.212.86.98";

uname -a
Linux ip-172-1-1-1 5.4.0-1034-aws #35-Ubuntu SMP Thu Dec 17 23:32:42 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

sudo shutdown now (did you know halt doesn’t tell the motherboard to actually shut off)

prettyping <URL> (better graphical command-line ping tool that makes it easy to see how it’s performing)

Bash Snippets

#! /bin/bash

YESTERDAY_DAY_LINUX=`date -d "yesterday 13:00" '+%d'`
YESTERDAY_DAY_MAC=`date -v-1d +%d`
YESTERDAY="${YESTERDAY_DAY_LINUX}${YESTERDAY_DAY_MAC}"
date -u +"%Y-%m-%d" #2019-03-25

VAR="test"
echo $VAR
echo ${VAR}

ls > /path/to/log
ls >> /path/to/log/and/append

.bashrc

echo "---"
echo ".bashrc OK"

# some more ls aliases
alias ll='ls -alFh'
alias la='ls -A'
alias l='ls -CF'

# If you use android, these are helpful 
export ANDROID_HOME=/Users/steve/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools

# Node Version Manager (NVM)
export NVM_DIR="$HOME/.nvm"

# This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] &amp;&amp; \. "$NVM_DIR/nvm.sh"  

# This loads nvm bash_completion
[ -s "$NVM_DIR/bash_completion" ] &amp;&amp; \. "$NVM_DIR/bash_completion"  

echo "---"

Netem (simulating poor network conditions)

# add 100ms delay to eth0
sudo tc qdisc add dev eth0 root netem delay 100ms 

# remove 100ms delay to eth0
sudo tc qdisc delete dev eth0 root netem delay 100ms

VIM

ddkP
Move the current line up one (by deleting the line, saving to buffer, and pasting it back in)

d$
Delete the rest of the line from the current cursor

dG
Delete all lines below the cursor

dt(space)
Delete current word until space character

ggVGu
Goes to the top of the document and lowercases all the letters

Save to a read-only file
ESC
:w !sudo tee #
Type [O]k or [L]oad, its already saved but you are looking at a stale copy, then run :q! (yes ignoring changes)

To search type ?<word> <enter>, to find the next occurrence press shift + N

Docker

docker ps -al

docker pull <imagename>

docker system prune 
#removes dangling images

docker system prune -a 
#aggressively removes all not currently running images

docker builder prune
#on mac and windows a vm is actually running the builds, if you get weird errors about disk space when you still have some, its because this vm is out of disk space.

docker logs --tail 100 <ontainername>

docker stats
# makes it easy to see resource usage of each container

CONTAINER ID   NAME                                           CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
1ac61a0df2c6   Jenkins.1.fs0ka1vd5bxt8ou6cfo2429t1            0.79%     5.119GiB / 62.72GiB   8.16%     38.8MB / 25.1MB   2.85MB / 15MB     56
a452e7543172   portainer                                      0.00%     16.31MiB / 62.72GiB   0.03%     2.37MB / 9.22MB   512kB / 384MB     7

Kubernetes

k9s is an amazing ncurse cli interface that makes kubectl commands so much easier to work with.

To switch which cluster you are connected to:

kubectrl config get-contexts
kubectrl config use-context <name>

Add this line to your .bash_profile and you can type kubeproxy to display the security token, launch your web browser to the dashboard and start the proxy server.

alias kubeproxy="kubectl describe secret && open http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/login && kubectl proxy --port=8001"

You have a bunch of failed pods from a deployment and want to purge them: (BE CAREFUL – if you have a service with the same name, they are independent and have to be removed as well)

kubectl get pods | grep Error | cut -d' ' -f 1 | xargs kubectl delete pod

kubectl get services | grep worker-204 | cut -d' ' -f 1 | xargs kubectrl delete service

kubectrl port-forward rabbitmq 15672:15672

GIT

You just messed up and pushed something to origin that you should not of –no one else has pulled yet, quick undo it before anyone pulls.

git reset --hard HEAD~1
git push --force

removes all untracked files/folders from the git repo

git clean -fd

You want to remove an existing remote GIT server to add a new one named origin (you can also edit it but this lets you copy/pate from github/bitbucket)

git remote remove origin
git remote add origin git@bitbucket.org:project/website.git
git push -u origin master

Additional 3rd Party Tools

ncdu (ncurses disk utlility)
great tool to find where all your disk space is being used on linux
(q to exit)

sudo apt install ncdu
sudo yum install ncdu

nmap (network mapping) - port scanning
ntop - like top but for networks
ctop - docker container top
htop -
tldr - short man pages about

openanalytics 1662 views

I'm a 35 year old UIUC Computer Engineer building mobile apps, websites and hardware integrations with an interest in 3D printing, biotechnology and Arduinos.