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

Show 10 Largest Open Files

 $ lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB" " " $9 " " $1 }' | sort -n -u | tail

Feb. 28, 2014, 3:34 a.m.cellojoe

8 score

List status of all GIT repos

 $ find ~ -name ".git" 2> /dev/null | sed 's/\/.git/\//g' | awk '{print "-------------------------\n\033[1;32mGit Repo:\033[0m " $1; system("git --git-dir="$1".git --work-tree="$1" status")}'

Oct. 16, 2016, 11:19 p.m.uMt

8 score

Generate a sequence of numbers

 $ echo {01..10}

March 1, 2015, 12:04 a.m.Elkku

7 score

Displays the quantity of connections to port 80 on a per IP basis

 $ clear;while x=0; do clear;date;echo "";echo "  [Count] | [IP ADDR]";echo "-------------------";netstat -np|grep :80|grep -v LISTEN|awk '{print $5}'|cut -d: -f1|uniq -c; sleep 5;done

April 9, 2014, 5:49 a.m.cesp

6 score

Convert directory of videos to MP4 in parallel

 $ for INPUT in *.avi ; do echo "${INPUT%.avi}" ; done | xargs -i -P9  HandBrakeCLI -i "{}".avi -o "{}".mp4

Aug. 13, 2013, 5:10 a.m.shavenwarthog

5 score

Nmap scan every interface that is assigned an IP

 $ ifconfig -a | grep -Po '\b(?!255)(?:\d{1,3}\.){3}(?!255)\d{1,3}\b' | xargs nmap -A -p0-

Feb. 8, 2015, 2:11 a.m.ratchode

5 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

Sept. 4, 2014, 3:44 p.m.Genunix

5 score

Rename all items in a directory to lower case

 $ for i in *; do mv "$i" "${i,,}"; done

April 20, 2013, 9:41 p.m.EvaggelosBalaskas

5 score

Show dd status every so often

 $ watch --interval 5 killall -USR1 dd

Dec. 6, 2012, 6:16 p.m.FoxWilson

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

4 score

Ternary conditional clause

 $ [ test_statement ] && ( then_statement ) || ( else_statement );

Jan. 22, 2018, 5:27 p.m.dhsrocha

4 score

Open another terminal at current location

 $ $TERMINAL & disown

July 18, 2017, 3:04 p.m.Jab2870

4 score

Ban all IPs that attempted to access phpmyadmin on your site

 $ grep "phpmyadmin" $path_to_access.log | grep -Po "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" | sort | uniq | xargs -I% sudo iptables -A INPUT -s % -j DROP

April 2, 2015, 8:58 a.m.openiduser187

4 score

Run a command and copy its output to clipboard (Mac OSX)

 $ echo "Here comes the output of my failing code" | tee >(pbcopy)

Feb. 28, 2015, 11:53 p.m.Elkku

4 score

Change the encoding of all files in a directory and subdirectories

 $ find . -type f  -name '*.java' -exec sh -c 'iconv -f cp1252 -t utf-8 "$1" > converted && mv converted "$1"' -- {} \;

Nov. 20, 2014, 12:15 p.m.Janos

4 score

Tree-like output in ls

 $ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

April 26, 2013, 1:37 p.m.clitips

3 score

Get executed script's current working directory

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

Jan. 22, 2018, 4:55 p.m.dhsrocha

3 score

Random Git Commit

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

Jan. 5, 2018, 4:55 p.m.Jab2870

3 score

Generate a sequence of numbers

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

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

3 score

Compute factorial of positive integer

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

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

3 score

Find all files recursively with specified string in the filename and output any lines found containing a different string.

 $ find . -name *conf* -exec grep -Hni 'matching_text' {} \; > matching_text.conf.list

April 14, 2014, 8:23 p.m.n00tz

3 score

Extract your external IP address using dig

 $ dig +short myip.opendns.com @resolver1.opendns.com

Feb. 25, 2014, 7:50 a.m.Janos

3 score

Remove offending key from known_hosts file with one swift move

 $ ssh-keygen -R <hostname>

Jan. 25, 2014, 1:35 p.m.openiduser126

3 score

Convert a music file (mp3) to a mp4 video with a static image

 $ ffmpeg -loop_input -i cover.jpg -i soundtrack.mp3 -shortest -acodec copy output_video.mp4

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