Autocomplete ssh

# /etc/profile.d/complete-hosts.sh
# Autocomplete Hostnames for SSH etc.
# by Jean-Sebastien Morisset (http://surniaulula.com/)
_complete_hosts () {
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    host_list=`{ 
        for c in /etc/ssh_config /etc/ssh/ssh_config ~/.ssh/config
        do [ -r $c ] && sed -n -e 's/^Host[[:space:]]//p' -e 's/^[[:space:]]*HostName[[:space:]]//p' $c
        done
        for k in /etc/ssh_known_hosts /etc/ssh/ssh_known_hosts ~/.ssh/known_hosts
        do [ -r $k ] && egrep -v '^[#\[]' $k|cut -f 1 -d ' '|sed -e 's/[,:].*//g'
        done
        sed -n -e 's/^[0-9][0-9\.]*//p' /etc/hosts; }|tr ' ' '\n'|grep -v '*'`
    COMPREPLY=( $(compgen -W "${host_list}" -- $cur))
    return 0
}
complete -F _complete_hosts ssh
complete -F _complete_hosts host

Source

Autoload ssh key

Note

Private key need to be in ~/.ssh/id_rsa. Place this code in your .bashrc

#autoload ssh agent
env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
    ssh-add
fi

unset env

Github

Auto set user for ssh connections

Edit or add .ssh/config file, ie nano /home/Emilien/.ssh/config :

Host serveur1
  User Emilien_Raffaelli
Host *.prod-?? #with wildcard * for letter, ?? for number
  User Emilien_Raffaelli
Host 127.0.0.1 #or with an ip
  User Emilien_Raffaelli
IdentityFile ~/.ssh/id_rsa #auto add your private key

Avoid ssh timeout

Edit or add .ssh/config file, ie nano /home/Emilien/.ssh/config :

Host *
  ServerAliveInterval 240

Source

Linux treesize command

#/bin/sh
du -k --max-depth=1 | sort -nr | awk '
     BEGIN {
        split("KB,MB,GB,TB", Units, ",");
     }
     {
        u = 1;
        while ($1 >= 1024) {
           $1 = $1 / 1024;
           u += 1
        }
        $1 = sprintf("%.1f %s", $1, Units[u]);
        print $0;
     }
    '

http://blog.aclarke.eu/2011/09/21/a-simple-treesize-shell-script-for-linux/

Or use ncdu x)

Show progress on archiving and extracting

You need to install pipe viewer

Archiving

https://www.cambus.net/visualizing-progression-of-file-operations-using-pv-pipe-viewer/

Extracting

pv file.tgz | tar xzf - -C target_directory

Source of the example : https://www.mexchip.com/en/2010/10/how-to-show-a-progress-bar-when-extracting-a-file/
https://superuser.com/a/665181

Monitor folder and do something when file change

#!/bin/sh
dir1=/path/to/A/
while inotifywait -qqre modify "$dir1"; do
    /run/backup/to/B 
done

Source

How to fix a “E: The method driver /usr/lib/apt/methods/http could not be found.” or “Could not find apt-transport-https” error

ln -s /usr/lib/apt/methods/http /usr/lib/apt/methods/https

apt-get update

apt-get install apt-transport-https

Trashbin like

Create a Trash folder where you want (ex: ~/.Trash)
Open ~/.bashrc and put this in :

alias rm='mv --target-directory ~/.Trash'

Logout/login to take effect

Supervisor

https://gist.github.com/mozillazg/6cbdcccbf46fe96a4edd
https://github.com/Supervisor/initscripts
http://supervisord.org/installing.html

Cron

Crontab generator
Crontab translator
Chmod calculator

Prepend foldername to file

#!/bin/bash

for i in */;do
    cd "$i" || return;
    for j in *.srt;do
        newfilename="${i::-1}_${j}";
        rename -v "s/$j/$newfilename/" "$j";
    done;
    cd ../;
done