Created
January 26, 2021 16:07
-
-
Save PaulEmmanuelSotir/cecc2d5064ba3f5942d85dbb47737e29 to your computer and use it in GitHub Desktop.
My everyday .bash_aliases.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Begining of custom MOTD | |
#echo -e "\e[32m\e[1m###################################> ☣❤ \e[33m\e[1mPES SHELL\e[39m\e[0m - INTRUDERS BE CAREFULL\e[32m\e[1m ❤☣ <###################################\e[39m\e[0m" | |
echo -e "\e[33m\e[1m################################><><><><><><><><><><><><><><><><><><><><><><><><################################\e[39m\e[0m" | |
echo -e "\e[32m ___ ________ ______ ________ __ " | |
echo -e " / _ \/ __/ __/ / __/ // / __/ / / / " | |
echo -e " / ___/ _/_\ \ _\ \/ _ / _// /__/ /__" | |
echo -e " /_/ /___/___/ /___/_//_/___/____/____/\e[39m\e[1m ☣❤(INTRUDERS BE CAREFULL)☣❤" | |
echo -e "\e[33m\e[1m################################><><><><><><><><><><><><><><><><><><><><><><><><################################\e[39m\e[0m" | |
IS_CUDA_SETUP=$SETUP_CUDA # Env. variable set by linux install script (whether if Nvidia driver / CUDA Toolkit have been installed) | |
GCC_8_PRIORITY=20 | |
GCC_9_PRIORITY=10 | |
export GIT_EDITOR=vim | |
######## Custom bash aliases ######## | |
alias now='date +"%T"' | |
alias ports='netstat -tulanp' | |
alias h="history" | |
alias grep_hist="history | grep" | |
alias greph="grep_hist" | |
alias git_log="git log --graph --oneline --decorate --all" | |
alias status="echo -e '\e[32m\e[1m#### GIT ALIASES #####\e[39m\e[0m\n- \e[33mgit_log\e[39m: pretty print graph of git log\n- \e[33mstatus\e[39m: this command\n- \e[33mmerge_dev\e[39m: ckeckout to master branch, merges dev into master, pushes all branches to remote and checkout back to dev branch\n\n\e[1m\e[4m\e[32m#### GIT BRANCH ####\e[0m\e[39m';git branch; echo -e '\n\e[1m\e[4m\e[32m#### GIT STATUS ####\e[0m\e[39m'; git status; echo -e '\n\e[1m\e[4m\e[32m#### GIT LOG ####\e[0m\e[39m'; git_log -n 10" | |
alias commit_all="git add --all; git commit -m" | |
alias merge_dev="set -euo pipefail; git checkout master; git merge dev; git push --all; git checkout dev" | |
alias git_info="status; echo -e '\e[32m\e[1m#### GIT GC #####\e[39m\e[0m\n'; git gc; echo -e '\e[32m\e[1m#### GIT OBJECT COUNT (size-pack: Packfiles size in KB) #####\e[39m\e[0m\n'; git count-objects -v" | |
alias ca="conda activate" | |
alias chkconfig="ls -alh /etc/rc?.d/* | grep " | |
alias smi="watch -cbd -n 1 -x 'nvidia-smi'" | |
alias stop_gui='gnome-session-quit' | |
alias exit_gui='stop_gui' | |
# URL Percent encoding/decoding | |
alias urldecode='python -c "import sys, urllib as ul; print(ul.unquote_plus(sys.argv[1]))"' | |
alias urlencode='python -c "import sys, urllib as ul; print(ul.quote_plus(sys.argv[1]))"' | |
# Rsyncc: Progress with checksum, recursive, relative paths, preserve permission, exucatibility, ACLs, Xattr, groups, modification times, handle sparse files efficiently, compress transfered data, prefer IPv4 + file-transfert stats + preallocate files, + store/recover privileged attrs using xattrs | |
RSYNC_BACKUP_DIR=\$HOME/.rsync_backups | |
RSYNC_TMP_DIR=\$HOME/.rsync_tmp | |
RSYNC_OPTS=" --progress -crRpEAXgtSzDo -4 --stats --preallocate --fake-super --temp-dir=\\\$RSYNC_TMP_DIR" | |
alias rsyncc="rsync \$RSYNC_OPTS -l" # Copy symlinks as is | |
alias rsync_backup="rsyncc --backup --backup-dir=\\\$RSYNC_BACKUP_DIR --sufix='.old'" | |
alias rsynccb="rsync_backup" | |
# Same as rsyncc but "unsafe" symlinks are transformed (J: symlinks modification time modified) | |
alias rsyncc_safelinks="rsyncc \$RSYNC_OPTS -J --copy-unsafe-links" | |
# TODO: replace sshconfig with an ssh alias? | |
# alias ssh='ssh -a4CX -o "TCPKeepAlive=" -o "ServerAliveInterval" -o "ServerAliveCountMax" -o "ForwardX11Timeout" -o "FingerprintHash" -o "ConnectTimeout" -o "ConnectionAttempts" -o "CheckHostIP" -o "BatchMode" -o "AddKeysToAgent" | |
# If color support then, add color support to some commands | |
if [ -x /usr/bin/dircolors ]; then | |
test -r \$HOME/.dircolors && eval "\$(dircolors -b \$HOME/.dircolors)" || eval "\$(dircolors -b)" | |
alias dir="dir --color=auto" | |
alias grep="grep --color=auto" | |
alias ls="ls --color=auto" | |
alias diff="colordiff" | |
fi | |
# Custom sudo/root aliases | |
if [ \$UID -ne 0 ]; then | |
alias allow_out="sudo ufw default allow outgoing" | |
alias deny_out="sudo ufw default deny outgoing" | |
alias shutoff="sudo shutdown -h now" | |
alias update='sudo apt-get update && sudo apt-get upgrade -y' | |
alias default_gui='sudo systemctl set-default graphical' | |
alias default_cmd='sudo systemctl set-default multi-user' | |
alias start_gui='sudo systemctl start gdm3' | |
else | |
alias allow_out="ufw default allow outgoing" | |
alias deny_out="ufw default deny outgoing" | |
alias shutoff="shutdown -h now" | |
alias update='apt-get update && apt-get upgrade -y' | |
alias default_gui='systemctl set-default graphical' | |
alias default_cmd='systemctl set-default multi-user' | |
alias start_gui='systemctl start gdm3' | |
fi | |
######## Custom functions ######## | |
echo -e "\n\e[32m-------------------------------------------> Custom helper functions <----------------------------------------\e[39m\e[0m" | |
# Simplified ssh-add commands (usage: 'ssha key_rsa' is equivalent to 'ssh-add \$HOME/.ssh/key_rsa') | |
agent() { | |
if [ -z "\$(ssh-add -L | grep \$1)" ] ; then | |
cd \$HOME/.ssh && ssh-add "\$1" | |
cd - > /dev/null | |
fi | |
} | |
export -f agent | |
echo -e "- \e[33magent <ssh_key>\e[39m : performs 'ssh-add' within '\$HOME/.ssh' directory" | |
sudo_if_not_root() { | |
if [ \$UID -ne 0 ]; then | |
eval sudo "\$@" | |
else | |
eval "\$@" | |
fi | |
} | |
export -f sudo_if_not_root | |
echo -e "- \e[33msudo_if_not_root *<command>\e[39m : runs following command with sudo if current user isn't already root (runs without sudo otherwise)" | |
ask() { | |
ANSWER="" | |
while [[ "\$ANSWER" != "Y" && "\$ANSWER" != "N" ]]; do | |
echo "\$1 (Y/N)" | |
read ANSWER | |
done | |
if [[ "\$ANSWER" == "Y" ]]; then | |
eval "\$2" | |
elif [[ "\$#" -ge 3 ]]; then | |
eval "\$3" | |
fi | |
} | |
export -f ask | |
echo -e "- \e[33mask <prompt> <cmd_if_yes> <?cmd_if_no>\e[39m : prompts user for a Y/N question. Runs first command if user choose 'Y' and runs another command if user choose to anwser 'N' (optional)." | |
# SSH utility function: agent + open ssh connection | |
ssha() { | |
agent "\$1" | |
ssh \${@:2} | |
} | |
export -f ssha | |
echo -e "- \e[33mssha <ssh_key> *<ssh_args>\e[39m : Adds 'ssh_key' to ssh agent with 'agent' utility function and ssh to given remote server with following arguments." | |
cenv() { | |
CMD="conda env \$1 --file \${@:2}" | |
echo \$CMD | |
eval \$CMD | |
} | |
export -f cenv | |
echo -e "- \e[33mcenv <action> <env_file> *<other_args>\e[39m : Shorthand for 'conda env <action> --file <env_file> *<other_args>'. E.g.: 'cenv update ./environment.yml'" | |
# Sends SMS to my phone (Free mobile SMS API) | |
freesms() { | |
usage() { echo "Usage: freesms [-m <string>] [-e] [-np] [-ns]" 1>&2; exit 1; } | |
NOSUFFIX=false | |
NOPREFIX=false | |
EVAL_MESS=false | |
while getopts ":m:ns:np:e:" o; do | |
case "\${o}" in | |
ns) | |
NOSUFFIX=true | |
;; | |
np) | |
NOPREFIX=true | |
;; | |
m) | |
MESSAGE="\${OPTARG}" | |
#TODO: if validation on argument is needed: ((s == 45 || s == 90)) || usage | |
;; | |
e) | |
EVAL_MESS=true | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift \$((OPTIND-1)) | |
if [ -z "\$MESSAGE" ]; then | |
echo -e "ERROR: Missing required argument '-m' with SMS content.\n" | |
usage | |
fi | |
# TODO: encrypt credentials and ask for its password, or, if root, use root-protected password file | |
USER="..." #TODO: put your Free Mobile ID here if you have one | |
PASS="..." #TODO: put your Free Mobile API key here if you have one | |
if [[ "\$EVAL_MESS" ]]; then | |
MESSAGE=\$(echo -e \$MESSAGE) | |
fi | |
if [[ ! \${NOPREFFIX} ]]; then | |
PREFIX="-%20USER=\$USER%0D%0A-%20TIMESTAMP=\$(now)%0D%0A" | |
MESSAGE="\$PREFIX\$MESSAGE" | |
fi | |
if [[ ! \${NOSUFFIX} ]]; then | |
SUFFIX="%0D%0A[Sent from bash throught Free SMS Web API]" | |
MESSAGE="\$MESSAGE\$SUFFIX" | |
fi | |
# Perform Free SMS Web API call | |
echo -e "Sending SMS throught Free SMS web API...\nSMS content: '\$MESSAGE'" | |
wget -q --prefer-family=IPv4 --no-cache --no-cookies --https-only -O- "https://smsapi.free-mobile.fr/sendmsg?user=\$USER&pass=\$PASS&msg=\$MESSAGE" | |
} | |
export -f freesms | |
echo -e "- \e[33mfreesms [-m <string>] [-np] [-ns]\e[39m : Send an SMS throught Free SMS Web API with '-m' arg string as content. Prefix and suffix can be disabled using '-np' or '-ns'. '-e' may be used to evaluate message (eq. to 'echo -e')" | |
# SSL encoding/decoding helper functions | |
echo -e "- \e[33msslenc/ssldec\e[39m : SSL encoding/decoding helper functions. Examples: 'sslenc message.txt', 'ssldec message.txt.enc', 'ssldec message.txt.enc > message.txt'" | |
sslenc() { | |
echo -e "Please type password for SSL encoding of '\$1' file..." | |
openssl enc -in "\$1" -aes-256-cbc -pass stdin > "\$1".enc | |
} | |
export -f sslenc | |
ssldec() { | |
echo -e "Please type password for SSL decoding of '\$1' file..." | |
echo \$(openssl enc -in "\$1" -d -aes-256-cbc -pass stdin) | |
} | |
export -f ssldec | |
remove_alternatives() { | |
if [[ -n "\$(update-alternatives --display \$1 | grep -io -E "error:\s+no\s+alternatives")" ]]; then | |
sudo_if_not_root update-alternatives --remove-all "\$1" | |
fi | |
} | |
export -f remove_alternatives | |
echo -e "- \e[33mremove_alternatives <group>\e[39m : Removes all alternatives of given group if it exists (deletes given alternative group). (uses 'sudo_if_not_root')" | |
# Additional aliases/scripting which are machine-specific (the current file is common to all my workstations, i.e. user-accross-machines-specific :-S ) | |
ADDITIONAL_ALIASES="\$HOME/.bash_aliases_additional" | |
if [ -f \$ADDITIONAL_ALIASES ]; then | |
. \$ADDITIONAL_ALIASES | |
export ADDITIONAL_ALIASES=\$ADDITIONAL_ALIASES | |
fi | |
######## Scripting ######## | |
# Clear systemd journals older than 10 days if user says so | |
#ask "> Clean systemd journals entries older than 10 days?" "sudo_if_not_root journalctl --vacuum-time=10d" | |
# Makes sure there is a backup and tmp directory for rsync (NOTE: -d condition and mkdir -p option are redundant) | |
[[ -d \$RSYNC_BACKUP_DIR ]] || mkdir -p \$RSYNC_BACKUP_DIR | |
[[ -d \$RSYNC_TMP_DIR ]] || mkdir -p \$RSYNC_TMP_DIR | |
# Setup GCC/G++ 8-9 versions alternatives (update-alternative) | |
update_gcc_versions() { | |
_GCC_8_PRIORITY=\$1 | |
_GCC_9_PRIORITY=\$2 | |
echo -e "> Setup GCC/G++ versions alternatives using 'update-alternatives' (GCC_9_PRIORITY=\$_GCC_9_PRIORITY ; GCC_8_PRIORITY=\$_GCC_8_PRIORITY)" | |
remove_alternatives gcc | |
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 \$_GCC_8_PRIORITY | |
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 \$_GCC_9_PRIORITY | |
remove_alternatives gcc-nm | |
sudo update-alternatives --install /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-8 \$_GCC_8_PRIORITY | |
sudo update-alternatives --install /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-9 \$_GCC_9_PRIORITY | |
remove_alternatives gcc-ar | |
sudo update-alternatives --install /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-8 \$_GCC_8_PRIORITY | |
sudo update-alternatives --install /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-9 \$_GCC_9_PRIORITY | |
remove_alternatives gcc-ranlib | |
sudo update-alternatives --install /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-8 \$_GCC_8_PRIORITY | |
sudo update-alternatives --install /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-9 \$_GCC_9_PRIORITY | |
remove_alternatives gcov-tool | |
sudo update-alternatives --install /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-8 \$_GCC_8_PRIORITY | |
sudo update-alternatives --install /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-9 \$_GCC_9_PRIORITY | |
remove_alternatives gcov-dump | |
sudo update-alternatives --install /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-8 \$_GCC_8_PRIORITY | |
sudo update-alternatives --install /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-9 \$_GCC_9_PRIORITY | |
remove_alternatives gcov | |
sudo update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-8 \$_GCC_8_PRIORITY | |
sudo update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-9 \$_GCC_9_PRIORITY | |
remove_alternatives g++ | |
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 \$_GCC_8_PRIORITY | |
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 \$_GCC_9_PRIORITY | |
# Set update-alternative GCC 'aliases' | |
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30 | |
sudo update-alternatives --set cc /usr/bin/gcc | |
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30 | |
sudo update-alternatives --set c++ /usr/bin/g++ | |
} | |
#ask "> Update GCC/G++ version alternatives?" "update_gcc_versions \$GCC_8_PRIORITY \$GCC_9_PRIORITY" | |
#if [[ \$IS_CUDA_SETUP ]]; then | |
# echo -e "> In order to change your default gcc/g++ default version manually, see 'update_gcc_versions()' helper function defined in '~/.bash_aliases'" | |
# echo -e "\t(Example usage: 'update_gcc_versions 20 10' to set gcc/g++ binaries ver. 8 and 9 priorities to 20 and 10 respectively, which makes gcc/g++ 8 the default compiler)" | |
#fi | |
export -f update_gcc_versions | |
# End of custom MOTD (List all available aliases) | |
echo -e "\n\e[32m-------------------------------------------> Custom bash aliases <--------------------------------------------\e[39m" | |
# Print Alias names | |
echo -e "COMMANDS: \e[33m\$(alias | sed -n "s/^alias\s\([^=]*\)=[^$]*$/\1/p" | awk 'BEGIN { ORS = " \033[39m-\033[33m " } {print \$1}')\e[39m" | |
echo -e "\e[32m\e[1m##############################################################################################################\e[39m\e[0m\a" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment