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

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

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

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

0 score

Remove new lines from files and folders

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

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

0 score

Get the HTTP status code of a URL

 $ curl -Lw '%{http_code}' -s -o /dev/null -I SOME_URL

June 19, 2017, 11:15 p.m.Janos

0 score

Corporate random bullshit generator (cbsg)

 $ curl -s http://cbsg.sourceforge.net/cgi-bin/live | grep -Eo '^<li>.*</li>' | sed s,\</\\?li\>,,g | shuf -n 1 | cowsay

June 7, 2017, 4:11 p.m.Jab2870

0 score

Create an array of CPU frequencies in GHz

 $ cpus=($({ echo scale=2; awk '/cpu MHz/ {print $4 " / 1000"}' /proc/cpuinfo; } | bc))

Dec. 28, 2015, 9:02 p.m.openiduser146

0 score

Test git archive before actually creating an archive // fake dry run

 $ git archive master some/project/subdir | tar t

Dec. 22, 2015, 2:29 p.m.openiduser146

0 score

Shuffle lines

 $ seq 5 | shuf

March 12, 2015, 7:58 a.m.openiduser184

0 score

Print the window title of current mpv session to display what is playing

 $ wmctrl -pl | grep $(pidof mpv) | cut -d- -f2-

Dec. 15, 2014, 3:37 a.m.openiduser171

0 score

Shuffle lines

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

Oct. 25, 2014, 10:40 p.m.Janos

0 score

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

 $ firefox $(grep -i ^url='*' file.url | cut -b 5-)

Sept. 11, 2014, 10:03 a.m.tsjswimmer

0 score

Check if a file exists and has a size greater than X

 $ [[ $(find /path/to/file -type f -size +51200c 2>/dev/null) ]] && echo true || echo false

Jan. 9, 2014, 12:34 p.m.Janos

0 score

Replace sequences of the same characters with a single character

 $ echo heeeeeeelllo | sed 's/\(.\)\1\+/\1/g'

Dec. 11, 2013, 7:58 p.m.Janos

0 score

Counting the number of commas in CSV format

 $ perl -ne 'print tr/,//, "\n"' < file.csv | sort -u

Dec. 1, 2013, 1:03 p.m.Janos

0 score

Count the lines of each file extension in a list of files

 $ git ls-files | xargs wc -l | awk -F ' +|\\.|/' '{ sumlines[$NF] += $2 } END { for (ext in sumlines) print ext, sumlines[ext] }'

Nov. 9, 2013, 11:49 a.m.Janos

0 score

Add all unknown files in a Subversion checkout

 $ svn add . --force

Sept. 24, 2013, 7:59 a.m.Janos

0 score

Find files that are not executable

 $ find /some/path -type f ! -perm -111 -ls

Sept. 18, 2013, 9:14 p.m.Janos

0 score

Create a heap dump of a Java process

 $ jmap -dump:format=b,file=/var/tmp/dump.hprof 1234

July 8, 2013, 8:18 a.m.Janos

0 score

Insert lines from one text file to another one

 $ awk 'NR % 10 == 1 {getline f2 < "file1"; print f2} 1' file2 | cat -n

June 22, 2013, 9:30 a.m.openiduser102

0 score

Check that a directory is a parent of another

 $ is_parent() { [[ "$2" =~ $1/? ]]; }

June 13, 2013, 11:03 p.m.Couannette

0 score

Create fattal tone mapped images from a directory of raw images

 $ for img in /path/to/rawimages/*.RW2; do pfsin ${img} | pfssize -x 1024 -y 768 | pfstmo_fattal02 -v -s 1 | pfsout /path/to/finished/${img%%}.jpg; done

June 3, 2013, 10:45 p.m.mmaki