bashoneliners.com

Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We review and improve every contributed one-liner to make sure it is of high quality: useful, easy to read, follows best practices, with clear, detailed, accurate explanation. These one-liners should help you automate tasks, troubleshoot problems, whether it be in system administration, file management, networking or programming.

0

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;

August 17, 2018gatero

0

Find and replace string inside specific files

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

August 17, 2018gatero

0

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, 2018cjedwa

0

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, 2018kazatca

2

Kill a process running on port 8080

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

June 15, 2018jamestomasino

1

Delete all untagged Docker images

docker images -f dangling=true -q | xargs --no-run-if-empty docker rmi

June 15, 2018penguincoder

1

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, 2018ManuViorel

1

Delete all untagged Docker images

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

April 27, 2018stefanobaghino

1

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, 2018openiduser111

2

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, 2018pratham2003

2

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

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

February 19, 2018stefanobaghino

2

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"

February 9, 2018stefanobaghino

0

Source without circular reference

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

January 24, 2018dhsrocha

5

Almost ternary conditional clause

command1 && command2 || command3

January 22, 2018dhsrocha

3

Get the absolute path of the current script's working directory

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

January 22, 2018dhsrocha

4

Perform git commit with a random message from whatthecommit.com

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

January 5, 2018Jab2870

1

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

November 14, 2017olshek_

0

Remove new lines from files and folders

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

September 30, 2017moverperfect

2

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

September 14, 2017FoxBuru

1

Kill a process running on port 8080

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

September 1, 2017bashoneliners

1

Kill a process running on port 8080

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

August 18, 2017kimbethwel

1

Get the latest Arch Linux news

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

August 15, 2017Jab2870

2

Make a new folder and cd into it.

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

August 3, 2017PrasannaNatarajan

1

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, 2017Jab2870

1

Go up to a particular folder

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

July 18, 2017Jab2870