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

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

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

Big CSV > batches > JSON array > CURL POST data with sleep

 $ cat post-list.csv | split -l 30 - --filter='jq -R . | jq --slurp -c .' | xargs -d "\n" -I % sh -c 'curl -H "Content-Type: application/json" -X POST -d '"'"'{"type":1,"entries":%}'"'"' http://127.0.0.1:8080/purge-something && sleep 30'

March 7, 2018, 12:12 p.m.pratham2003

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 '{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

1 score

Shuffle lines

 $ ... | perl -MList::Util=shuffle -e 'print shuffle <>;'

Jan. 31, 2016, 9:02 p.m.openiduser81

1 score

Convert all flac files in dir to mp3 320kbps using ffmpeg

 $ for FILE in *.flac; do ffmpeg -i "$FILE" -b:a 320k "${FILE[@]/%flac/mp3}"; done;

Sept. 20, 2015, 5:45 p.m.Orkan

1 score

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

 $ upup(){ DEEP=$1; [ -z "${DEEP}" ] && { DEEP=1; }; for i in $(seq 1 ${DEEP}); do cd ../; done; }

June 9, 2015, 3:09 p.m.andreaganduglia

1 score

Get number of all Python Behave scenarios (including all examples from Scenario Outlines)

 $ behave -d | grep "scenarios passed" | cut -d, -f4 | sed -e 's/^[[:space:]]*//' | sed 's/untested/scenarios/g'

April 17, 2015, 2:21 p.m.openiduser188

1 score

Print a flat list of dependencies of a Maven project

 $ mvn dependency:list | sed -ne s/..........// -e /patterntoexclude/d -e s/:compile//p -e s/:runtime//p | sort | uniq

Sept. 22, 2014, 9:02 p.m.Janos

1 score

Open Windows internet shortcut (*.url) files in firefox

 $ grep -i url='*' file.url | cut -b 5- | xargs firefox

Sept. 12, 2014, 12:06 a.m.tsjswimmer

1 score

Remove all at jobs

 $ atq | sed 's_\([0-9]\{1,8\}\).*_\1_g' | xargs atrm

Sept. 10, 2014, 9:56 a.m.laurip

1 score

Deletes orphan 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

Sept. 2, 2014, 6:51 p.m.rafaeln

1 score

Find recent logs that contain the string "Exception"

 $ find . -name '*.log' -mtime -2 -exec grep -Hc Exception {} \; | grep -v :0$

July 19, 2014, 7:53 a.m.Janos