Skip to content

Instantly share code, notes, and snippets.

@florianpasteur
Last active May 22, 2024 09:29
Show Gist options
  • Save florianpasteur/24ecde447c95ba9f2a26aa6e4313fbbf to your computer and use it in GitHub Desktop.
Save florianpasteur/24ecde447c95ba9f2a26aa6e4313fbbf to your computer and use it in GitHub Desktop.
Bash profile
#!/bin/bash
# START=$(perl -MTime::HiRes=time -e 'printf "%.9f\n", time')
#######################################################
# EXPORTS
#######################################################
# Expand the history size
export HISTFILESIZE=10000
export HISTSIZE=500
# Don't put duplicate lines in the history and do not add lines that start with a space
export HISTCONTROL=erasedups:ignoredups:ignorespace
PROMPT_COMMAND='history -a'
# Allow ctrl-S for history navigation (with ctrl-R)
stty -ixon
# Ignore case on auto-completion
# Note: bind used instead of sticking these in .inputrc
if [[ $iatest > 0 ]]; then bind "set completion-ignore-case on"; fi
# Show auto-completion list automatically, without double tab
if [[ $iatest > 0 ]]; then bind "set show-all-if-ambiguous On"; fi
# Set the default editor
export EDITOR=vim
export VISUAL=vim
alias pico='edit'
alias spico='sedit'
alias nano='edit'
alias snano='sedit'
alias ws='webstorm'
# To have colors for ls and all grep commands such as grep, egrep and zgrep
export CLICOLOR=1
export LS_COLORS='no=00:fi=00:di=00;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:*.xml=00;31:'
#export GREP_OPTIONS='--color=auto' #deprecated
alias grep="/usr/bin/grep $GREP_OPTIONS"
unset GREP_OPTIONS
# Color for manpages in less makes manpages a little easier to read
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'
#######################################################
# MACHINE SPECIFIC ALIAS'S
#######################################################
# Alias's to change the directory
alias downloads='cd ~/Downloads'
alias desktop='cd ~/Desktop'
#######################################################
# GENERAL ALIAS'S
#######################################################
# To temporarily bypass an alias, we preceed the command with a \
# EG: the ls command is aliased, but to use the normal ls command you would type \ls
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Edit this .bashrc file
alias ebrc='edit ~/.bashrc'
# Show help for this .bashrc file
alias hlp='less ~/.bashrc_help'
# alias to show the date
alias da='date "+%Y-%m-%d %A %T %Z"'
# Alias's to modified commands
#alias cp='cp -i'
alias mv='mv -i'
#alias rm='rm -iv'
alias mkdir='mkdir -p'
alias ps='ps aux'
alias ping='ping -c 10'
alias less='less -R'
alias cls='clear'
alias apt-get='sudo apt-get'
alias multitail='multitail --no-repeat -c'
alias freshclam='sudo freshclam'
alias vi='vim'
alias svi='sudo vi'
alias vis='vim "+set si"'
# Change directory aliases
alias home='cd ~'
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ..4='cd ../../../..'
alias ..5='cd ../../../../..'
# cd into the old directory
alias bd='cd "$OLDPWD"'
# Remove a directory and all files
# alias rmd='/bin/rm --recursive --force --verbose '
# Alias's for multiple directory listing commands
alias l='ls' # show hidden files
alias la='ls -Alh' # show hidden files
alias ls='ls -h' # add colors and file type extensions
alias lx='ls -lXBh' # sort by extension
alias lk='ls -lSrh' # sort by size
alias lc='ls -lcrh' # sort by change time
alias lu='ls -lurh' # sort by access time
alias lr='ls -lRh' # recursive ls
alias lt='ls -ltrh' # sort by date
alias lm='ls -alh |more' # pipe through 'more'
alias lw='ls -xAh' # wide listing format
alias ll='ls -Fls' # long listing format
alias labc='ls -lap' #alphabetical sort
alias lf="ls -l | egrep -v '^d'" # files only
alias ldir="ls -l | egrep '^d'" # directories only
# alias chmod commands
alias mx='chmod a+x'
alias 000='chmod -R 000'
alias 644='chmod -R 644'
alias 666='chmod -R 666'
alias 755='chmod -R 755'
alias 777='chmod -R 777'
# Search command line history
alias h="history | grep "
# Search running processes
alias p="ps aux | grep "
alias topcpu="/bin/ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10"
# Search files in the current folder
alias f="find . | grep "
# Count all files (recursively) in the current folder
alias countfiles="for t in files links directories; do echo \`find . -type \${t:0:1} | wc -l\` \$t; done 2> /dev/null"
# To see if a command is aliased, a file, or a built-in command
alias checkcommand="type -t"
# Show current network connections to the server
alias ipview="netstat -anpl | grep :80 | awk {'print \$5'} | cut -d\":\" -f1 | sort | uniq -c | sort -n | sed -e 's/^ *//' -e 's/ *\$//'"
# Show open ports
alias openports='netstat -nape --inet'
# Alias's for safe and forced reboots
alias rebootsafe='sudo shutdown -r now'
alias rebootforce='sudo shutdown -r -n now'
# Alias's to show disk space and space used in a folder
alias diskspace="du -S | sort -n -r |more"
alias folders='du -h --max-depth=1'
alias folderssort='find . -maxdepth 1 -type d -print0 | xargs -0 du -sk | sort -rn'
alias tree='tree -CAhF --dirsfirst'
alias treed='tree -CAFd'
alias mountedinfo='df -hT'
# Alias's for archives
alias mktar='tar -cvf'
alias mkbz2='tar -cvjf'
alias mkgz='tar -cvzf'
alias untar='tar -xvf'
alias unbz2='tar -xvjf'
alias ungz='tar -xvzf'
# Show all logs in /var/log
alias logs="sudo find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]$' | xargs tail -f"
# SHA1
alias sha1='openssl sha1'
#######################################################
# Set the ultimate amazing command prompt
#######################################################
function __setprompt
{
local LAST_COMMAND=$? # Must come first!
# Define colors
local LIGHTGRAY="\033[0;37m"
local WHITE="\033[1;37m"
local BLACK="\033[0;30m"
local DARKGRAY="\033[1;30m"
local RED="\033[0;31m"
local LIGHTRED="\033[1;31m"
local GREEN="\033[0;32m"
local LIGHTGREEN="\033[1;32m"
local BROWN="\033[0;33m"
local YELLOW="\033[1;33m"
local BLUE="\033[0;34m"
local LIGHTBLUE="\033[1;34m"
local MAGENTA="\033[0;35m"
local LIGHTMAGENTA="\033[1;35m"
local CYAN="\033[0;36m"
local LIGHTCYAN="\033[1;36m"
local NOCOLOR="\033[0m"
# Show error exit code if there is one
if [[ $LAST_COMMAND != 0 ]]; then
# PS1="\[${RED}\](\[${LIGHTRED}\]ERROR\[${RED}\])-(\[${LIGHTRED}\]Exit Code \[${WHITE}\]${LAST_COMMAND}\[${RED}\])-(\[${LIGHTRED}\]"
PS1="\[${DARKGRAY}\](\[${LIGHTRED}\]ERROR\[${DARKGRAY}\])-(\[${RED}\]Exit Code \[${LIGHTRED}\]${LAST_COMMAND}\[${DARKGRAY}\])-(\[${RED}\]"
if [[ $LAST_COMMAND == 1 ]]; then
PS1+="General error"
elif [ $LAST_COMMAND == 2 ]; then
PS1+="Missing keyword, command, or permission problem"
elif [ $LAST_COMMAND == 126 ]; then
PS1+="Permission problem or command is not an executable"
elif [ $LAST_COMMAND == 127 ]; then
PS1+="Command not found"
elif [ $LAST_COMMAND == 128 ]; then
PS1+="Invalid argument to exit"
elif [ $LAST_COMMAND == 129 ]; then
PS1+="Fatal error signal 1"
elif [ $LAST_COMMAND == 130 ]; then
PS1+="Script terminated by Control-C"
elif [ $LAST_COMMAND == 131 ]; then
PS1+="Fatal error signal 3"
elif [ $LAST_COMMAND == 132 ]; then
PS1+="Fatal error signal 4"
elif [ $LAST_COMMAND == 133 ]; then
PS1+="Fatal error signal 5"
elif [ $LAST_COMMAND == 134 ]; then
PS1+="Fatal error signal 6"
elif [ $LAST_COMMAND == 135 ]; then
PS1+="Fatal error signal 7"
elif [ $LAST_COMMAND == 136 ]; then
PS1+="Fatal error signal 8"
elif [ $LAST_COMMAND == 137 ]; then
PS1+="Fatal error signal 9"
elif [ $LAST_COMMAND -gt 255 ]; then
PS1+="Exit status out of range"
else
PS1+="Unknown error code"
fi
PS1+="\[${DARKGRAY}\])\[${NOCOLOR}\]\n"
else
PS1=""
fi
# Date
PS1+="\[${DARKGRAY}\](\[${CYAN}\]$(date +%a) $(date +%b-'%-m')" # Date
PS1+="${BLUE} $(date +'%-I':%M:%S%P)\[${DARKGRAY}\])-" # Time
if ! command -v md5 &> /dev/null;
then
alias md5="md5sum";
fi;
SERVER_HASH=`hostname | md5 | awk '{ print $1 }'`
SERVER_HASH_DEC=$(( 16#$SERVER_HASH ))
case $(( $SERVER_HASH_DEC % 5 )) in
1)
USER_COLOR=${MAGENTA}
;;
2)
USER_COLOR=${LIGHTMAGENTA}
;;
3)
USER_COLOR=${CYAN}
;;
4)
USER_COLOR=${LIGHTCYAN}
;;
*)
USER_COLOR=${YELLOW}
;;
esac
# User and server
local SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'`
local SSH2_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'`
if [ $SSH2_IP ] || [ $SSH_IP ] ; then
PS1+="(\[${USER_COLOR}\]\u@\h"
else
PS1+="(\[${USER_COLOR}\]\u"
fi
# Current directory
PS1+="\[${DARKGRAY}\]:\[${BROWN}\]\w\[${DARKGRAY}\])-"
# Skip to the next line
PS1+="\n"
if [[ $EUID -ne 0 ]]; then
PS1+="\[${GREEN}\]>\[${NOCOLOR}\] " # Normal user
else
PS1+="\[${RED}\]>\[${NOCOLOR}\] " # Root user
fi
# PS2 is used to continue a command using the \ character
PS2="\[${DARKGRAY}\]>\[${NOCOLOR}\] "
# PS3 is used to enter a number choice in a script
PS3='Please enter a number from above list: '
# PS4 is used for tracing a script in debug mode
PS4='\[${DARKGRAY}\]+\[${NOCOLOR}\] '
}
PROMPT_COMMAND='__setprompt'
function __screenshot_prompt() {
local LIGHTGRAY="\033[0;37m"
local WHITE="\033[1;37m"
local BLACK="\033[0;30m"
local DARKGRAY="\033[1;30m"
local RED="\033[0;31m"
local LIGHTRED="\033[1;31m"
local GREEN="\033[0;32m"
local LIGHTGREEN="\033[1;32m"
local BROWN="\033[0;33m"
local YELLOW="\033[1;33m"
local BLUE="\033[0;34m"
local LIGHTBLUE="\033[1;34m"
local MAGENTA="\033[0;35m"
local LIGHTMAGENTA="\033[1;35m"
local CYAN="\033[0;36m"
local LIGHTCYAN="\033[1;36m"
local NOCOLOR="\033[0m"
PS1="(\[${USER_COLOR}\]\u\[${DARKGRAY}\]:\[${BROWN}\]\W\[${DARKGRAY}\])\n\[${GREEN}\]>\[${NOCOLOR}\] "
}
set-screenshot-prompt() {
PROMPT_COMMAND='__screenshot_prompt'
}
get-bashrc-cmd() {
echo "get-bashrc() {
mkdir -p ~/.gists;
cd ~/.gists;
git clone git@gist.github.com:24ecde447c95ba9f2a26aa6e4313fbbf.git || https://gist.github.com/florianpasteur/24ecde447c95ba9f2a26aa6e4313fbbf.git
}"
}
update-bashrc() {
HERE=$(pwd)
BASHRC_LOCATION=$(readlink $HOME/.bashrc | xargs dirname)
cd "$HOME/$BASHRC_LOCATION"
git fetch --all && git pull --rebase
source $HOME/.bashrc
cd "$HERE"
}
get-vimrc() {
HERE=$(pwd)
BASHRC_LOCATION=$(readlink ~/.bashrc | xargs dirname)
cd "$BASHRC_LOCATION"/..
git clone git@gist.github.com:6b39bc46c5c27254d1c18b024f3b7f38.git || git clone https://gist.github.com/florianpasteur/6b39bc46c5c27254d1c18b024f3b7f38.git
cd 6b39bc46c5c27254d1c18b024f3b7f38
VIMRC_LOCATION=$(pwd)
cd ~
ln -s "$VIMRC_LOCATION"/.vimrc
cd "$HERE"
}
get-vim-plug() {
curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
}
update-vimrc() {
HERE=$(pwd)
VIMRC_LOCATION=$(readlink ~/.vimrc | xargs dirname)
cd "$VIMRC_LOCATION"
git fetch --all && git pull --rebase
cd "$HERE"
}
# Docker shortcuts
dopen() { docker exec -ti "$1" bash; }
dexec() { docker exec -ti "$2" $1; }
dfind () { docker ps | tail -n +2 | grep "$1" | cut -d " " -f1; }
dafind () { docker ps -a | tail -n +2 | grep "$1" | cut -d " " -f1; }
dls () { docker ps | tail -n +2 | grep "$1"; }
dals () { docker ps -a | tail -n +2 | grep "$1"; }
drm-all() { docker ps -a | tail -n +2 | grep "$1" | cut -d " " -f1 | xargs docker rm; }
# Java shortcuts
if [ -e /usr/libexec/java_home ];
then
alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"
fi
port-usage() {
netstat -vanp tcp | grep -E "pid|$1"
}
#######################################################
# GIT
#######################################################
git-user() {
git config user.name $1
git config user.email $2
}
git-user-public() {
git config user.email "florian.c.p@pm.me"
git config user.name "Florian PASTEUR"
}
git-preferences() {
git config $@ core.autocrlf false
git config $@ core.longpaths true
git config $@ init.defaultBranch main
}
git-ignore-standard() {
echo ".idea" >> .gitignore
echo "node_modules" >> .gitignore
echo "!.gitkeep" >> .gitignore
echo "screenlog.*" >> .gitignore
for ignored_file in "$@"; do
echo ${ignored_file} >> .gitignore
done
}
git-init-commit() {
git add .
git commit -a -m "Initial Commit"
}
git-download() {
git archive --remote=$1 --format=tar ${2:-HEAD} | tar xf -
}
git-hook-install-json-file-bump-version() {
git-hook-install https://gist.githubusercontent.com/florianpasteur/f1cc272815f08d133ead0ddd2c21e421/raw/6d90357e65b46fd76e66a48179d69d63ecf20709/post-commit post-commit
}
git-hook-install-npm-bump-version() {
git-hook-install https://gist.githubusercontent.com/florianpasteur/c9764ddc56042a075662e1adcfbcc0b4/raw/01dffa82ee800762792a896a795eca25937cf888/post-commit.sh post-commit
}
git-hook-install() {
curl "$1" --output .git/hooks/"$2" && chmod +x .git/hooks/"$2"
}
git-init-work-project() {
git init && \
git-user-work && \
git-ignore-standard && \
git-init-commit && \
idea .
}
git-init-public-project() {
set -e
git init && \
git-user-public && \
git-ignore-standard && \
git-init-commit && \
gh repo create --private -s .
git push -u origin main
idea .
}
git-commit-last-command() {
history | tail -n1 | cut -c 8- > /tmp/last_command
git add .
git commit -F /tmp/last_command
}
git-reset-hard() {
git reset HEAD --hard
}
#######################################################
# UTILS
#######################################################
slack-post-message() {
CHANNEL=$1;
MESSAGE=$2;
TOKEN=$3
SLACK_TOKEN_FILE=~/.slack-token
if test -f "${SLACK_TOKEN_FILE}"; then
TOKEN=$(cat "${SLACK_TOKEN_FILE}")
fi
curl -X POST \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \
-d '{"channel":"'${CHANNEL}'","text":"'"${MESSAGE}"'"}' \
https://slack.com/api/chat.postMessage
}
two-digits() {
TMP=0$1
echo ${TMP: -2} # space is important
}
reindex-files() {
start=$1
stop=$2
shift 2;
for i in $(seq $start $stop);
do
next=$(two-digits $(($i+1)))
prev=$(two-digits $i)
rename 's/V'$prev'/V'$next'/' ""$*;
done
}
rename_sub_files() {
find *V$1* -type d -exec rename 's/V\d{2}/V'${1}'/g' "{}" \;
find *V$1* -type f -exec rename 's/V\d{2}/V'${1}'/g' "{}" \;
}
copy-file-list() {
ls -n1 $1 | pbcopy;
}
format-json() {
for file in "$@";
do
output_file="${file%.*}.formatted.json"
jq '.' $file > $output_file
done
}
# https://oozou.com/til/add-a-macos-style-drop-shadow-to-your-partial-screen-captures-or-plain-bordered-images-153
# https://imagemagick.org/script/download.php#macosx
# brew install imagemagick
shadow() {
for file in "$@"; do
convert "$file" \( +clone -background black -shadow 80x20+0+15 \) +swap -background transparent -layers merge +repage "$file";
done
}
loadNvm() {
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && source "$NVM_DIR/bash_completion" # This loads nvm bash_completion
}
720p() {
for input_file in "$@"; do
output_file="${input_file%.*}.720p.mp4"
ffmpeg -loglevel quiet -y -i "$input_file" -vf scale=1280:720 -c:a copy -progress pipe:1 "$output_file" > /dev/null
echo "Done processing $input_file"
done
}
webpToPng() {
for input_file in "$@"; do
output_file="${input_file%.*}.png"
sips -s format png "$input_file" --out "$output_file"
done
}
# brew install exiftool
deleteMetadata () {
for input_file in "$@"; do
exiftool -all= "$input_file"
done
}
# Upload ssh key
upload_ssh_key() {
local host=$1
local ssh_key_path=~/.ssh/id_rsa.pub
if [[ ! -f "$ssh_key_path" ]]; then
echo "SSH key file not found: $ssh_key_path"
return 1
fi
cat "$ssh_key_path" | ssh "$host" 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
if [[ $? -eq 0 ]]; then
echo "SSH key successfully uploaded to $host"
else
echo "Failed to upload SSH key to $host"
fi
}
# Video to Fig
video_to_gif() {
if [ -z "$1" ]; then
echo "Usage: $0 input_video_file"
exit 1
fi
# Get the input file name and its base name
input_file="$1"
base_name="${input_file%.*}"
# Set the output file name
output_file="${base_name}.gif"
# Convert the video to GIF
ffmpeg -i "$input_file" -vf "fps=10" -c:v gif "$output_file"
}
PATH=$HOME/.nvm/current/bin:$PATH
PATH=$PATH:/Library/Java/apache-maven-3.9.4/bin/
PATH=$PATH:/usr/local/bin/
split_video() {
input_file="$1"
output_prefix="$2"
if [ -z "$input_file" ] || [ -z "$output_prefix" ]; then
echo "Usage: split_video <input_file> <output_prefix>"
return 1
fi
ffmpeg -i "$input_file" -c copy -map 0 -segment_time 30 -f segment "${output_prefix}_%03d.mp4"
}
# END=$(perl -MTime::HiRes=time -e 'printf "%.9f\n", time')
# echo "Loaded in " $(node -e "console.log(process.argv[2] - process.argv[1])" $START $END) "s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment