Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash tips and tricks. We are committed to review every contributed one-liner to ensure its high quality, practical usefulness, and a solid explanation of how it works. We want to document one-liners for frequent (non-trivial) tasks executed in the shell, written in the best possible way, following good practices known in the industry. To get the latest Bash one-liners, follow @bashoneliners on Twitter. If you find any problems, please report on GitHub Issues.
1 score

Clear the Exim Queue (remove all messages)

 $ exim -bp | exiqgrep -i | xargs exim -Mrm

Feb. 11, 2019, 8:58 a.m.Robert

0 score

Check if a text snippet is valid C code

 $ gcc -fsyntax-only -xc - <<< "text snippet"

Feb. 10, 2019, 8:12 a.m.Janos

1 score

Inspect the HTTP headers of a website

 $ curl -I amazon.com

Feb. 8, 2019, 10:56 p.m.Janos

2 score

Send HTTP POST to a website with a file input field

 $ curl -L -v -F "value=@myfile" "http://domain.tld/whatever.php"

Feb. 15, 2012, 11:26 p.m.openiduser14

3 score

Find all log files modified 24 hours ago, and zip them

 $ find . -type f -mtime +1 -name "*.log" -exec zip -m {}.zip {} \; >/dev/null

Nov. 9, 2018, 10:04 a.m.TrongTan124

1 score

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 -

Dec. 18, 2018, 11:31 a.m.Jab2870

4 score

List IP addresses connected to your server on port 80

 $ netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head

Sept. 26, 2018, 11:10 p.m.Goeks1

1 score

Random 6-digit number

 $ python -c 'import random; print(random.randint(0,1000000-1))'

Sept. 19, 2018, 10:42 p.m.johntellsall

1 score

Very fast history search with Ripgrep

 $ rh() { rg "$1" ~/.bash_history }

Sept. 18, 2018, 7 p.m.johntellsall

0 score

While loop to pretty print system load (1, 5 & 15 minutes)

 $ while :; do date; awk '{printf "1 minute load: %.2f\n", $1; printf "5 minute load: %.2f\n", $2; printf "15 minute load: %.2f\n", $3}' /proc/loadavg; sleep 3; done

Sept. 5, 2018, 8:41 p.m.Janos

1 score

Scan entire Git repo for dangerous Amazon Web Service IDs

 $ git grep -Ew '[A-Z0-9]{20}'

Sept. 5, 2018, 8:30 p.m.Janos

0 score

Scan entire Git repos for dangerous Amazon Web Service IDs

 $ git ls-tree --full-tree -r --name-only HEAD | xargs egrep -w '[A-Z0-9]{20}'

Aug. 31, 2018, 10:29 p.m.johntellsall

0 score

While loop to pretty print system load (1, 5 & 15 minutes)

 $ while [ 1 == 1 ]; do  cat /proc/loadavg | awk '{printf "1 minute load: %.2f\n", $(NF-5)}' && cat /proc/loadavg |awk '{printf "5 minute load: %.2f\n", $(NF-3)}' && cat /proc/loadavg |awk '{printf "15 minute load: %.2f\n", $(NF-2)}'; sleep 3; date; done

Aug. 30, 2018, 8:54 a.m.peek2much3

0 score

Dump all AWS IAM users/roles to a Terraform file for editing / reusing in another environment

 $ echo iamg iamgm iamgp iamip iamp iampa iamr iamrp iamu iamup | AWS_PROFILE=myprofile xargs -n1  terraforming

Aug. 28, 2018, 12:38 a.m.johntellsall

0 score

Organise image by portrait and landscape

 $ mkdir "portraits"; mkdir "landscapes"; for f in ./*.jpg; do WIDTH=$(identify -format "%w" "$f")> /dev/null; HEIGHT=$(identify -format "%h" "$f")> /dev/null; if [[ "$HEIGHT" > "$WIDTH" ]]; then mv "$f" portraits/ ; else mv "$f" landscapes/ ; fi; done

Aug. 23, 2018, 2:09 p.m.Jab2870

0 score

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

Aug. 22, 2018, 2:02 p.m.Kifli88

1 score

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

Aug. 22, 2018, 1:21 p.m.cddr

0 score

Remove all container from an specific network (docker)

 $ docker ps -a -f network=$NETWORK --format='{{.ID}}' | xargs docker rm -f

Aug. 17, 2018, 4:38 p.m.gatero

0 score

Up all docker services as detached mode over all immediate subdirectories

 $ for dir in $(ls -d */); do eval $(cd $PWD/$dir && docker-compose up -d && cd ..); done;

Aug. 17, 2018, 4:31 p.m.gatero

0 score

Find and replace string inside specific files

 $ grep -ril '$SEARCH_PATTERN' src | sed -i 's/$FIND_PATTERN/$REPLACE_PATTERN/g'

Aug. 17, 2018, 4:18 p.m.gatero

0 score

Puppet/Bash: test compare json objects.

 $ unless => "client_remote=\"$(curl localhost:9200/_cluster/settings | python -c \"import json,sys;obj=json.load(sys.stdin);print(obj['persistent']['search']['remote'])\")\"; new_remote=\"$( echo $persistent_json | python -c \"import json,sys;obj=json.load(sys.stdin);print(obj['persistent']['search']['remote'])\")\"; [ \"$client_remote\" = \"$new_remote\" ]",

July 27, 2018, 8:37 p.m.cjedwa

0 score

Print wifi access points sorted by signal

 $ iw dev IFACE scan | egrep "SSID|signal" | awk -F ":" '{print $2}' | sed 'N;s/\n/:/' | sort

June 16, 2018, 5:37 a.m.kazatca

2 score

Kill a process running on port 8080

 $ lsof -i :8080 | awk '{l=$2} END {print l}' | xargs kill

June 15, 2018, 4:18 a.m.jamestomasino

0 score

Delete all untagged Docker images

 $ docker images -q -f dangling=true | xargs --no-run-if-empty --delim='\n' docker rmi

June 15, 2018, 1:12 a.m.penguincoder

1 score

Take values from a list (file) and search them on another file

 $ for ITEM in `cat values_to_search.txt`; do  (egrep $ITEM full_values_list.txt && echo $ITEM found) | grep "found" >> exit_FOUND.txt; done

May 16, 2018, 3:20 p.m.ManuViorel