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 raw values with jq -r

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

October 3, 2023bashoneliners

Get detailed info about distro

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

July 18, 2023harsszeg

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

Print $PATH entries one per line

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

March 29, 2020tg-z

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

Compute factorial of positive integer using only built-ins

factorial() { local N; eval let N=1 N*={1..$1}; echo "$N"; }

November 29, 2019CK

Recursively compute factorial of positive integer using only built-ins

factorial() ( IFS=\*; let N=$1-1 k="$*" && factorial "$N" "$k" || echo ${2-1} )

November 25, 2019CK

Generate a sequence of numbers using Brace Expansion

for i in {1..10}; do echo "$i"; done

October 25, 2019diego

Scan all open local ports without any external programs

for i in {1..65535}; do (< "/dev/tcp/127.0.0.1/$i") &>/dev/null && { echo; echo "[+] Open Port at: $i"; }  || printf "."; done; echo

May 8, 2019Goeks1

Dump all AWS IAM users/roles to a Terraform file for editing / reusing in another environment

echo iamg iamgm iamgp iamip iamp iampa iamr iamrp iamu iamup | AWS_PROFILE=myprofile xargs -n1  terraforming

August 28, 2018johntellsall

List open processes ordered by it's number of open files

ps -ef |awk '{ print $2 }' \ 	|tail -n +2 \ 	|while read pid; do echo "$pid	$(lsof -p $pid |wc -l)"; done \ 	|sort -r -n -k 2 \ 	|while read pid count; do echo "$pid	$count	$(ps -o command= -p $pid)"; done

August 22, 2018cddr

Puppet/Bash: test compare json objects.

unless => "client_remote=\"$(curl localhost:9200/_cluster/settings | python -c \"import json,sys;obj=json.load(sys.stdin);print(obj['persistent']['search']['remote'])\")\"; new_remote=\"$( echo $persistent_json | python -c \"import json,sys;obj=json.load(sys.stdin);print(obj['persistent']['search']['remote'])\")\"; [ \"$client_remote\" = \"$new_remote\" ]",

July 27, 2018cjedwa

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

Blackhole ru zone

echo "address=/ru/0.0.0.0" | sudo tee /etc/NetworkManager/dnsmasq.d/dnsmasq-ru-blackhole.conf && sudo systemctl restart network-manager

November 14, 2017olshek_

Create an array of CPU frequencies in GHz

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

December 28, 2015openiduser146

Generate a sequence of numbers, zero-padded

echo {01..10}

March 1, 2015Elkku

Copy the output of a command to clipboard for easy pasting with Command-V in OSX

echo "Example command output..." | tee >(pbcopy)

February 28, 2015Elkku

Download a file from a webserver with telnet

(echo 'GET /'; echo; sleep 1; ) | telnet www.google.com 80

December 22, 2014bashoneliners

Generate a sequence of numbers using a simple for loop

for ((i=1; i<=10; ++i)); do echo $i; done

November 4, 2014bashoneliners

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

Compute factorial of positive integer

fac() { { echo 1; seq $1; } | paste -s -d\* | bc; }

May 21, 2014jeroenjanssens

Extensive "cleanup" operations following "sudo yum upgrade"

sudo yum upgrade && for pkg in $(package-cleanup --orphans -q); do repoquery $(rpm -q $pkg --queryformat="%{NAME}") | grep -q ".*" && echo $pkg; done | xargs sudo yum -y remove && for pkg in $(package-cleanup --leaves --all -q); do repoquery --groupmember $pkg | grep -q "@" || echo $pkg; done

April 16, 2014openiduser143

Display the number of connections per IP to port 80

while true; do clear; date; echo; echo "[Count] | [IP ADDR]"; echo "-------------------"; netstat -n | grep ':80\>' | awk '! /LISTEN/ {print $5}' | cut -d: -f1 | uniq -c; sleep 5; done

April 9, 2014cesp