DevOps Buzz
Search…
Bash / Shell
Bitbucket
Distros
Elasticsearch
General
Guidelines / Standards
microk8s
Prometheus
RabbitMQ
VirtualBox
Cheat Sheet
Useful bash commands and snippets.

apt

Install specific package version

1
apt-cache policy kubelet
2
apt-get install kubelet=1.13.4-00
Copied!

Hold version

1
apt-mark hold kubelet kubeadm kubectl
Copied!

Remove hold version

1
apt-mark unhold kubelet kubeadm kubectl
Copied!

crontab

List crontab for all users

1
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
Copied!

du

Hidden files

1
du -sch .[!.]* * |sort -h
Copied!

Sort by size

1
du -hsc * |sort -h
Copied!

ncurses interface

1
sudo apt install ncdu
2
ncdu
Copied!

find

find and du -hsc

1
find . \
2
-name "*.txt" \
3
-maxdepth 2 \
4
-type d \
5
-exec du -csh --block-size=1M {} \;
Copied!

find and rm

1
find . -name "server.log-2018*" \
2
-print0 | xargs -0 rm -f
Copied!

Find and sed

1
find . -type f -print0 | xargs -0 sed -i 's/OLD/NEW/g'
Copied!

Tools

fzf

GitHub - junegunn/fzf: A command-line fuzzy finder
GitHub

grep

Sort by second column:
1
grep -air "count-" file-name* |sort -k2
Copied!

Hard drive management

List all hard drives and partitions:
1
sudo lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL
Copied!

journalctl

Get all logs

Get 20 most recent:
1
journalctl -n 20
Copied!

Get process logs

1
journalctl -u kubelet
Copied!

Processes management

Check if process is running

1
#!/bin/bash
2
service=mongod
3
4
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
5
then
6
echo "$service is running!!!"
7
else
8
/etc/init.d/$service start
9
fi
Copied!

Swap management

Create swap partition

1
sudo /bin/dd if=/dev/zero of=/var/swap.0 bs=1M count=1024
2
sudo /sbin/mkswap /var/swap.0
3
sudo /sbin/swapon /var/swap.0
4
echo "/var/swap.0 swap swap defaults 0 0" >> /etc/fstab
Copied!
Replace 1024 with your desired swap size in MB.

Disable swap

1
sudo swapoff -a
Copied!

systemctl

Check if process is enable

1
systemctl is-enabled PROCESS-NAME-HERE
Copied!

List all services

1
systemctl list-units --type service --all
Copied!

telnet

Client close connection

Input ctrl+]
Then execute:
1
close
Copied!

wget

Backup entire website.

1
wget \
2
--recursive \
3
--no-clobber \
4
--page-requisites \
5
--html-extension \
6
--convert-links \
7
--restrict-file-names=windows \
8
--domains devops.buzz \
9
--no-parent \
10
https://www.devops.buzz/public/
Copied!

References