274 lines
5.9 KiB
Plaintext

_sod () {
if [ -d "$1" ] && [ -n "$(ls "$1"/ 2>/dev/null)" ]; then
for FILENAME in $(ls "$1"/ | sort -n)
do
source "$1"/"$FILENAME"
done
fi
}
lt () {
local DIR
test -z "$1" && DIR="." || DIR="$1"
du -h "$DIR" 2>/dev/null | sort -h | tac
}
destroy() {
test -n "$1" || return 1
test -e "$1" || return 1
echo -n "Sure want to destroy file '$1' with size $(du -hs "$1" | awk '{printf $1}') [y/N] "
read -r response
[[ $response == [yY] ]] && shred -zun 25 "$1"
}
lsl () {
local DIR
DIR="$1"
if [ -z "$1" ]; then
DIR="$(pwd)"
fi
ls -lhFA --color=always "${DIR}" | tail -n +2 | less -R
}
workon() {
local ENV_NAME
ENV_NAME="$1"
if [ -z "$1" ]; then
ENV_NAME=$(basename "$(pwd)")
fi
source /opt/pythonenv/"$ENV_NAME"/bin/activate
}
cl() {
local DIR
DIR="$*"
if [ $# -lt 1 ]; then
DIR="$HOME"
fi
cd "${DIR}" && ls -F --color=auto
}
py() {
if [[ -z "$@" && -x "$(command -v ipython 2>/dev/null)" ]]; then
ipython -i -c "q = exit"
else
python3 $@
fi
}
ve() {
local venv_name
if [ -n "$1" ]; then
venv_name="$1"
else
venv_name="venv"
fi
python3 -m virtualenv "$venv_name" && . "$venv_name/bin/activate"
}
va() {
local activate_venv
if [ -f "./venv/bin/activate" ]; then
source "./venv/bin/activate"
return 0
fi
activate_venv="$(find -P . -maxdepth 3 -type f -wholename '*/bin/activate' | sort | head -n 1)"
if [ -n "$activate_venv" ]; then
source "$activate_venv"
return 0
else
echo "va: error: virtual environment not found, use python3 -m virtualenv venv" >&2
return 1
fi
}
extract () {
if [ -z "$1" ]; then # if string non-zero
# display usage if no parameters given
echo "extract: error: usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
return 2
fi
if ! [ -f "$1" ]; then # if file not exist
echo "extract: error: '$1' file does not exist" >&2
return 1
fi
local NAME
NAME=${1%%.*} # removes extension from filename
if [ -e "$NAME" ]; then
echo "extract: error: '$NAME' exists" >&2
return 1
fi
mkdir "$NAME" && cd "$NAME" || return 1
case $1 in
*.tar.bz2) tar xjf ../"$1" ;;
*.tar.gz) tar xzf ../"$1" ;;
*.tar.xz) tar xJf ../"$1" ;;
*.lzma) unlzma ../"$1" ;;
*.bz2) bunzip2 ../"$1" ;;
*.rar) unrar x -ad ../"$1" ;;
*.gz) gunzip ../"$1" ;;
*.tar) tar xf ../"$1" ;;
*.tbz2) tar xjf ../"$1" ;;
*.tgz) tar xzf ../"$1" ;;
*.zip) unzip ../"$1" ;;
*.Z) uncompress ../"$1" ;;
*.7z) 7z x ../"$1" ;;
*.xz) unxz ../"$1" ;;
*.exe) cabextract ../"$1" ;;
*) echo "extract: error: '$1' - unknown archive method" >&2 ;;
esac
cd ..
}
mcd () {
test -z "$1" && return 1
mkdir -p "$1" && cd "$1" || return
}
# toggle wireguard vpn with interface /etc/wireguard/wg0.conf
# to allow sudo user toggle vpn without password:
## sudo groupadd wireguard
## sudo usermod -aG wireguard $USER
vpn() {
local STATUS SERVICE CONFIG CONFIGFILE
CONFIG="$1"
test -z "$CONFIG" && CONFIG="wg0"
CONFIGFILE="/etc/wireguard/$CONFIG.conf"
test -e "$CONFIGFILE" || (echo "Config '$CONFIGFILE' not exists" >&2; return 1) && (
SERVICE="wg-quick@$CONFIG.service"
systemctl --quiet is-active "$SERVICE" && \
(sudo systemctl stop "$SERVICE" && echo DOWN) || \
(sudo systemctl start "$SERVICE" && ip -br a | grep wg0 | awk '{print $1":\t"$3}')
)
}
# setup ssh tunnel to $1 as proxy
toggle-proxy() {
if [ -z "$http_proxy" ]; then
export {http,https,ftp,rsync}_proxy="socks5://127.0.0.1:8888"
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
ssh -vCTN -D 0.0.0.0:8888 "$1" 1>/dev/null 2>&1 &
else
unset {http,https,ftp,rsync,no}
fi
}
open() {
test -e $1 || return 1
nohup xdg-open $1 1>/dev/null 2>&1 &
}
split-file() {
local _size
test -z "$2" && _size=1G || _size="$2"
test -e "$1" || return 1
test -d "$1" && \
(mkdir "$1.tar.gz.splitted" && cd "$1.tar.gz.splitted" && tar -cz "../$1" | split -d -b "$_size") || \
(mkdir "$1.splitted" && cd "$1.splitted" && split -d -b "$_size" "../$1")
}
# returns url to code previewer
rayso() {
local CODE TITLE
if [[ -n "$1" ]]; then
CODE="$(cat "$1" | base64)"
TITLE="$(basename "$1")"
else
CODE="$(cat | base64)"
TITLE="Code"
fi
echo "https://ray.so/?colors=candy&background=true&darkMode=true&padding=64&title=${TITLE}&code=${CODE}&language=auto"
}
# json prettyfier
json() {
if [ -t 0 ]; then # argument
python -m json.tool <<< "$*"
else # pipe
python -m json.tool
fi
}
_get_full_file_extension() {
local filename
filename=$(basename "$1")
if [ "${filename:0:1}" = "." ]; then
filename="${filename:1}"
fi
echo ${filename#*.}
}
rmt() {
local filename TRASH
TRASH=~/.trash
if [ ! -d "$TRASH" ]; then
mkdir "$TRASH"
fi
for filename in $*
do
mv "$filename" "$TRASH"/"$(basename "$filename")-$(date +%s)"
done
}
# shows text from ~/.tips/*
showtips() {
TIPS_DIR="$HOME/.tips"
if [ ! -d "$TIPS_DIR" ]; then
mkdir "$TIPS_DIR" 2>/dev/null
git init "$TIPS_DIR"
fi
cat "$TIPS_DIR"/* 2>/dev/null
return 0
}
most-often-commands() {
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn
}
# calculator
= () {
echo "$*" | bc -l
}