Awesome One-liner

ℹ️ Note

All commands here assumed Bash execution, unless noted as Zsh. Full POSIX compatibility is not guranteed.

Fun

Get current BTC price (without demicals) using rate.sx API:

curl -s 'rate.sx/1btc?format=qT' | cut -f1 -d\.

Start one-off bukuserver with uvx aka. uv tool run:

uvx --from "buku[server]" bukuserver run --host 127.0.0.1 --port 5001

Check running time (run at the end of script, mainly to avoid prevent COND && COMMAND bug in Bash Strict Mode):

# most bashism, but not working in Bash Strict Mode
timing() { echo "Task finished in $SECONDS $(( SECONDS <= 1 ? "second" : "seconds" ))."; }
# boring, but working with "set -u"
timing() { echo "Task finished in $SECONDS $( (( SECONDS <= 1 )) && echo second || echo seconds )."; }
# a bit hacky, but works
timing() { units=(seconds second); echo "Task finished in $SECONDS ${units[SECONDS <= 1 ? 1 : 0]}."; }
# awesome one-liner
timing() { local s=${SECONDS:-0} u="seconds"; [[ $s -le 1 ]] && u="second"; echo "Task finished in $s $u."; }

IP

Get internal WSL IP:

ip route | awk '/^default/{print $3}'

Get Windows host IP from WSL (if generateHosts isn’t set to False in /etc/wsl.conf)

host `hostname` | grep -oP '(\s)\d+(\.\d+){3}' | tail -1 | awk '{ print $NF }' | tr -d '\r'

Get inet IP:

# BSD
ifconfig | awk '/inet/ { sub(/%.*/, "", $2); print $2 }'
# Linux
ip a | awk '/inet/ { sub(/\/.*/, "", $2); print $2 }'
# IPv4 only
ip a | awk '/inet / { sub(/\/.*/, "", $2); print $2 }'
# IPv6 only
ip a | awk '/inet6/ { sub(/\/.*/, "", $2); print $2 }'

Git

Create a full git bundle using repo name:

gbc() {
	git bundle create $(basename `git rev-parse --show-toplevel`).bundle --all
}

Pull all git repo in sub-folders (change 3 to dig deeper) in parallel:

find . -maxdepth 3 -name .git -type d -printf "%h\0" | parallel -0 -k --line-buffer --tag git -c color.ui=always -C {} pull

Get nightly or any selected release file on GitHub (inspired by Bash: Determining latest GitHub release tag and version):

# Get Citra Nightly Download URL
GITHUB_URL=$(curl --silent --location https://api.github.com/repos/citra-emu/citra-nightly/releases/latest | jq --raw-output ".assets[].browser_download_url" | grep --extended-regexp "citra-linux-.*?.tar.xz")
	
# A more advanced (read: wicked) one
# Download Sophia-Script Selected Release
curl --silent --location https://api.github.com/repos/farag2/Sophia-Script-for-Windows/releases/latest | jq --raw-output ".assets[].browser_download_url" | grep --extended-regexp "10.LTSC.2021|11.v" | xargs wget --quiet --input-file

GitHub Action custom filter:

on:
  push:
	branches: ['main']
	# Only run when foo is changed
	paths: ['a-path/foo']

jobs:
  build-that-can-skip:
	runs-on: ubuntu-latest
	# Skip build with [ci skip] in last commit
	if: "! contains(github.event.head_commit.message, '[ci skip]')"

  sensitive-build:
	runs-on: ubuntu-latest
	# Only run on commits with [build]
	if: "contains(toJSON(github.event.head_commit.message), '[build]')"

File

Delete huge number of files using rsync

mkdir empty && rsync -r --delete empty/ some-dir && rmdir some-dir

Sort, filter, redirect a file using only sort:1

rookie
-cat en.utf-8.add | sort | uniq > 1.add; mv 1.add en.utf-8.add
normie
-cat en.utf-8.add | sort | uniq | sponge en.utf-8.add
cool, but not enough
-cat en.utf-8.add | sort -u | sponge en.utf-8.add
cooler, still not enough
-sort -u < en.utf-8.add | sponge en.utf-8.add
nerdy
+sort -u en.utf-8.add -o en.utf-8.add

Format

Convert PDF to PNG using command line

# requires ghostscript
gs -dBATCH -dNOPAUSE -sDEVICE=pnggray -r300 -dUseCropBox -sOutputFile=item-%03d.png examples.pdf

Convert SQL to formatted single line JSON in zsh (used in datasette-dashboards):

# aliases.zsh
sql2json() {
    # Usage:
    #   sql2json 1.sql
    #   cat 1.sql | sql2json
    #   echo 'SELECT * FROM public' | sql2json
    sed 's/--.*$//' "${1:-/dev/stdin}" | tr '\n' ' ' | sed 's/^ *//;s/ *$//;s/  */ /g' | jq -R '{query: .}'
}

Convert ROMs in CUE+BIN to CHD:

cue2chd() {
    # use cue file name
    # find . -maxdepth 2 -name '*.cue' -type f -exec bash -c 'chdman createcd -i "$0" -o "../${0%.*}.chd"' {} \;
    # use subdirectory name
    find . -maxdepth 2 -name '*.cue' -type f -exec bash -c 'chdman createcd -i "$0" -o "$(basename "$(dirname "$0")").chd"' {} \;
}

Zsh

print $PATH using zsh built-in parameter expansion:

path() {
	# echo $PATH | sed 's/:/\n/g' # not cool
	echo "${PATH//:/\n}" # cool
}

Use latest installed version of python/lua as alias (failsafe of distro specific method like dpkg-reconfigure, xbps-alternatives, or stow):

# aliases.zsh
# Lua
for cmd in lua5.{2..5}; (( $+commands[$cmd] )) && alias lua=$cmd
# luarocks is manually installed and updated
# for cmd in luarocks-5.{2..5}; (( $+commands[$cmd] )) && alias luarocks=$cmd
# Python
for cmd in python python3 python3.{11..14}; (( $+commands[$cmd] )) && alias py=$cmd; alias python=$cmd

Misc

Determine glibc or musl-libc:

if [ -e /usr/lib/ld-musl-x86_64.so ]; then echo musl; elif [ -e /usr/lib/ld-linux-x86-64.so ]; then echo glibc; else echo unknown; fi

# long version
check_musl() {
	local libdir=/usr/lib
	if ls "$libdir"/ld-musl* 1>/dev/null 2>&1; then
	  echo musl
	  exit 0
	fi
	if ls "$libdir"/ld-linux* 1>/dev/null 2>&1; then
	  echo glibc
	  exit 0
	fi
	echo unknown
}

Ask for explicit confirmation before proceeding (useful in hashsum check, dry run preview):

# just press y, without enter
confirm() { read -n 1 -rp "Looking good? [y/N] " m < /dev/tty; echo; [[ "${m,,}" == "y" ]] || exit 1; }

# or if you like hitting enter
confirm() { read -rp "Looking good? [y/N] " m < /dev/tty; [[ "${m,,}" == y* ]] || exit 1; }

# standard version
confirm() { read -rp "Looking good? [y/N] " m < /dev/tty; [[ "${m,,}" =~ ^(y|yes)$]] || { echo "Canceled."; exit 1; }; echo "Proceeding."; }

# long (boring) version
confirm() {
    local match
    read -r -p "Looking good? [y/N]" match < /dev/tty
    case "${match,,}" in # convert to lower case, bash 4.0+
        y|yes) echo "Proceeding." ;;
        *) echo "Canceled."; exit 1 ;;
    esac
}

Show size of installed packages in descending order on Linux, from my ultimate backup() which works everywhere:

# apk-tools on Alpine, requires gawk and coreutils/uutils-coreutils
apk query --fields name,installed-size --installed $(apk info) | awk '/^Name:/ {pkg=$2} /^Installed-Size:/ {print $2, pkg}' | sort -rn | numfmt --to=iec --field=1

# pacman on Arch/Artix/CachyOS/Manjaro, requires expac
expac "%m\t%n" | sort -rn | numfmt --to=iec --field=1

# apt/dpkg on Debian/Devuan/Termux/Ubuntu
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -rn | numfmt --from-unit=1024 --to=iec --field=1

# emerge/portage on Gentoo, requires app-portage/portage-utils (aka. qtools)
qsize --sum

# yum/dnf on Alma/CentOS/Fedora/RHEL/Rocky
rpm -qa --queryformat '%{size}\t%{name}\n' | sort -rn | numfmt --to=iec --field=1

# xbps on Void, requires xtools-minimal
xhog

  1. -o is used here because sort supports inplace redirect. You CAN NOT use it for any command! For GNU grep you have to write grep -v '^#' cities.txt | sponge cities.txt, or just use sed -i '/^#/d' cities.txt↩︎

Vinfall's Geekademy

Sine īrā et studiō


Collection of awesome one-liners in shells.


Created 2022-09-22
Updated 2026-03-07
Contain 1089 words
GPG Key html asc

QR code
QR code linking to https://blog.vinfall.com/posts/2022/09/awesome-one-liner/
#dev #git #ip #linux #mime #wsl