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.

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

Print the 10 most used commands with their counts

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

April 5, 2019Julien_Tremblay_McLellan

List top 10 IP addresses connected to your server on port 80 with the number of connections

netstat -tn 2>/dev/null | awk '/:80\>/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head

September 26, 2018Goeks1

Output an arbitrary number of open TCP or UDP ports in an arbitrary range

comm -23 <(seq "$FROM" "$TO") <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | shuf | head -n "$HOWMANY"

February 9, 2018stefanobaghino

Retrieve dropped connections from firewalld journaling

sudo journalctl -b | grep -o "PROTO=.*" | sed -r 's/(PROTO|SPT|DPT|LEN)=//g' | awk '{print $1, $3}' | sort | uniq -c

September 14, 2017FoxBuru

Ban all IPs that attempted to access phpmyadmin on your site

grep "phpmyadmin" $path_to_access.log | grep -Po "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" | sort | uniq | xargs -I% sudo iptables -A INPUT -s % -j DROP

April 2, 2015openiduser187

Print a flat list of dependencies of a Maven project

mvn dependency:list | sed -ne s/..........// -e /patterntoexclude/d -e s/:compile//p -e s/:runtime//p | sort | uniq

September 22, 2014bashoneliners

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

Find all the unique 4-letter words in a text

cat ipsum.txt | perl -ne 'print map("$_\n", m/\w+/g);' | tr A-Z a-z | sort | uniq | awk 'length($1) == 4 {print}'

January 29, 2012bashoneliners