2024-10-09 11:41:17 +03:00

277 lines
6.2 KiB
Bash

# vim: ft=bash
_sod() {
if [ -d "$1" ] && [ -n "$(ls "$1")" ]; then
local filename
for filename in $(ls "$1" | sort -n)
do
source "${1}/${filename}"
done
fi
}
lt() {
du -h "${1:-.}" 2>/dev/null | sort -h
}
destroy() {
local -r filename="${1}"
if [[ ! -f "${filename}" ]]; then
echo "destroy: File '${filename}' not found" >&2
return 1
fi
local filesize
filesize="$(du -hs "${filename}" | awk '{printf $1}')"
readonly filesize
echo -n "Sure want to destroy file '${filename}' with size ${filesize} [y/N] "
local response
read -r response
readonly response
if [[ ! "${response}" == [yY] ]]; then
return 1
fi
shred -zun 3 "${filename}"
}
workon() {
source "/opt/pythonenv/${1:-$(basename "$PWD")}/bin/activate"
}
py() {
if [[ -z "$@" && -x "$(command -v ipython 2>/dev/null)" ]]; then
ipython -i -c "q = exit"
else
python3 "$@"
fi
}
ve() {
python3 -m virtualenv "${1:-venv}" && . "${1:-venv}/bin/activate"
}
va() {
if [[ -v 1 ]]; then
if [[ -f "${1}/bin/activate" ]]; then
source "${1}/bin/activate"
return 0
fi
echo "va: error: virtual environment ${1} not found, use python3 -m virtualenv venv" >&2
return 1
fi
if [[ -f "./venv/bin/activate" ]]; then
source "./venv/bin/activate"
return 0
fi
local activate_venv
activate_venv="$(find -P . -maxdepth 3 -type f -wholename '*/bin/activate' | sort | head -n 1)"
readonly activate_venv
if [[ -f "$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
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
echo "extract: error: '$1' file does not exist" >&2
return 1
fi
local -r 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
)
mcd() {
if [ -z "$1" ]; then
cd "$(mktemp -td "${USER:-user}.XXXX")"
else
mkdir -p "$1" && cd "$1"
fi
}
open() {
test -e "$1" || return 1
nohup xdg-open "$1" 1>/dev/null 2>&1 &
}
split-file() {
if [[ ! -e "$1" ]]; then
echo "file '${1}' not found" >&2
return 1
fi
local -r size="${2:-1G}"
mkdir "${1}.splitted"
cd "${1}.splitted"
split -d -b "$size" "../${1}"
}
json() {
if [ -t 0 ]; then
python -m json.tool <<< "$*"
else # pipe
python -m json.tool
fi
}
_get_full_file_extension() {
local filename
filename="$(basename "$1")"
readonly filename
if [ "${filename:0:1}" = "." ]; then
filename="${filename:1}"
fi
echo "${filename#*.}"
}
rmt() {
local -r trash="${TRASH:-"${HOME}/.trash"}"
local filename
for filename in "$@"
do
local filename_out_path
filename_out_path="${trash}$(realpath "$filename")"
readonly filename_out_path
mkdir -p "$(dirname "$filename_out_path")"
mv "$filename" "${filename_out_path}_$(date +%s)"
done
}
showtips() {
local -r tips_dir="${HOME}/.tips"
if [ ! -d "$tips_dir" ]; then
mkdir "$tips_dir" 2>/dev/null
git init "$tips_dir"
fi
local content
local filename
for filename in "$tips_dir"/*
do
content="$(cat "$filename")"
echo "$content"
done
return 0
}
most-often-commands() {
history 0 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn
}
# Enable X11Forwarding and disable mux session
sshx() {
ssh -X -o ControlMaster=no -o ControlPath=none "$@"
}
django-create-project() (
set -ue
local -r project_name="${1}"
mkdir "${project_name}"
cd "${project_name}"
python3 -m virtualenv venv
. venv/bin/activate
pip install django
django-admin startproject core .
git init
)
cleanup-directory() {
local -r directory="${1}"
if [[ ! -d "${directory}" ]]; then
echo "Directory '${directory}' not found" >&2
return 1
fi
age="+21" # Notation: +n => "At least n days"
echo "Deleting files not accessed for a ${age} days:"
find "${directory}" -atime "${age}" -exec rm -fv {} \;
echo "Deleting empty directories:"
find "${directory}" -type d -empty -print -delete
}
cleanup-downloads() {
local -r downloads_directory="${HOME}/Downloads"
local -r log_file="${downloads_directory}/cleanup.log"
touch "${log_file}"
echo "$(date -Iseconds) : Start cleanup" >> "${log_file}"
cleanup-directory "${downloads_directory}" >> "${log_file}"
}
_bak() {
local -r filename="${1}"
mv "${filename}" "${filename}.bak"
}
_unbak() {
local -r filename="${1}"
local -r new_filename="${filename/%.bak/}"
if [[ -e "${new_filename}" ]]; then
echo "Filename '${new_filename}' already exists" >&2
return 1
fi
mv "${filename}" "${new_filename}"
}
bak() {
local filename
for filename in "$@"; do
if [[ "${filename: -4}" = ".bak" ]]; then
_unbak "${filename}"
else
_bak "${filename}"
fi
done
}