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

View specific column of data from a large file with long lines

 $ cat /tmp/log.data |colrm 1 155|colrm 60 300

Oct. 4, 2011, 9:55 p.m.versorge

1 score

Replace a regexp pattern in many files at once

 $ vi +'bufdo %s/pattern/replacement/g | update' +q $(grep -rl pattern /path/to/dir)

Sept. 15, 2011, 11:50 p.m.Janos

-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

Get a free shell account on a community server

 $ sh <(curl hashbang.sh | gpg)

March 15, 2015, 9:49 a.m.lrvick

-1 score

Run a local shell script on a remote server without copying it there

 $ ssh user@server bash < /path/to/local/script.sh

June 21, 2012, 12:06 a.m.Janos

0 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

0 score

Date March-18-2019_21h-11m-02s Month-DD-YYYY HH MM SS

 $ date  +"%B-%d-%Y_%Hh-%Mm-%S"s

March 19, 2019, 2:13 a.m.Julien_Tremblay_McLellan

0 score

Print the list of your Git commits this month

 $ git log --since='last month' --author="$(git config user.name)" --oneline

Feb. 25, 2019, 6:57 p.m.Janos

0 score

Store the output of find in an array

 $ mapfile -d $'\0' arr < <(find /path/to -print0)

Feb. 18, 2019, 7:48 p.m.Janos

0 score

Check if a text snippet is valid C code

 $ gcc -fsyntax-only -xc - <<< "text snippet"

Feb. 10, 2019, 8:12 a.m.Janos

0 score

While loop to pretty print system load (1, 5 & 15 minutes)

 $ while :; do date; awk '{printf "1 minute load: %.2f\n", $1; printf "5 minute load: %.2f\n", $2; printf "15 minute load: %.2f\n", $3}' /proc/loadavg; sleep 3; done

Sept. 5, 2018, 8:41 p.m.Janos

0 score

Scan entire Git repos for dangerous Amazon Web Service IDs

 $ git ls-tree --full-tree -r --name-only HEAD | xargs egrep -w '[A-Z0-9]{20}'

Aug. 31, 2018, 10:29 p.m.johntellsall

0 score

While loop to pretty print system load (1, 5 & 15 minutes)

 $ while [ 1 == 1 ]; do  cat /proc/loadavg | awk '{printf "1 minute load: %.2f\n", $(NF-5)}' && cat /proc/loadavg |awk '{printf "5 minute load: %.2f\n", $(NF-3)}' && cat /proc/loadavg |awk '{printf "15 minute load: %.2f\n", $(NF-2)}'; sleep 3; date; done

Aug. 30, 2018, 8:54 a.m.peek2much3

0 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

0 score

Organise image by portrait and landscape

 $ mkdir "portraits"; mkdir "landscapes"; for f in ./*.jpg; do WIDTH=$(identify -format "%w" "$f")> /dev/null; HEIGHT=$(identify -format "%h" "$f")> /dev/null; if [[ "$HEIGHT" > "$WIDTH" ]]; then mv "$f" portraits/ ; else mv "$f" landscapes/ ; fi; done

Aug. 23, 2018, 2:09 p.m.Jab2870

0 score

Create a txt files with 10000 rows

 $ for FILE in *.full ; do split -l 100000 $FILE; mv -f xaa `echo "$FILE" | cut -d'.' -f1`.txt; rm -f x*; done

Aug. 22, 2018, 2:02 p.m.Kifli88

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

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

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