Useful Linux commands

Find directory size:

cd to dir which size you want to know
du -chd 1 | sort -h (1 – subdirs shown in output, size output value not affected when changing)

Show HDD space usage:

df -h
Interesting utulity showing extended info about disk usage:
https://dev.yorhel.nl/ncdu

Show HDD’s installed in the system:

fdisk -l

Su as another user:

su USERNAME

Sleep in scripts

Example:
sleep .5 # Waits 0.5 second.
sleep 5 # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.
One can also employ decimals when specifying a time unit; e.g. sleep 1.5s
https://stackoverflow.com/questions/21620406/how-do-i-pause-my-shell-script-for-a-second-before-continuing

Find specific word in file:

grep -a “WORD-WE-NEED” /var/log/some.log

Watch for changes in file (usually we need to watch small status log):

watch -n 1 cat /var/log/some-status.log

Stress-test CPU:

https://ph0en1x.net/102-linux-cpu-stress-test-load-cores-tools.html
yes > /dev/null

Clear, remove, erase bash history::

cat /dev/null > ~/.bash_history && history -c
https://askubuntu.com/a/192001

Move all data from larger Linux Ubuntu partition to a smaller disk::

Prepare smaller VMDK
Stop original VM and copy VMDK in case of something going wrong to a safe place
Connect both old large and new small to a fresh created Ubuntu VM
Decrease old partition to fit new disk
Delete swap partition from original disk
Copy decreased partition with data to a new disk using dd: dd if=/dev/sdb of=/dev/sdc status=progress
Create swap partition on the new small disk and set it’s type as swap
Connect new small VMDK to original VM
Create swap file:
mkswap /dev/sda2
fallocate -l 512M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show
free -h

Discover current swap partition’s UUID by typing blkid
Setting new swap partition’s UUID in /etc/fstab.
Test how VM is running
If everyting is OK delete old original VMDK and it’s copy

Change swapfile size:
swapoff -a
fallocate -l 512M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show
free -h

Very useful swap file configuration:
https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04

Scan subnet with result displaying MAC and IP addresses::

https://linuxhint.com/use-nmap-scan-subnet/
nmap -sP 192.168.0.1/24