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 first 100 characters of a file

head -c 100 /path/to/file

November 20, 2023bashoneliners

Watch cpu processes

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

July 18, 2023harsszeg

Print the 10 most used commands with their counts

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

April 5, 2019Julien_Tremblay_McLellan

Generate a random 32 characters password

tr -dc 'a-zA-Z0-9~!@#$%^&*_()+}{?></";.,[]=-' < /dev/urandom | fold -w 32 | head -n 1

March 24, 2019cheuv25

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

Perform git commit with a random message from whatthecommit.com

git commit -m "$(w3m whatthecommit.com | head -n 1)"

January 5, 2018Jab2870

Get the latest Arch Linux news

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

August 15, 2017Jab2870

Get average CPU temperature from all cores.

__=`sensors | grep Core` && echo \(`echo $__ | sed 's/.*+\(.*\).C\(\s\)\+(.*/\1/g' | tr "\n" "+" | head -c-1`\)\/`echo $__ | wc -l` | bc && unset __

April 2, 2014openiduser139

Get mac address from default interface OS X

netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {}  ifconfig {} | awk '/ether/ {print $2}'

August 21, 2013spotmac

Get only the latest version of a file from across mutiple directories.

find . -name 'filename' | xargs -r ls -tc | head -n1

March 7, 2013Anntoin

Really lazy way to print the first instance of $foo that occurs after $bar

ifconfig | grep ^en1 -A5 | grep inet | head -n 1

May 12, 2012bashoneliners

Find the most recently modified files in a directory and all subdirectories

find /path/to/dir -type f -mtime -7 -print0 | xargs -0 ls -lt | head

March 8, 2012bashoneliners