See more cool one liners at http://www.bashoneliners.com

Check if a text snippet is valid C code
$ gcc -fsyntax-only -xc – <<< “text snippet”
Feb. 10, 2019, 8:12 a.m. — Janos

Inspect the HTTP headers of a website
$ curl -I amazon.com
Feb. 8, 2019, 10:56 p.m. — Janos

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


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


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


Random 6-digit number
$ python -c ‘import random; print(random.randint(0,1000000-1))’
Sept. 19, 2018, 10:42 p.m. — johntellsall


Very fast history search with Ripgrep
$ rh() { rg “$1” ~/.bash_history }
Sept. 18, 2018, 7 p.m. — johntellsall


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


Delete all untagged Docker images
$ docker rmi $(docker images -f “dangling=true” -q)
April 27, 2018, 2:50 p.m. — stefanobaghino


Have script run itself in a virtual terminal
$ tty >/dev/null || { urxvt -e /bin/sh -c “tty >/tmp/proc$$; while test x; do sleep 1; done” & while test ! -f /tmp/proc$$; do sleep .1; done; FN=$(cat /tmp/proc$$); rm /tmp/proc$$; exec >$FN 2>$FN <$FN; }

2 Replies to “Cool Bash One Liners”

Comments are closed.