bashoneliners.com

Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We review and improve every contributed one-liner to make sure it is of high quality: useful, easy to read, follows best practices, with clear, detailed, accurate explanation. These one-liners should help you automate tasks, troubleshoot problems, whether it be in system administration, file management, networking or programming.

11

Show 10 Largest Open Files

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

February 28, 2014cellojoe

8

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")}'

October 16, 2016uMt

8

Generate a sequence of numbers, zero-padded

echo {01..10}

March 1, 2015Elkku

7

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, 2014cesp

6

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

August 13, 2013shavenwarthog

6

Rename all items in a directory to lower case

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

April 20, 2013EvaggelosBalaskas

5

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-

February 8, 2015ratchode

5

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

September 4, 2014Genunix

5

Show dd status every so often

watch --interval 5 killall -USR1 dd

December 6, 2012FoxWilson

4

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

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

November 9, 2018TrongTan124

4

List top 10 IP addresses connected to your server on port 80 with the number of connections

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

September 26, 2018Goeks1

4

Ternary conditional clause

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

January 22, 2018dhsrocha

4

Open another terminal at current location

$TERMINAL & disown

July 18, 2017Jab2870

4

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, 2015openiduser187

4

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

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

February 28, 2015Elkku

4

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"' -- {} \;

November 20, 2014bashoneliners

4

Tree-like output in ls

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

April 26, 2013clitips

3

Run multiple commands chained with && and || using a single sudo

sudo -s <<< 'apt update -y && apt upgrade -y'

October 27, 2019secure-debian

3

Get executed script's current working directory

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

January 22, 2018dhsrocha

3

Random Git Commit

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

January 5, 2018Jab2870

3

Generate a sequence of numbers using a simple for loop

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

November 4, 2014bashoneliners

3

Compute factorial of positive integer

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

May 21, 2014jeroenjanssens

3

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, 2014n00tz

3

Extract your external IP address using dig

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

February 25, 2014bashoneliners