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

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

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

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

2 score

Convert all flac files in dir to mp3 320kbps using ffmpeg

 $ for FILE in *.flac; do ffmpeg -i "$FILE" -b:a 320k "${FILE[@]/%flac/mp3}"; done;

Sept. 20, 2015, 5:45 p.m.Orkan

2 score

Generate a sequence of numbers

 $ for ((i=1; i<=10; ++i)); do echo $i; done

Nov. 4, 2014, 12:29 p.m.Janos

2 score

Generates random texts

 $ tr -dc a-z1-4 </dev/urandom | tr 1-2 ' \n' | awk 'length==0 || length>50' | tr 3-4 ' ' | sed 's/^ *//' | cat -s | fmt

July 31, 2014, 10:45 p.m.bkmeneguello

2 score

Parse nginx statistics output

 $ i=$(curl -s server/nginx_stats); IFS=$'\n'; i=($i); a=${i[0]/Active connections: } && a=${a/ }; r=${i[2]# [0-9]* [0-9]* }; echo "Active: $a, requests: $r"

June 20, 2014, 3:19 p.m.azat

2 score

Install profiling versions of all libghc dpkg packages

 $ sudo dpkg -l | grep libghc | grep "\-dev" | cut -d " " -f 3 | tr '\n' ' ' | sed -e 's/\-dev/\-prof/g' | xargs sudo apt-get install --yes

May 26, 2014, 1:14 a.m.openiduser146

2 score

Compute factorial of positive integer

 $ fac() { (echo 1; seq $1) | paste -s -d\* | bc; }

May 21, 2014, 10:55 p.m.jeroenjanssens

2 score

Have script run itself in a virtual terminal

 $ tty >/dev/null || { urxvt -hold -e "$0" "$@" & exit; }

March 6, 2014, 3:18 a.m.openiduser111

2 score

Remove .DS_Store from the repository you happen to staging by mistake

 $ find . -name .DS_Store -exec git rm --ignore-unmatch --cached {} +

Feb. 22, 2014, 9:45 a.m.Kuwana

2 score

Get mac address from default interface OS X

 $ netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {}  ifconfig {} | awk '/ether/ {print $2}'

Aug. 21, 2013, 10:28 a.m.spotmac

2 score

Dump network traffic with tcpdump to file with time-stamp in its filename

 $ date +"%Y-%m-%d_%H-%M-%Z" | xargs -I {} bash -c "sudo tcpdump -nq -s 0 -i eth0 -w ./dump-{}.pcap"

May 17, 2013, 8:03 p.m.kowalcj0

2 score

Remove files and directories whose name is a timestamp older than a certain time

 $ ls | grep '....-..-..-......' | xargs -I {} bash -c "[[ x{} < x$(date -d '3 days ago' +%Y-%m-%d-%H%M%S) ]] && rm -rfv {}"

May 7, 2013, 8:54 a.m.openiduser95

2 score

Unhide all hidden files in the current directory.

 $ find . -maxdepth 1 -type f -name '\.*' | sed -e 's,^\./\.,,' | sort | xargs -iname mv .name name

April 25, 2013, 7:46 a.m.openiduser93

2 score

Create a thumbnail from the first page of a PDF file

 $ convert -thumbnail x80 file.pdf[0] thumb.png

Feb. 6, 2013, 9:44 p.m.Janos

2 score

Create a visual report of the contents of a usb drive

 $ find /path/to/drive -type f -exec file -b '{}' \; -printf '%s\n' | awk -F , 'NR%2 {i=$1} NR%2==0 {a[i]+=$1} END {for (i in a) printf("%12u %s\n",a[i],i)}' | sort -nr

April 15, 2012, 6:04 a.m.Anon1Qa6UsYT

2 score

Send HTTP POST to a website with a file input field

 $ curl -L -v -F "value=@myfile" "http://domain.tld/whatever.php"

Feb. 15, 2012, 11:26 p.m.openiduser14

2 score

Rename all files in the current directory by capitalizing the first letter of every word in the filenames

 $ ls | perl -ne 'chomp; $f=$_; tr/A-Z/a-z/; s/(?<![.'"'"'])\b\w/\u$&/g; print qq{mv "$f" "$_"\n}'

Nov. 1, 2011, 12:51 p.m.Janos

2 score

Remove spaces recursively from all subdirectories of a directory

 $ find /path/to/dir -type d | tac | while read LINE; do target=$(dirname "$LINE")/$(basename "$LINE" | tr -d ' '); echo mv "$LINE" "$target"; done

Sept. 20, 2011, 4:52 p.m.Janos

2 score

Rename all files in a directory to lowercase names

 $ paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done

Aug. 5, 2011, 8:57 p.m.Janos

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