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 the 10 largest open files

lsof / | awk '$7 > 1048576 { print $7 / 1048576 "MB", $9, $1 }' | sort -nu | tail

February 28, 2014cellojoe

8

List status of all Git repositories

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

October 16, 2016uMt

8

Generate a sequence of numbers, zero-padded

echo {01..10}

March 1, 2015Elkku

7

Display the number of connections per IP to port 80

while true; do clear; date; echo; echo "[Count] | [IP ADDR]"; echo "-------------------"; netstat -n | grep ':80\>' | awk '! /LISTEN/ {print $5}' | cut -d: -f1 | uniq -c; sleep 5; done

April 9, 2014cesp

7

Rename all items in a directory to lowercase names

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

April 20, 2013EvaggelosBalaskas

6

Convert directory of videos to MP4 in parallel

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

August 13, 2013shavenwarthog

5

Almost ternary conditional clause

command1 && command2 || command3

January 22, 2018dhsrocha

5

Nmap scan every interface that is assigned an IP

ifconfig -a | perl -lne 'print $_ for /\b(?!255)(?:\d{1,3}\.){3}(?!255)\d{1,3}\b/g' | xargs nmap -A -p0-

February 8, 2015ratchode

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

Perform git commit with a random message from whatthecommit.com

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

January 5, 2018Jab2870

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

Copy the output of a command to clipboard for easy pasting with Command-V in OSX

echo "Example command output..." | 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 the absolute path of the current script's working directory

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

January 22, 2018dhsrocha

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 'text to match' {} \; > matches.txt

April 14, 2014n00tz

3

Extract your external IP address using dig

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

February 25, 2014bashoneliners

3