bashoneliners.com

Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We review and improve every contributed one-liner to make sure it is 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.

2

Get a wide info of all pods on all namespaces with Kubectl

kubectl get pods -A -o wide

September 15, 2022adawolfs

1

Validate sha512 checksum

expected_checksum=...; sha512sum path/to/file | { read actual_checksum _; [ "$actual_checksum" = "$expected_checksum" ] && printf "\033[0;32mValid\n" || printf "\033[0;31mInvalid\n"; }

August 24, 2022Hussain-Aqeel

1

Move file and cd with one command

mv file dir/ && cd "$_" && pwd

April 16, 2022Bryan-netizen

2

Report disk usage by file type

find . -type f -empty -prune -o -type f -printf "%s\t" -exec file --brief --mime-type '{}' \; | awk 'BEGIN {printf("%12s\t%12s\n", "bytes", "type")} {type = $2; a[type] += $1} END {for (i in a) printf("%12u\t%12s\n", a[i], i) | "sort -nr"}'

December 28, 2020joeashcraft

1

Use inverted conditions with the find command

find . -type f -not -name '*.mp3'

November 23, 2020sandreas

1

Upgrade outdated packages when using pip

pip install --upgrade $(pip list --outdated | tail -n +3 | awk '{print $1}')

April 16, 2020docdyhr

1

Recursively remove all "node_modules" folders

find . -name node_modules -exec rm -rf {} +

March 29, 2020tg-z

1

Print $PATH entries one per line

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

March 29, 2020tg-z

1

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

2

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

2

Generate a sequence of numbers

seq 1 10

January 17, 2020penkoad

1

Generate a pseudo-random 6-digit number

let N=0 N=N+10**{0..5}*${RANDOM:0:1}

December 7, 2019CK

1

Compute factorial of positive integer using only built-ins

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

November 29, 2019CK

1

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

3

Run multiple commands chained with && and || using a single sudo

sudo -s <<< 'apt update -y && apt upgrade -y'

October 27, 2019secure-debian

2

Generate a sequence of numbers using Brace Expansion

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

October 25, 2019diego

1

List docker log sizes

docker ps -qa | xargs docker inspect --format='{{.LogPath}}' | xargs sudo du -hl

June 18, 2019lockntross

2

Find directories modified between two dates

find . -type d -newermt "2019-01-01" ! -newermt "2019-02-01" -exec ls -ld {} \;

May 20, 2019fenchu

1

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

0

Extract audio only from video files using ffmpeg

ffmpeg -i video.any -vn -acodec libvorbis audio.ogg

April 21, 2019bashoneliners

1

Print the 10 most used commands with their counts

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

April 5, 2019Julien_Tremblay_McLellan

2

Generate a random 32 characters password

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

March 24, 2019cheuv25