Skip to content

Linux

Bash

Flow control

bash
# loop
for file in *.txt; do cat $file; done

# if-else
if [ $(whoami) = 'root' ]; then
 echo "You are root"
else
 echo "You are not root"
fi

Run script every x interval

bash
while true;
do
    echo "hello";
    sleep 300; # 5 minutes
done;

Loop list from a file

bash
IFS=$'\n' images=($(cat need_to_process_files.txt))
for i in ${images[@]}
do
    aws s3 cp "s3://$BUCKET_NAME/$i" images/
done

xargs

bash
fd md | xargs -I {} topydo add @refactor {}

Generate random number

bash
r=$(( $RANDOM % 1000 + 1 ))

Check if file exists

bash
if [ -f ".env" ]; then
 source ./.env
else
 echo "env doesn't exist!"
    exit 1
fi

Compression

KindCompressionExtraction
tar-czvf ARCHIVE.tar.gz FILE_OR_FOLDER-xzvf TAR_FILE -C OUT_PATH
gzipFILE
zipARCHIVE.zip FILE or -r ARCHIVE.zip PATTERN
bash
# compress each file as gzip
for i in */*.jl; do echo "$i" && gzip "$i"; done

## as zip
for i in *.csv; do zip `basename $i .csv`.zip $i; done

# compress each folder
for i in *; do zip -r `basename $i`.cbz $i; done

Filesystem

Mount & Umount

bash
$ apt install ntfs-3g

## https://gist.github.com/etes/aa76a6e9c80579872e5f
sudo blkid # find devices
mkdir /mnt/volume
sudo chmod 770 /mnt/volume # set permission for mount point
sudo mount /dev/sda1 /mnt/volume

# Auto mount at boot
sudo nano /etc/fstab
UUID=D424912B2491119A /mnt/media FILE_SYSTEM uid=1000,gid=1000,nofail,umask=0 0 0
bash
ln -s $SOURCE $TARGET
bash
cp -Lr /usr/share/solr/ ~/solrTest

I/O

sed

bash
# replace string in text file
sed -i 's/old-text/new-text/g' input.txt

## can also use `#` as separator
sed -i.bak 's#$HOME#/home/runner/work#' scripts/docker-pytest.sh

## osx
sed -i .bak 's/old-text/new-text/g' input.txt

## recursive
grep -rl old-text . | xargs sed -i '' 's/old-text/new-text/g'

split

bash
split -l 300 file.txt new
split -b 500m httpd.log

Append multi-line to file

bash
cat <<EOF >>$FILE
foo
bar
baz
EOF

Write base64 string to json file

bash
echo "$GOOGLE_APPLICATION_CREDENTIALS_BASE64" | tr -d '"' | base64 -d > "./credentials/google_cloud.json"

Logs

journalctl

bash
# set maximum storage for logs
https://www.digitalocean.com/community/tutorials/how-to-use-journalctl-to-view-and-manipulate-systemd-logs

# prune logs
journalctl --vacuum-size=1G

Networking

bash
ip addr

sudo ip route

# inspect the NAT rules
sudo iptables -t nat -L POSTROUTING -v -n

# cleanup NAT rule
sudo iptables -t nat -D POSTROUTING -o "$HOST_IFACE" -j MASQUERADE || true

# find process by port
sudo lsof -i :80

Process

bash
# kill all task containing a name
pkill -9

# see killed processes
dmesg

# kill all processes
killall python3

# kill all processes - from grep
ps aux | grep "node dist/server.js" | grep -v grep | awk {'print $2'} | xargs kill -9

# get resources usage
top -p $PID

## MacOS
top -pid $PID

Sensors

bash
sudo apt-get install lm-sensors
sensors

System

bash
# get kernel version
uname -r

sudo

Add user to sudo group

bash
sudo adduser username
sudo usermod -aG sudo username

Assume user

bash
su - $USER

Disable password for sudo

bash
echo "$(whoami) ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/99-nopasswd-$(whoami)

Allow binary to run as sudo

bash
sudo visudo

# add this
$USER ALL=(ALL) NOPASSWD: $CADDY_PATH

Tools

base64

bash
# base64 binary decode
echo "$myImgStr" | base64 -d > image2.jpg

openssl

bash
# generate password
docs openssl rand -base64 24

jq

bash
# string -> json
jq '.c | fromjson | .id' myFile.json

# json -> string
echo '{ "foo": [ "bar", "baz" ] }' | jq tostring

rsync

bash
# copy with progress bar
rsync -ah --progress source-file destination-file

# move files
--remove-source-files

# ignore folder
rsync -avm --exclude='node_modules' --progress $HOST:$PATH $TARGET/

ssh

bash
if [ -z "$SSH_AUTH_SOCK" ]; then
	eval "$(ssh-agent -s)"
fi

Generate public key from private key

bash
ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub

snap

Remove unused:

bash
snap list --all | awk '/disabled/{print 1,"−−revision="1,"−−revision="3}' | xargs -rn2 sudo snap remove

Misc

Desktop

bash
# check X11 / Wayland
echo $XDG_SESSION_TYPE

# install CUDA
sudo apt install nvidia-cudnn nvidia-cuda-toolkit

Shell

bash
# set default editor
sudo update-alternatives --config editor

Fix tracker-miner-fs-3 process

bash
tracker3 reset --filesystem
rm -fr ~/.cache/tracker3
# then reboot

Set timezone

bash
sudo timedatectl set-timezone UTC

Utilities

Resources