bashoneliners.com

Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We review and improve every contributed one-liner to make sure it is of high quality: useful, easy to read, follows best practices, with clear, detailed, accurate explanation. These one-liners should help you automate tasks, troubleshoot problems, whether it be in system administration, file management, networking or programming.

0

Download a URL with cookies set on the command line with curl

curl -b "session=$session" 'https://adventofcode.com/2023/day/18/input'

December 18, 2023bashoneliners

0

Print the lines of file2 that exist in file1

grep -xFf file1 file2

December 11, 2023bashoneliners

0

Extract regex capture groups using [[ and BASH_REMATCH

text='3 blue; 5 green'; [[ $text =~ ([0-9]+)" "(blue|green) ]] && { echo "count = ${BASH_REMATCH[1]}"; echo "color = ${BASH_REMATCH[2]}"; }

December 4, 2023bashoneliners

0

Truncate a file or create an empty file

> /path/to/file

November 29, 2023bashoneliners

0

Print lines of a file that don't exist in another file, ignoring the first two fields

awk -F'|' 'NR == FNR { $1 = ""; $2 = ""; seen[$0]++ } NR != FNR { orig = $0; $1 = ""; $2 = ""; if (!seen[$0]) print orig }' first.txt second.txt

November 27, 2023bashoneliners

0

Replace a pattern in a file in a portable way

f=/path/to/file; sed -e "s/pattern/replacement/" "$f" > "$f".bak && mv "$f".bak "$f"

November 22, 2023bashoneliners

0

Print the first 100 characters of a file

head -c 100 /path/to/file

November 20, 2023bashoneliners

0

Pretty-print JSON with Python

curl -s 'https://api.github.com/orgs/github/repos' | python -m json.tool

November 15, 2023bashoneliners

0

Send an HTTP POST request as if submitting an HTML form using curl

curl --data "title=recipe&text=steps123" https://example.com

November 13, 2023bashoneliners

0

Printing with jq multiple values in CSV or TSV formats

curl -s 'https://api.github.com/orgs/github/repos' | jq -r '.[] | [.id, .name, .stargazers_count] | @csv'

October 13, 2023bashoneliners

0

Check if a version string is in valid SemVer format

re_semver=...; perl -wln -e "/$re_semver/ or exit(1)" <<< "$version"

October 4, 2023bashoneliners

0

Print raw values with jq -r

echo '{"foo": "bar \" baz"}' | jq -r .foo

October 3, 2023bashoneliners

0

The robust way to read lines in a loop

while IFS= read -r line || [[ $line ]]; do ...; done < /path/to/input

September 28, 2023bashoneliners

1

Print the list of unique usernames currently logged in

w | tail -n +3 | cut -d ' ' -f1 | sort -u

September 25, 2023but-i-am-dominator

1

Check if an SSL private key matches a certificate

{ openssl x509 -noout -modulus -in server.crt | openssl md5; openssl rsa -noout -modulus -in server.key | openssl md5; } | uniq

September 20, 2023but-i-am-dominator

0

Check if a value is an integer

is_int() { [[ $1 =~ ^[0-9]+$ ]]; }

September 17, 2023bashoneliners

0

Extract the n-th field from a single line of comma separated values

IFS=, read -r first_name _ city _ <<< "John,Doe,New York,Times Square"

August 31, 2023bashoneliners

1

List usernames sorted by user ids

cut -d ':' -f 1,3 /etc/passwd | sort -t ':' -k2n - | tr ':' '\t'

July 18, 2023harsszeg

1

Watch cpu processes

watch -d -n 1 'ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head'

July 18, 2023harsszeg

1

Get detailed info about distro

echo /etc/*_ver* /etc/*-rel*; cat /etc/*_ver* /etc/*-rel*

July 18, 2023harsszeg

0

Watch a mashup of many video files

L=5; while true; do; readarray -t paths < <(find . -type f -print | shuf -n 1); for i in "${!paths[@]}"; do; path=${paths[i]}; if ffprobe -i "$path" -show_entries format=duration -v quiet -of csv="p=0" > /dev/null; then; N=$(ffprobe -i "$path" -show_entries format=duration -v quiet -of csv="p=0"); D=${N%.*}; P=$((D / 100 * 25)); R=$((1 + RANDOM % D - P * 2)); S=$((P + RANDOM % R)); W=$((R / 4)); LEN=$((1 + RANDOM % L)); mpv "$path" --start="$S" --length="$LEN" --fs &> /dev/null; W=$(bc <<< "$LEN - 0.5"); sleep "$W"; unset 'paths[i]'; fi; done; done

July 6, 2023krypniok

1

Get AWS Instance-ID from all Kubernetes nodes

kubectl get node -ojson | jq -r '.items[].spec.providerID' | cut -d/ -f5 | tr -d '"'

June 29, 2023tanmay-bhat

1

Unzip logrotated files in order and print concatenated content

(cd /var/log && ls syslog* | sort -n -t. -k2 | while read file; do echo "===== $file ====="; zcat "$file" || cat "$file"; done)

June 16, 2023harisokanovic

1

Get the Proportional Set Size (PSS) memory use of a Linux process

sudo cat /proc/1/smaps | grep '^Pss:' | tr -s ' ' | cut -d' ' -f2 | paste -sd+

June 16, 2023harisokanovic