TAR commands

tar --list -f backups_master_2019-07-11.tar
tar -xf TAR_FILE.tar
              
Linux Total Size of Directory

du -sh
              
Linux List Volumes and their Sizes

df -h 
              
Bash Curl Commands to send Form Parameters

curl -s -X POST -F 'par1=value' -F 'par2=value' http://HOST/PATH
OR
curl -s --data "par1=value&par2=value" http://HOST/PATH
              
Gnerating a public SSH Key

ssh-keygen -t rsa -b 4096 -C "YOUR_EMAIL@DOMAIN.COM"
              
Matching Regular Expression (Number) using grep

echo 'Hello. This is a number 342 for you to match' | grep -oP '(\d+\.*\d*)+'
              
Resize JPG images using ImageMagick

mkdir Resized
for i in $(ls *.JPG); do convert -resize 25% $i Resized/$i; done
              
Measure RaspberryPi Temperature

vcgencmd measure_temp
              
Linux Random 12 Bytes in HEX

openssl rand -hex 12
# To take the first 5 characters
openssl rand -hex 12|head -c 5
              
Installing Node.JS on Linux

export VERSION=v15.5.1
cd ~/Downloads
wget https://nodejs.org/dist/$VERSION/node-$VERSION-linux-x64.tar.xz
tar -xf node-$VERSION-linux-x64.tar.xz
cd node-$VERSION-linux-x64/bin
sudo rm /usr/local/bin/node
sudo rm /usr/local/bin/npm
cd /usr/local/bin
sudo ln -s /home/omar/Downloads/node-$VERSION-linux-x64/bin/node node
sudo ln -s /home/omar/Downloads/node-$VERSION-linux-x64/bin/npm npm
              
Docker Useful Shortcuts

# Remove Stopped Containers
docker rm $(docker ps -f status=exited -q)
# Remove Dangling Images
docker rmi $(docker images -f "dangling=true" -q)
              
Git Delete a Remote Branch

git push -d origin remote_branch_name
              
Git Pull Remote Branch

git checkout --track origin/remote_branch
              
Git Remove Untracked Files

git clean -f -d
              
Docker Swarm Init / Start a Service

docker swarm init --advertise-addr INTERNAL_IP_ADDRESS
docker service create --replicas 3 -p 9292:9292 --host database1:DATABASE_INTERNAL_IP -e RAILS_ENV=production --name chessduo chessduo:latest
docker service scale chessduo=5
              
Add Swap Memory to Linux Virtual Machine (Works on Digital Ocean)

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -h
              
Scan all open ports on a system

nmap -sT -p- localhost
              
List all files in a TAR file

tar --list --file=filename.tar.gz
              
Copying files over SSH, and ignoring duplicates on destination

This command can be useful in case you want to sync some daily backups:


rsync -va --ignore-existing [HOST:]PATH [HOST:]PATH
              
Postgres - Print Table Sizes

SELECT
    pt.tablename AS TableName
    ,t.indexname AS IndexName
    ,pc.reltuples AS TotalRows
    ,pg_size_pretty(pg_relation_size(quote_ident(pt.tablename)::text)) AS TableSize
    ,pg_size_pretty(pg_relation_size(quote_ident(t.indexrelname)::text)) AS IndexSize
    ,t.idx_scan AS TotalNumberOfScan
    ,t.idx_tup_read AS TotalTupleRead
    ,t.idx_tup_fetch AS TotalTupleFetched
FROM pg_tables AS pt
LEFT OUTER JOIN pg_class AS pc
    ON pt.tablename=pc.relname
LEFT OUTER JOIN
(
    SELECT
        pc.relname AS TableName
        ,pc2.relname AS IndexName
        ,psai.idx_scan
        ,psai.idx_tup_read
        ,psai.idx_tup_fetch
        ,psai.indexrelname
    FROM pg_index AS pi
    JOIN pg_class AS pc
        ON pc.oid = pi.indrelid
    JOIN pg_class AS pc2
        ON pc2.oid = pi.indexrelid
    JOIN pg_stat_all_indexes AS psai
        ON pi.indexrelid = psai.indexrelid
)AS T
    ON pt.tablename = T.TableName
WHERE pt.schemaname='public'
ORDER BY totalrows DESC;