Remove 1st character of a variable: var=${var:1}

Concatenate 2 variables: var1="${var1};$var2"

Remove specific char from a variable (here “): var=${var//\"}

recursively find the latest modified file in a directory: find . -type f -printf '%T@ %p\\n' | sort -n | tail -1 | cut -f2- -d" "

Show everything after a PATTERN: sed -n -e 's/^.\*PATTERN//p'

Same but remove all spaces: sed -n -e 's/^.\*stalled: //p'

Get http status code of an url: curl -o /dev/null --silent --head --write-out '%{http_code}\\n'

firewalld allow a port: firewall-cmd --permanent --add-port=1234/tcp && firewall-cmd --reload


Record Terminal

apt install -y asciinema

#Launch a record: 
asciinema rec filename.cast
#(end with Ctrl+d or “exit”)

Memory operations

Check memory usage (sorted)

ps -o pid,user,%mem,command ax | sort -b -k3 -r

Clear RAM Memory Cache / Buffer / Swap space

free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m

(PageCache (echo 1), dentries & inodes (echo 2), the three ones (echo 3)


Decrypt 50 times the content of a .txt file

(crappy method)

for item in `cat b64.txt`;do
        for count in {1..50};do
                if [ $count -eq 1 ]; then
                        current=$(echo "$item" |base64 --decode)
                else

                        current=$(echo "$current" |base64 --decode)
                fi
                if [ $count -eq 50 ]; then
                        echo $current
                fi
        done
done

(clean method)

state=$(<base64.txt)
for i in {1..100}; do
   state=$(<<<"$state" base64 --decode)
done
echo "$state"

(Backticks ` are discouraged. Use $(…) instead. bash deprecated and obsolete syntax)


Bash read content of a file(all methods) For loop

for line in $(cat file); do
  echo $line
done

While read

    echo "--- FILE READING # Method n°1 ---"
    while IFS= read -r line; do
	    printf '%s\n' "$line"
    done <data

Fast & efficient

    echo -e "\n\n--- FILE READING ---"
    value=$(<data)
    echo "$value"

Find latest modified folder and sort them

for folder in `ls /etc`; do
    if [ -z "$(ls -A /etc/$folder)" ]; then
        echo "$folder: EMPTY"
    else
        echo -ne "$folder: "
        find /etc/$folder/ -type f -exec stat \{} --printf="%y\n" \; |sort -n -r | head -n1
    fi
done

EXT4 Create partition bigger than 2TB

Here we are targeting sdb fdisk to be 4TB

apt install -y parted
parted /dev/sdb
mklabel gpt
yes
unit TB
mkpart primary 0.00TB 4.00TB
print
quit
fdisk -l
mkfs.ext4 /dev/sdb1

Image manipulation

Resize an image: convert -resize 25% organization1.png organization2.png

Reduce image quality: convert background.jpg -quality 50% background2.jpg

Convert image from png/jpg to webp: cwebp -q 60 organization2.png -o organization2_1.webp


SSH specificities

SSH port forwarding

… <INSERT DETAILS + Explanations> ssh -p8522 -L 13389:<IP_ADDR>:3389 <USER>@<IP_ADDR2>

ssh-copy-id cannot copy key

When the ssh-copy-id is copying key but this is still not working, with this when activating verbosity (-vvv)

/etc/ssh/ssh_config
PubkeyAcceptedKeyTypes +ssh-rsa

ssh “no matching host key type

/usr/bin/ssh-copy-id: ERROR: Unable to negotiate with <ipaddr> port 22: no matching host key type found. Their offer: ssh-rsa,ssh-dss

create ~/.ssh/config
Host <hostname>
HostName <hostname>
HostKeyAlgorithms=+ssh-rsa

/boot partition is full on Centos/rocky/RedHat

/etc/yum.conf -> installonly_limit=2
yum install yum-utils -y
package-cleanup --oldkernels --count=2

Python2.7 on old Centos Manual install

Only 2.6 on Centos6. <- This is breaking yum binary, so Ansible isn’t working anymore

yum groupinstall 'Development Tools' -y
wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
tar xvzf Python-2.7\*
cd Python-2.7.18
./configure --prefix=/usr/local
make

Alternative install method

instead of install, this will avoid the overwrite of default system python

make altinstall
python2.7 -V
ln -sf /usr/local/bin/python2.7 /usr/bin/python
python -V

Python3.6 on EoL Centos6

yum install gcc openssl-devel bzip2-devel -y
cd /usr/local/src/
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
tar xzf Python-3.6.6.tgz
cd Python-3.6.6
./configure --enable-optimizations

Alternative install method

instead of install, will avoid the overwrite of default system python

make altinstall
ln -sfn /usr/local/bin/python3.6 /usr/bin/python3.6
#Centos6 -> when trying to yum .... "Error: xz compression not available"
cat <<eof>\> /etc/yum.repos.d/epel.repo</eof>
\[epel\]
name=Extra Packages for Enterprise Linux 6 - $basearch
enabled=1
failovermethod=priority
baseurl=\[[http://mirrors.aliyuncs.com/epel-archive/6/$basearch\\](http://mirrors.aliyuncs.com/epel-archive/6/$basearch%5C)\](http://mirrors.aliyuncs.com/epel-archive/6/$basearch)
gpgcheck=0
gpgkey=http://mirrors.aliyuncs.com/epel-archive/RPM-GPG-KEY-EPEL-6
EOF