# vim: ft=zsh 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() { local -r venvs="${VENVS:-/opt/pythonenv}" source "${venvs}/${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() { local -r venv_name="${1:-venv}" python3 -m venv "${venv_name}" && . "${venv_name}/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 venv ${1}'" >&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 venv venv" >&2 return 1 fi } extract() ( if [ -z "${1}" ]; then echo "extract: error: usage: extract ." 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 set -o nullglob local filename for filename in "${tips_dir}"/* do cat "${filename}" done return 0 } most-often-commands() { history 0 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn } 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}" | tee -a "${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 } mirror-site() ( set -eu local -r name="${1}"; shift mkdir -p "${name}" && cd "${name}" local -r user_agent="Mozilla/5.0 ..." wget \ --page-requisites \ --convert-links \ --adjust-extension \ --restrict-file-names=ascii \ --span-hosts \ --random-wait \ --execute robots=off \ --recursive \ --timestamping \ -l inf \ --no-remove-listing \ --no-parent \ --user-agent "${user_agent}" \ --reject '*.woff*,*.ttf,*.eot,*.js' \ --tries 10 \ $@ ) sha() { if [ -v 2 ]; then shasum -a 256 "${@}" elif [ -v 1 ]; then shasum -a 256 "${@}" | head -c 64 else shasum -a 256 | head -c 64 fi }