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

Scan all open ports without any required program

 $ for i in {1..65535}; do (echo < /dev/tcp/127.0.0.1/$i) &>/dev/null && printf "\n[+] Open Port at\n: \t%d\n" "$i" || printf "."; done

May 8, 2019, 4:56 p.m.Goeks1

1 score

Generate a random 32 characters password

 $ tr -dc 'a-zA-Z0-9~!@#$%^&*_()+}{?></";.,[]=-' < /dev/urandom | fold -w 32 | head -n 1

March 24, 2019, 6:39 p.m.cheuv25

1 score

Using tcpdump with port ranges and file count/size

 $ sudo /usr/sbin/tcpdump -i any -s 0 -n -Z <user_name> -C 500 -W 100 -w /home/<user_name>/$(hostname).pcap -f '(port (# or # or # or # or # or # or ...) or portrange <start>-<end>)' &>/dev/null

March 15, 2019, 1:02 a.m.peek2much3

1 score

Compare (diff) vim-generated backup of a file with that file's current version.

 $ diff~() { diff "$1"~ "$1"; }

March 12, 2019, 2:52 a.m.juanolopezgerena

1 score

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, 2019, 9:09 p.m.pingiun

1 score

Clear the Exim Queue (remove all messages)

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

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

1 score

Inspect the HTTP headers of a website

 $ curl -I amazon.com

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

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

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

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

1 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

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

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

1 score

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; }

March 9, 2018, 2:56 a.m.openiduser111

1 score

Blackhole ru zone

 $ echo "address=/ru/0.0.0.0" | sudo tee /etc/NetworkManager/dnsmasq.d/dnsmasq-ru-blackhole.conf && sudo systemctl restart network-manager

Nov. 14, 2017, 2:12 p.m.olshek_

1 score

Kill a process running on port 8080

 $ lsof -i :8080 | awk 'NR > 1 {print $2}' | xargs --no-run-if-empty kill

Sept. 1, 2017, 8:31 p.m.Janos

1 score

Kill a process running on port 8080

 $ lsof -i :8080 | awk '{print $2}' | tail -n 1 | xargs kill

Aug. 18, 2017, 8:22 a.m.kimbethwel

1 score

Get the latest Arch Linux news

 $ w3m https://www.archlinux.org/ | sed -n "/Latest News/,/Older News/p" | head -n -1

Aug. 15, 2017, 10:35 a.m.Jab2870

1 score

Listen to the radio (radio2 in example)

 $ mpv http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_med/llnw/bbc_radio_two.m3u8

July 19, 2017, 2:44 p.m.Jab2870

1 score

Go up to a particular folder

 $ alias ph='cd ${PWD%/public_html*}/public_html'

July 18, 2017, 6:07 p.m.Jab2870

1 score

Preserve your fingers from cd ..; cd ..; cd..; cd..;

 $ up(){ DEEP=$1; for i in $(seq 1 ${DEEP:-"1"}); do cd ../; done; }

June 28, 2017, 5:40 p.m.alireza6677

1 score

Generate a sequence of numbers

 $ perl -e 'print "$_\n" for (1..10);'

May 30, 2017, 2:47 p.m.abhinickz6

1 score

List the content of a GitHub repository without cloning it

 $ svn ls https://github.com/user/repo/trunk/some/path

May 21, 2017, 6:01 p.m.Janos

1 score

Delete static and dynamic arp for /24 subnet

 $ for i in {1..254}; do arp -d 192.168.0.$i; done

Oct. 21, 2016, 5:07 a.m.dennyhalim.com