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 list of unique usernames currently logged in

w | tail -n +3 | cut -d ' ' -f1 | sort -u

September 25, 2023but-i-am-dominator

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

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

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

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

Create a txt files with 10000 rows

for FILE in *.full ; do split -l 100000 $FILE; mv -f xaa `echo "$FILE" | cut -d'.' -f1`.txt; rm -f x*; done

August 22, 2018Kifli88

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

Get number of all Python Behave scenarios (including all examples from Scenario Outlines)

behave -d | grep "scenarios passed" | cut -d, -f4 | sed -e 's/^[[:space:]]*//' | sed 's/untested/scenarios/g'

April 17, 2015openiduser188

Print the window title of current mpv session to display what is playing

wmctrl -pl | grep $(pidof mpv) | cut -d- -f2-

December 15, 2014openiduser171

Open Windows internet shortcut (*.url) files in firefox

grep -i url='*' file.url | cut -b 5- | xargs firefox

September 12, 2014tsjswimmer

Open Windows internet shortcut (*.url) files in firefox

firefox $(grep -i ^url='*' file.url | cut -b 5-)

September 11, 2014tsjswimmer

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

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

Calculate md5sum from an input string

md5sum <<< YOUR_TEXT | cut -f1 -d' '

May 17, 2013kowalcj0

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 video files cached by the flash plugin in browsers

file /proc/*/fd/* 2>/dev/null | grep Flash | cut -f1 -d:

August 27, 2011bashoneliners

Make another user superuser in Ubuntu

for i in $(grep :boss /etc/group | cut -f1 -d:); do adduser wife $i; done

August 5, 2011bashoneliners