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"
fiRun 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/
donexargs
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
fiCompression
| Kind | Compression | Extraction |
|---|---|---|
| tar | -czvf ARCHIVE.tar.gz FILE_OR_FOLDER | -xzvf TAR_FILE -C OUT_PATH |
| gzip | FILE | |
| zip | ARCHIVE.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; doneFilesystem
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 0Set symlink
bash
ln -s $SOURCE $TARGETRemove symlink for copy
bash
cp -Lr /usr/share/solr/ ~/solrTestI/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.logAppend multi-line to file
bash
cat <<EOF >>$FILE
foo
bar
baz
EOFWrite 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=1GNetworking
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 :80Process
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 $PIDSensors
bash
sudo apt-get install lm-sensors
sensorsSystem
bash
# get kernel version
uname -rsudo
Add user to sudo group
bash
sudo adduser username
sudo usermod -aG sudo usernameAssume user
bash
su - $USERDisable 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_PATHTools
base64
bash
# base64 binary decode
echo "$myImgStr" | base64 -d > image2.jpgopenssl
bash
# generate password
docs openssl rand -base64 24jq
bash
# string -> json
jq '.c | fromjson | .id' myFile.json
# json -> string
echo '{ "foo": [ "bar", "baz" ] }' | jq tostringrsync
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)"
fiGenerate public key from private key
bash
ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pubsnap
Remove unused:
bash
snap list --all | awk '/disabled/{print 1,"−−revision="1,"−−revision="3}' | xargs -rn2 sudo snap removeMisc
Desktop
bash
# check X11 / Wayland
echo $XDG_SESSION_TYPE
# install CUDA
sudo apt install nvidia-cudnn nvidia-cuda-toolkitShell
bash
# set default editor
sudo update-alternatives --config editorFix tracker-miner-fs-3 process
bash
tracker3 reset --filesystem
rm -fr ~/.cache/tracker3
# then rebootSet timezone
bash
sudo timedatectl set-timezone UTCUtilities
- Chmod Calculator
- RAID Calculator
- permissionator - chmod calculator for generating Linux file permissions
Resources
- Makefile Tutorial
- SRE deep dive into Linux Page Cache
- linux-insides - A book-in-progress about the linux kernel and its insides.
- Computing from the Command Line - 🐧 Linux command line and Scripting guide for beginner to intermediate users.
- Unix shell script tactics