Array in Bash
string="0123456789"                   # create a string of 10 characters
array=(0 1 2 3 4 5 6 7 8 9)           # create an indexed array of 10 elements
declare -A hash
hash=([one]=1 [two]=2 [three]=3)      # create an associative array of 3 elements
echo "string length is: ${#string}"   # length of string
echo "array length is: ${#array[@]}"  # length of array using @ as the index
echo "array length is: ${#array[*]}"  # length of array using * as the index
echo "hash length is: ${#hash[@]}"    # length of array using @ as the index
echo "hash length is: ${#hash[*]}"    # length of array using * as the index
Special characters in Bash

Special Variable Variable Details

- $1 to $n : $1 is the first arguments, $2 is second argument till $n n’th arguments.
 From 10’th argument, you must need to inclose them in braces like ${10}, ${11} and so on
- $0 : The name of script itself
- $$ : Process id of current bash
- $* : Values of all the arguments. All agruments are double quoted
- $# : Total number of arguments passed to script
- $@ : Values of all the arguments
- $? : Exit status id of last command
- $! : Process id of last command
Get cluster allocation
GET _cluster/allocation/explain\?pretty
Get shards status
GET _cat/shards?v=true&h=index,shard,prirep,state,node,unassigned.reason&s=state
Get category health
GET _cat/health?v
Get indices
GET _cat/indices?v
ElasticSearch Backup/Restore

Create or update snapshot repository API

PUT /_snapshot/my_repository

{
  "type": "fs",
  "settings": {
    "location": "my_backup_location"
  }
}
Verify snapshot repository
POST /_snapshot/my_repository/_verify
Repository analysis
POST /_snapshot/my_repository/_analyze?blob_count=10&max_blob_size=1mb&timeout=120s
Get snapshot repository
GET /_snapshot/my_repository
Delete snapshot repo
DELETE /_snapshot/my_repository
Cleanup snapshot repo
POST /_snapshot/my_repository/_cleanup
Clone snapshot

PUT /_snapshot/my_repository/source_snapshot/_clone/target_snapshot

{
  "indices": "index_a,index_b"
}
Create snapshot
PUT /_snapshot/my_repository/my_snapshot
Get snapshot
GET /_snapshot/my_repository/my_snapshot
Restore snapshot
POST /_snapshot/my_repository/my_snapshot/_restore
Delete snapshot
DELETE /_snapshot/my_repository/my_snapshot
List snapshots for one repo
GET /_cat/snapshots/repo1?v=true&s=id
Init a git repository
# Git global setup
git config --global user.name "myname"
git config --global user.email "myemail@domain.local"

#Create a new repository
git clone git@gitlab.domain.net:it/diskusagereports.git
cd diskusagereports
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

# Push an existing folder
cd existing_folder
git init
git remote add origin git@gitlab.domain.net:it/diskusagereports.git
git add .
git commit -m "Initial commit"
git push -u origin master

# Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.domain.net:it/diskusagereports.git
git push -u origin --all
git push -u origin --tags
Retrieve just one file from a remote repository
git archive --format=tar --remote=git_url HEAD -- <file> | tar xf -
Git: make hotfix
# display branch info
git branch -v

#go in branch hotfix/xx
git checkout -b hotfix/xx

# commit change
git commit -am "ton message"
git status

# push hotfix
git push origin hotfix/xxx

# MAJ branch master
git pull master

# update submodule
git submodule update --init --remote

# init submodule with branch
git submodule add -b branchname git@bitbucket.org:dir/repo.git path_to_submodule
Annuler un commit après un push
# Annuler un commit, c'est finalement appliquer l'inverse de son diff !
# On peut rediriger le diff des commits à annuler vers la commande patch --reverse :)
git diff HEAD^ | patch --reverse

# Pour faire plus simple, il y a git revert !
# Par exemple pour annuler les trois derniers commits :
git revert HEAD~3..HEAD

# Ou pour annuler un commit en particulier :
git revert 444b1cff
# Il suffit alors de pousser proprement le commit obtenu sur le serveur.
Branch manipulation
# To list all local branches
git branch
# To list all remote branches
git branch -r
# To list all branches (local and remote)
git branch -a

# If you are on the branch you want to rename:
git branch -m new-name

# Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name

# Reset the upstream branch for the new-name local branch.
# Switch to the branch and then:
git push origin -u new-name

# Deleting a single local branch
git branch -d <branchname>
# If you are sure you want to delete an unmerged branch:
git branch -D <branch>

# To delete all merged local branches:
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

# DANGER! Only run these if you are sure you want to delete unmerged branches.
# delete all local unmerged branches
git branch --no-merged | egrep -v "(^\*|master|dev)" | xargs git branch -D
# delete all local branches (merged and unmerged).
git branch | egrep -v "(^\*|master|dev)" | xargs git branch -D

# Deleting non-existent tracking branches
git remote prune <remote> --dry-run

# Deleting a single remote branch
git push <remote> --delete <branch>

# To delete all merged remote branches:
git branch -r --merged | egrep -v "(^\*|master|dev)" | sed 's/origin\///' | xargs -n 1 git push origin --delete
LDAP search commands
# use with klist :
ldapsearch -Y GSSAPI -LLL -H ldap://ad1 -b 'dc=corp,dc=domain,dc=net'

# with password
ldapsearch -LLL -H ldap://ad1 -b "dc=corp,dc=domain,dc=net" -D "cn=administrator,cn=users,dc=corp,dc=domain,dc=net" -w mypassword

ldapsearch -LLL -H ldap://ad1 -b "dc=corp,dc=domain,dc=net" -D "CN=name,OU=users,OU=domain,DC=corp,dc=domain,dc=net" -w xxxxx
Linux rename username

The really right way? Say you want to change user ‘peter’ to ‘paul’.

groupadd paul usermod -d /home/paul -m -g paul -l paul peter
list of file used by PID
lsof -a -p <PID>
# ls - l /proc/<PID>/fd
ps -aeo pid,pcpu,args --sort=-%cpu | head
Linux SiG Signal list of file used by PID
SIGHUP -HUP gracefully reloads the workers and the application
SIGTERM -TERM "brutally" reloads
SIGINT -INT and SIGQUIT -QUIT kills all the workers immediately
SIGUSR1 -USR1 prints statistics (stdout)
SIGUSR2 -USR2 prints worker status
SIGURG -URG restores a snapshot
SIGTSTP -TSTP pauses, suspends or resumes an instance
SIGWINCH -WINCH wakes up a worker blocked in a syscall
Remove all package marked as rc
dpkg --list |grep "^rc" | cut -d " " -f 3 | xargs sudo dpkg --purge
how-to-loop-through-file-names-returned-by-find

Execute process once for each file

find . -name '*.txt' -exec process {} \;

If you have time, read through the rest to see several different ways and the problems with most of them.

The full answer:

The best way depends on what you want to do, but here are a few options. As long as no file or folder in the subtree has whitespace in its name, you can just loop over the files:

for i in $x; do # Not recommended, will break on whitespace
    process "$i"
done

# Marginally better, cut out the temporary variable x:

for i in $(find -name \*.txt); do # Not recommended, will break on whitespace
    process "$i"
done

#It is much better to glob when you can. White-space safe, for files in the current directory:

for i in *.txt; do # Whitespace-safe but not recursive.
    process "$i"
done
# By enabling the globstar option, you can glob all matching files in this directory and all subdirectories:

# Make sure globstar is enabled
shopt -s globstar
for i in **/*.txt; do # Whitespace-safe and recursive
    process "$i"
done

# IFS= makes sure it doesn't trim leading and trailing whitespace
# -r prevents interpretation of \ escapes.
while IFS= read -r line; do # Whitespace-safe EXCEPT newlines
    process "$line"
done < filename
# read can be used safely in combination with find by setting the delimiter appropriately:

find . -name '*.txt' -print0 |
    while IFS= read -r -d '' line; do
        process "$line"
    done
# For more complex searches, you will probably want to use find,
# either with its -exec option or with -print0 | xargs -0:

# execute `process` once for each file
find . -name \*.txt -exec process {} \;

# execute `process` once with all the files as arguments*:
find . -name \*.txt -exec process {} +

# using xargs*
find . -name \*.txt -print0 | xargs -0 process

# using xargs with arguments after each filename
# (implies one run per filename)
find . -name \*.txt -print0 | xargs -0 -I{} process {} argument
Rsync ssh script
#!/usr/bin/env bash

checkBinary() {
  command -v "$1" >/dev/null 2>&1 || { echo >&2 "please install binary : $1. Aborting."; exit 1; }
}
checkBinary "rsync"

clear

STIME=$(date +"%X")
SOURCE_USER="appREMOTE"
SOURCE_HOST="app.mydomain.com"
SOURCE_DIR="/mnt/production/web/uploads/media/*"
DEST_DIR="/efs/media/"

rsync -avz \
      --chown=1000:1000 \
      -e "ssh -o StrictHostKeyChecking=no \
      -o UserKnownHostsFile=/dev/null \
      -i /home/ubuntu/.ssh/id_rsa" \
      ${SOURCE_USER}@"${SOURCE_HOST}":"${SOURCE_DIR}" \
      "${DEST_DIR1}"

printf "### Initiated at : %s\n" ${STIME}
printf "### Terminated at : %s\n" $(date +"%X")

exit 0
Benchmark script with strace

Now let’s remove all the packages marked as rc.

strace -o trace -c -Ttt ./scrip
# where:
# -c is to trace the time spent by cpu on specific call.
# -Ttt will tell you time in microseconds at time of each system call running.
# -o will save output in file "trace".
Compile script and crypt

Lien github

apt install shc

# to see content of encrypt script :
env bashOPTS=verbose ./test
mount RAID LVM disk recovery
  1. Boot avec un USB linux live
  2. Install mdadm
apt install mdadm

Build : RAID1 + LVM

mdadm --detail --scan
cat /proc/mdstat
mdadm -A -R /dev/md/8 /dev/sdc8
mdadm -S /dev/vg/lv
lvdisplay
mount /dev/lg/lv
Make ISO command line linux
# Install package
apt install genisoimage
mkisofs -allow-limited-size -l -J -r -iso-level 3 -o
# this command will generate bigger then 4GB ISO
Disabled SWAP on linux
swapon --show

NAME      TYPE      SIZE USED PRIO
/dev/sda2 partition   4G   0B   -1

fallocate -l 1G /swapfile
dd if=/dev/zero of=/swapfile bs=1024 count=1048576
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
nano /etc/fstab

# Edit : /etc/fstab
/swapfile swap swap defaults 0 0

swapon --show

NAME      TYPE  SIZE   USED PRIO
/swapfile file 1024M 507.4M   -1
Get detailled disk partitions
findmnt -Do TARGET,SOURCE,USED,SIZE,OPTIONS
Umask Linux
Umask   Created Files       Created Directories
_______________________________________________
000     666 (rw_rw_rw_)     777     (rwxrwxrwx)
002     664 (rw_rw_r__)     775     (rwxrwxr_x)
022     644 (rw_r__r__)     755     (rwxr_xr_x)
027     640 (rw_r_____)     750     (rwxr_x___)
077     600 (rw_______)     700     (rwx______)
277     400 (r________)     500     (r_x______)
NFS Server show clients
# show clients connected
netstat | grep :nfs
# list all connected clients
showmount -a
# Discover/view NFS shares from the client
showmount -e
# list active share on client
showmount -e <ip_adress>
# Exports all file system paths specified in the /etc/exports file
exportfs -r
Disable Bell on linux
rmmod pcspkr
# Blacklisting the pcspkr module will prevent udev from loading it at boot:
echo "blacklist pcspkr" > /etc/modprobe.d/nobeep.conf
Delete full disk on Linux
shred --verbose --random-source=/dev/urandom -n1 /dev/sda1
Compiling a linux Kernel
# Download the source code and pgp signature from https://www.kernel.org/ to a directory of your choice
mkdir -p /usr/src/
cd /usr/src/
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.16.tar.xz
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.16.tar.sign

# Uncompress the source code and check the signature
xz -d -v linux-5.1.16.tar.xz
gpg --verify linux-5.1.16.tar.sign

# Untar the source code and cd into the directory
tar xf linux-5.1.16.tar
cd linux-5.1.16/

# Copy over actual kernel config file and run 'make menuconfig'
cp -v /boot/config-$(uname -r) .config

# Install necessary packages
apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev bc

make menuconfig
# Go to Cryptographic API  --> Certificates for signature checking -->
# and leave 'File name or PKCS#11 URI of module signing key' and
# 'Additional X.509 keys for default system keyring' blank if not
# already blank
# Compile using make or make -j n where n is the number of processors to use
make # or
make -j 4

# Install kernel modules
make modules_install

# Optimize and compile new kernel
cd /lib/modules/5.1.16/
find . -name *.ko -exec strip --strip-unneeded {} +
cd /usr/src/linux-5.1.16/
make install
Flush DNS MacOSx
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Openlens block update

Openlens github

sudo chmod -R 000 ~/Library/Application\ Support/Caches/open-lens-updater/pending
PromQL Expressions

List of operations and functions to your PromQL expressions.

Arithmetic binary operators:

\+ (addition)
\- (subtraction)
\* (multiplication)
/ (division)
% (modulo)
^ (power/exponentiation)

Comparison binary operators:

== (equal)
!= (not-equal)
\> (greater-than)
< (less-than)
\>= (greater-or-equal)
<= (less-or-equal)

Logical/set binary operators:

and (intersection)
or (union)
unless (complement)
Aggregation operators:

sum (calculate sum over dimensions)
min (select minimum over dimensions)
max (select maximum over dimensions)
avg (calculate the average over dimensions)
stddev (calculate population standard deviation over dimensions)
stdvar (calculate population standard variance over dimensions)
count (count number of elements in the vector)
count_values (count number of elements with the same value)
bottomk (smallest k elements by sample value)
topk (largest k elements by sample value)
quantile (calculate ?-quantile (0 ? ? ? 1) over dimensions)

Get the total memory in bytes:

node_memory_MemTotal_bytes

Get a sum of the total memory in bytes:

sum(node_memory_MemTotal_bytes)

Get a percentage of total memory used:

((sum(node_memory_MemTotal_bytes) - sum(node_memory_MemFree_bytes) - sum(node_memory_Buffers_bytes) - sum(node_memory_Cached_bytes)) / sum(node_memory_MemTotal_bytes)) * 100

Using a function with your query:

irate(node_cpu_seconds_total{job="node-exporter", mode="idle"}[5m])

Using an operation and a function with your query:

avg(irate(node_cpu_seconds_total{job="node-exporter", mode="idle"}[5m]))

Grouping your queries:

avg(irate(node_cpu_seconds_total{job="node-exporter", mode="idle"}[5m])) by (instance)
Sed Expressions
# Viewing a Range of Lines of a File
sed -n '5,10p' file.txt

# Viewing the Entire File Except a Given Range
sed '20,35d' file.txt

# Viewing Non-Consecutive Lines and Ranges
sed -n -e '5,7p' -e '10,13p' file.txt

# Replacing Words or Strings in a File
sed 's/version/story/g' file.txt
sed 's/version/story/gi' file.txt # ignore character case

# Replate multiple blank spaces with a single space
cat file.txt | sed 's/  */ /g'

# Replacing Words or Strings Inside a Range
sed '30,40 s/version/story/g' file.txt

# Remove Comments From a File
sed '/^#\|^$\| *#/d' file.txt

# Replace a Case-insensitive Word in a File
sed 's/[Zz]ip/rar/g' file.txt

# Finding a Specific Events in a Log File
sed -n '/^Jan  1/ p' /var/log/syslog

# Inserting Spaces or Blank Lines in a File
sed G file.txt
sed 'G;G' file.txt # 2 blank lines

# Removing ^M in a File
sed -i 's/\r//' file.txt

# Create a Backup File with Sed Command
sed -i'.orig' 's/this/there/gi' file.txt

# Switching Pairs of Words in a File
sed 's/^\(.*\),\(.*\)$/\2\, /g' names.txt
Vim Tips
# Supprimer toutes les lignes vides ou contenant tabulation et/ou espace
:%s/^[\ \t]*\n//g

# Suppression des lignes commençant par un #
:g/^#/d

# Suppression des ^M en milieu de ligne et substitution par un vrai retour à la ligne : Taper sur “Enter” pour obtenir le ^M.
:g/^M/s//^M/g

# Changer de mode DOS / UNIX
:se ff=dos
:se ff=unix

# You can show control characters (including where a line ends)
:set list
:set nolist (to toggle off)

# This replaces the beginning of each line with "//":
:%s!^!//!

# This replaces the beginning of each selected line (use visual mode to select) with "//":
:'<,'>s!^!//!

# Execute external command
:map ,t :!make<cr>