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.

Print the lines of file2 that exist in file1

grep -xFf file1 file2

December 11, 2023bashoneliners

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

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

Print the first 100 characters of a file

head -c 100 /path/to/file

November 20, 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

The robust way to read lines in a loop

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

September 28, 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

Find and replace string inside specific files

grep -ril '$SEARCH_PATTERN' src | sed -i 's/$FIND_PATTERN/$REPLACE_PATTERN/g'

August 17, 2018gatero

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

Remove new lines from files and folders

rename 's/[\r\n]//g' *

September 30, 2017moverperfect

Get the latest Arch Linux news

w3m https://www.archlinux.org/ | sed -n "/Latest News/,/Older News/p" | head -n -1

August 15, 2017Jab2870

Shuffle lines

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

January 31, 2016openiduser81

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