bashoneliners.com

Welcome to bashoneliners.com, a curated collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We aim to make each published one-liner to be 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.

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

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

Pretty-print JSON with Python

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

November 15, 2023bashoneliners

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

Print raw values with jq -r

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

October 3, 2023bashoneliners

Print the list of unique usernames currently logged in

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

September 25, 2023but-i-am-dominator

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

List usernames sorted by user ids

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

July 18, 2023harsszeg

Print $PATH entries one per line

echo "${PATH//:/\\n}"

March 29, 2020tg-z

Print .bash_history with epoch timestamps converted to human-readable dates

awk '/^#[0-9]*$/ {split($0, arr, "#"); print "#", strftime("%c", arr[2]); getline; print }' < /path/to/.bash_history

March 10, 2020joeashcraft

Print the paths in $PATH sorted by line length

echo "${PATH//:/\\n}" | awk '{print length, $0}' | sort -n | cut -f2- -d' '

March 7, 2020tg-z

Print the 10 most used commands with their counts

history | awk '{print $2}' | sort | uniq -c | sort -nr | head

April 5, 2019Julien_Tremblay_McLellan

Count the total number of hours of your music collection

find . -print0 | xargs -0 -P 40 -n 1 sh -c 'ffmpeg -i "$1" 2>&1 | grep "Duration:" | cut -d " " -f 4 | sed "s/.$//" | tr "." ":"' - | awk -F ':' '{ sum1+=$1; sum2+=$2; sum3+=$3; sum4+=$4; if (sum4 > 100) { sum3+=1; sum4=0 }; if (sum3 > 60) { sum2+=1; sum3=0 }; if (sum2 > 60) { sum1+=1; sum2=0 } if (NR % 100 == 0) { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 } } END { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 }'

March 1, 2019pingiun

Print wifi access points sorted by signal

iw dev IFACE scan | egrep "SSID|signal" | awk -F ":" '{print $2}' | sed 'N;s/\n/:/' | sort

June 16, 2018kazatca

Take values from a list (file) and search them on another file

for ITEM in $(cat values_to_search.txt); do  (egrep $ITEM full_values_list.txt && echo $ITEM found) | grep "found" >> exit_FOUND.txt; done

May 16, 2018ManuViorel

Big CSV > batches > JSON array > CURL POST data with sleep

cat post-list.csv | split -l 30 - --filter='jq -R . | jq --slurp -c .' | xargs -d "\n" -I % sh -c 'curl -H "Content-Type: application/json" -X POST -d '"'"'{"type":1,"entries":%}'"'"' http://127.0.0.1:8080/purge-something && sleep 30'

March 7, 2018pratham2003

Create an array of CPU frequencies in GHz

cpus=($({ echo scale=2; awk '/cpu MHz/ {print $4 " / 1000"}' /proc/cpuinfo; } | bc))

December 28, 2015openiduser146

Get number of all Python Behave scenarios (including all examples from Scenario Outlines)

behave -d | grep "scenarios passed" | cut -d, -f4 | sed -e 's/^[[:space:]]*//' | sed 's/untested/scenarios/g'

April 17, 2015openiduser188

Shuffle lines

seq 5 | shuf

March 12, 2015openiduser184

Shuffle lines

... | perl -MList::Util -e 'print List::Util::shuffle <>'

October 25, 2014bashoneliners

Open Windows internet shortcut (*.url) files in firefox

grep -i url='*' file.url | cut -b 5- | xargs firefox

September 12, 2014tsjswimmer

Delete orphan vi and vim undo files

find . -type f -iname '*.un~' | while read UNDOFILE ; do FILE=$( echo "$UNDOFILE" | sed -r -e 's/.un~$//' -e 's&/\.([^/]*)&/\1&' ) ; [[ -e "$FILE" ]] || rm "$UNDOFILE" ; done

September 2, 2014rafaeln

Parse nginx statistics output

i=$(curl -s server/nginx_stats); IFS=$'\n'; i=($i); a=${i[0]/Active connections: } && a=${a/ }; r=${i[2]# [0-9]* [0-9]* }; echo "Active: $a, requests: $r"

June 20, 2014azat

Convert data format from DD/MM/YYYY to ISO-8601 (YYYY-MM-DD)

sed 's_\([0-9]\{1,2\}\)/\([0-9]\{1,2\}\)/\([0-9]\{4\}\)_\3-\2-\1_g'

December 30, 2013laurip

Replace sequences of the same characters with a single character

echo heeeeeeelllo | sed 's/\(.\)\1\+/\1/g'

December 11, 2013bashoneliners