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.
while IFS= read -r line || [[ $line ]]; do ...; done < /path/to/input
IFS=, read -r first_name _ city _ <<< "John,Doe,New York,Times Square"
(cd /var/log && ls syslog* | sort -n -t. -k2 | while read file; do echo "===== $file ====="; zcat "$file" || cat "$file"; done)
expected_checksum=...; sha512sum path/to/file | { read actual_checksum _; [ "$actual_checksum" = "$expected_checksum" ] && printf "\033[0;32mValid\n" || printf "\033[0;31mInvalid\n"; }
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
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
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
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
mysql --defaults-file=my.cnf -e 'show tables' | while read t; do mysql --defaults-file=my.cnt -e 'drop table '$t; done
find . -name .svn -type d | while read ss; do dir=$(dirname "$ss"); test $(ls -a "$dir" | wc -l) == 3 && echo "svn rm \"$dir\""; done
ping some_host | while read LINE; do echo $(date): $LINE; done
paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done | sh
find /path/to/dir -type d | tac | while read LINE; do target=$(dirname "$LINE")/$(basename "$LINE" | tr -d ' '); echo mv "$LINE" "$target"; done
paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done