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.

The robust way to read lines in a loop

while IFS= read -r line || [[ $line ]]; do ...; done < /path/to/input

September 28, 2023bashoneliners

Extract the n-th field from a single line of comma separated values

IFS=, read -r first_name _ city _ <<< "John,Doe,New York,Times Square"

August 31, 2023bashoneliners

Unzip logrotated files in order and print concatenated content

(cd /var/log && ls syslog* | sort -n -t. -k2 | while read file; do echo "===== $file ====="; zcat "$file" || cat "$file"; done)

June 16, 2023harisokanovic

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

List open processes ordered by it's number of open files

ps -ef |awk '{ print $2 }' \ 	|tail -n +2 \ 	|while read pid; do echo "$pid	$(lsof -p $pid |wc -l)"; done \ 	|sort -r -n -k 2 \ 	|while read pid count; do echo "$pid	$count	$(ps -o command= -p $pid)"; done

August 22, 2018cddr

Delete orphan vi and vim undo files

find . -type f -iname '*.un~' | while read UNDOFILE ; do FILE=$( echo "$UNDOFILE" | sed -r -e 's/.un~$//' -e 's&/\.([^/]*)&/\1&' ) ; [[ -e "$FILE" ]] || rm "$UNDOFILE" ; done

September 2, 2014rafaeln

Calculate an h index from an EndNote export

MAX=$(NUM=1;cat author.xml |perl -p -e 's/(Times Cited)/\n$1/g'|grep "Times Cited" |perl -p -e 's/^Times Cited:([0-9]*).*$/$1/g'|sort -nr | while read LINE; do if [ $LINE -ge $NUM ]; then echo "$NUM"; fi; NUM=$[$NUM+1]; done;); echo "$MAX"|tail -1

February 4, 2012openiduser14

Group count sort a log file

A=$(FILE=/var/log/myfile.log; cat $FILE | perl -p -e 's/.*,([A-Z]+)[\:\+].*/$1/g' | sort -u | while read LINE; do grep "$LINE" $FILE | wc -l | perl -p -e 's/[^0-9]+//g'; echo -e "\t$LINE"; done;);echo "$A"|sort -nr

January 31, 2012openiduser14

Delete all tables of a mysql database

mysql --defaults-file=my.cnf -e 'show tables' | while read t; do mysql --defaults-file=my.cnt  -e 'drop table '$t; done

January 8, 2012bashoneliners

Remove all the versioned-but-empty directories from a Subversion checkout

find . -name .svn -type d | while read ss; do dir=$(dirname "$ss"); test $(ls -a "$dir" | wc -l) == 3 && echo "svn rm \"$dir\""; done

November 27, 2011bashoneliners

Add timestamp to the output of ping

ping some_host | while read LINE; do echo $(date): $LINE; done

October 10, 2011janos

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