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

List docker log sizes and remind how to empty them

 $ docker ps -qa | xargs docker inspect --format='{{.LogPath}}' | xargs sudo du -hl; sleep 1; printf "\r\n  echo '' > \$(docker inspect --format={{.LogPath}} container_name_or_id) \r\n*****COPY****ABOVE****TO******CLEAR*****LOG*****CHANGE***CONTAINER_ID******* \r\n \n"

June 18, 2019, 8:06 p.m.lockntross

1 score

Find files/dirs modified within a given period

 $ find . -type d -newermt "2019-01-01" \! -newermt "2019-02-01" -exec ls -ld {} \;

May 20, 2019, 6:48 p.m.fenchu

1 score

Scan all open ports without any required program

 $ for i in {1..65535}; do (echo < /dev/tcp/127.0.0.1/$i) &>/dev/null && printf "\n[+] Open Port at\n: \t%d\n" "$i" || printf "."; done

May 8, 2019, 4:56 p.m.Goeks1

0 score

Extract audio only from video files using ffmpeg

 $ ffmpeg -i video.any -vn -acodec libvorbis audio.ogg

April 21, 2019, 10:55 p.m.Janos

1 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

1 score

Using tcpdump with port ranges and file count/size

 $ sudo /usr/sbin/tcpdump -i any -s 0 -n -Z <user_name> -C 500 -W 100 -w /home/<user_name>/$(hostname).pcap -f '(port (# or # or # or # or # or # or ...) or portrange <start>-<end>)' &>/dev/null

March 15, 2019, 1:02 a.m.peek2much3

1 score

Compare (diff) vim-generated backup of a file with that file's current version.

 $ diff~() { diff "$1"~ "$1"; }

March 12, 2019, 2:52 a.m.juanolopezgerena

1 score

Count the total number of hours of your music collection

 $ find . -print0 | xargs -0 -P 40 -n 1 sh -c 'ffmpeg -i "$1" 2>&1 | grep "Duration:" | cut -d " " -f 4 | sed "s/.$//" | tr "." ":"' - | awk -F ':' '{ sum1+=$1; sum2+=$2; sum3+=$3; sum4+=$4; if (sum4 > 100) { sum3+=1; sum4=0 }; if (sum3 > 60) { sum2+=1; sum3=0 }; if (sum2 > 60) { sum1+=1; sum2=0 } if (NR % 100 == 0) { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 } } END { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 }'

March 1, 2019, 9:09 p.m.pingiun

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

1 score

Clear the Exim Queue (remove all messages)

 $ exim -bp | exiqgrep -i | xargs exim -Mrm

Feb. 11, 2019, 8:58 a.m.Robert

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

1 score

Inspect the HTTP headers of a website

 $ curl -I amazon.com

Feb. 8, 2019, 10:56 p.m.Janos

1 score

Search man pages and present a PDF

 $ man -k . | awk '{ print $1 " " $2 }' | dmenu -i -p man | awk '{ print $2 " " $1 }' | tr -d '()' | xargs man -t | ps2pdf - - | zathura -

Dec. 18, 2018, 11:31 a.m.Jab2870

4 score

Find all log files modified 24 hours ago, and zip them

 $ find . -type f -mtime +1 -name "*.log" -exec zip -m {}.zip {} \; >/dev/null

Nov. 9, 2018, 10:04 a.m.TrongTan124

4 score

List IP addresses connected to your server on port 80

 $ netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head

Sept. 26, 2018, 11:10 p.m.Goeks1

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

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

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

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

1 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