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

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

Generate a sequence of numbers

 $ echo {01..10}

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

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

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

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

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

4 score

Show dd status every so often

 $ watch --interval 5 killall -USR1 dd

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

3 score

Ternary conditional clause

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

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

3 score

Open another terminal at current location

 $ $TERMINAL & disown

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

3 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

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

3 score

Remove offending key from known_hosts file with one swift move

 $ sed -i 18d .ssh/known_hosts

Jan. 16, 2013, 2:29 p.m.EvaggelosBalaskas

2 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

2 score

Kill a process running on port 8080

 $ lsof -i :8080 | awk '{l=$2} END {print l}' | xargs kill

June 15, 2018, 4:18 a.m.jamestomasino

2 score

Big CSV > batches > JSON array > CURL POST data with sleep

 $ cat post-list.csv | split -l 30 - --filter='jq -R . | jq --slurp -c .' | xargs -d "\n" -I % sh -c 'curl -H "Content-Type: application/json" -X POST -d '"'"'{"type":1,"entries":%}'"'"' http://127.0.0.1:8080/purge-something && sleep 30'

March 7, 2018, 12:12 p.m.pratham2003