Skip to main content

Bash-Fu

Cool things you can do on a Linux command line

File Querying/Manipulation

Unzip files into a directory named after the zip file
for file in *.zip; do     [ -f "$file" ] || continue;     unzip "$file" -d "${file%%.zip}"; done
for file in *.zip; do     [ -f "$file" ] || continue;     mkdir "${file%%.zip}" && bsdtar -xvf "$file" -C "${file%%.zip}"; done
for file in *.zip; do     [ -f "$file" ] || continue; podman run --rm --workdir /data -it -v .:/data docker.io/crazymax/7zip 7za x -aoa "$file" -o"${file%%.zip}"; done
Recursively count the number of files of any extension exist
find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort | uniq -c | sort -rn

# Example Output
14 zip
# 12 config
#  8 7z
Find which ports have been used by Podman/Docker containers
# Recursively search a directory for quadlet/compose files
find *.* -exec grep -oE '[0-9]{4,}:' '{}' \; | sort -h

# Search through running containers
podman ps --format "{{.Ports}}" | grep -oE ':[0-9]{4,}' | sort -h

FFmpeg

Cut a clip
ffmpeg -ss 00:26:03 -to 00:27:05  -i <input file> -filter:v scale=720x480 -c:a copy <output file>
Recursively convert FLAC files into mp3
fdfind -t f -e flac -x ffmpeg -i "{}" -qscale:a 0 "{.}.mp3"

Other

Create a WiFi QR code
qrencode -o wifi.png "WIFI:T:WPA;S:<SSID>;P:<PASSWORD>;;"
Create and open a two-factor LUKS device (password and keyfile)
# Format
echo 'Name of /dev/<device file>:'; read -r devicefile; echo 'Location of keyfile:'; read -r filepath; echo 'Password:'; read -rs passwd; fullpass="$(cat $filepath)$passwd"; echo $fullpass | sudo cryptsetup luksFormat /dev/$devicefile -d -
# Unlock
echo 'Name of /dev/<device file>:'; read -r devicefile; echo 'Location of keyfile:'; read -r filepath; echo 'Password:'; read -rs passwd; fullpass="$(cat $filepath)$passwd"; echo $fullpass | sudo cryptsetup luksOpen /dev/$devicefile cryptdisk -d -