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.

List usernames sorted by user ids

cut -d ':' -f 1,3 /etc/passwd | sort -t ':' -k2n - | tr ':' '\t'

July 18, 2023harsszeg

Get AWS Instance-ID from all Kubernetes nodes

kubectl get node -ojson | jq -r '.items[].spec.providerID' | cut -d/ -f5 | tr -d '"'

June 29, 2023tanmay-bhat

Get the Proportional Set Size (PSS) memory use of a Linux process

sudo cat /proc/1/smaps | grep '^Pss:' | tr -s ' ' | cut -d' ' -f2 | paste -sd+

June 16, 2023harisokanovic

Generate a random 32 characters password

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

March 24, 2019cheuv25

Count the total number of hours of your music collection

find . -print0 | xargs -0 -P 40 -n 1 sh -c 'ffmpeg -i "$1" 2>&1 | grep "Duration:" | cut -d " " -f 4 | sed "s/.$//" | tr "." ":"' - | awk -F ':' '{ sum1+=$1; sum2+=$2; sum3+=$3; sum4+=$4; if (sum4 > 100) { sum3+=1; sum4=0 }; if (sum3 > 60) { sum2+=1; sum3=0 }; if (sum2 > 60) { sum1+=1; sum2=0 } if (NR % 100 == 0) { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 } } END { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 }'

March 1, 2019pingiun

Search man pages and present a PDF

man -k . | awk '{ print $1, $2 }' | dmenu -i -p man | awk '{ print $2, $1 }' | tr -d '()' | xargs man -t | ps2pdf - - | zathura -

December 18, 2018Jab2870

List all packages with at least a class defined in a JAR file

jar tf "$1" | grep '/.*\.class$' | xargs dirname | sort -u | tr / .

February 19, 2018stefanobaghino

Generate random texts

tr -dc a-z1-4 </dev/urandom | tr 1-2 ' \n' | awk 'length==0 || length>50' | tr 3-4 ' ' | sed 's/^ *//' | cat -s | fmt

July 31, 2014bkmeneguello

Install profiling versions of all libghc dpkg packages

sudo dpkg -l | grep libghc | grep "\-dev" | cut -d " " -f 3 | tr '\n' ' ' | sed -e 's/\-dev/\-prof/g' | xargs sudo apt-get install --yes

May 26, 2014openiduser146

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

Counting the number of commas in CSV format

perl -ne 'print tr/,//, "\n"' < file.csv | sort -u

December 1, 2013bashoneliners

Convert text from decimal to little endian hexadecimal

echo $(printf %08X 256 | grep -o .. | tac | tr -d '\n')

August 21, 2013openiduser111

Faster disk imaging with dd

dd if=/dev/sda bs=$(hdparm -i /dev/sda | grep BuffSize | cut -d ' ' -f 3 | tr [:lower:] [:upper:] | tr -d BUFFSIZE=,) conv=noerror | dd of=image.dd conv=noerror

May 19, 2012austindcc

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

Rename all files in the current directory by capitalizing the first letter of every word in the filenames

ls | perl -ne 'chomp; $f=$_; tr/A-Z/a-z/; s/(?<![.'"'"'])\b\w/\u$&/g; print qq{mv "$f" "$_"\n}'

November 1, 2011bashoneliners

Test a one-liner with echo commands first, pipe to bash when ready

paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done | sh

October 8, 2011janos

Remove spaces recursively from all subdirectories of a directory

find /path/to/dir -type d | tac | while read LINE; do target=$(dirname "$LINE")/$(basename "$LINE" | tr -d ' '); echo mv "$LINE" "$target"; done

September 20, 2011bashoneliners

Rename all files in a directory to lowercase names

paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done

August 5, 2011bashoneliners