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.
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

-1 score

Delete all untagged Docker images

 $ docker rmi $(docker images -f "dangling=true" -q)

April 27, 2018, 2:50 p.m.stefanobaghino

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

2 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

2 score

List all packages with at least a class defined in a JAR file

 $ jar tf "$1" | grep '/.*\.class$' | xargs dirname | sort -u | tr / .

Feb. 19, 2018, 12:13 p.m.stefanobaghino

2 score

Output an arbitrary number of open TCP or UDP ports in an arbitrary range

 $ comm -23 <(seq "$FROM" "$TO") <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | shuf | head -n "$HOWMANY"

Feb. 9, 2018, 3:51 p.m.stefanobaghino

0 score

Source without circular reference

 $ [ ! "${LIB}" ] && ( readonly LIB; . "${ $( cd $( dirname $0 ) && pwd ) }/<path_to>/LIB.sh" )

Jan. 24, 2018, 4:30 p.m.dhsrocha

3 score

Ternary conditional clause

 $ [ test_statement ] && ( then_statement ) || ( else_statement );

Jan. 22, 2018, 5:27 p.m.dhsrocha

2 score

Get executed script's current working directory

 $ CWD=$(cd "$(dirname "$0")" && pwd)

Jan. 22, 2018, 4:55 p.m.dhsrocha

2 score

Random Git Commit

 $ git commit -m "$(w3m whatthecommit.com | head -n 1)"

Jan. 5, 2018, 4:55 p.m.Jab2870

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_

0 score

Remove new lines from files and folders

 $ rename 's/[\r\n]//g' *

Sept. 30, 2017, 10:07 p.m.moverperfect

2 score

Retrieve dropped connections from firewalld journaling

 $ sudo journalctl -b | grep -o "PROTO=.*" | sed -r 's/(PROTO|SPT|DPT|LEN)=//g' | awk '{print $1, $3}' | sort | uniq -c

Sept. 14, 2017, 5:10 p.m.FoxBuru

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

2 score

Make a new folder and cd into it.

 $ mkcd(){ NAME=$1; mkdir -p "$NAME"; cd "$NAME"; }

Aug. 3, 2017, 6:49 a.m.PrasannaNatarajan

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