bashoneliners.com

Welcome to bashoneliners.com, a curated collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We aim to make each published one-liner to be 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.

Print lines of a file that don't exist in another file, ignoring the first two fields

awk -F'|' 'NR == FNR { $1 = ""; $2 = ""; seen[$0]++ } NR != FNR { orig = $0; $1 = ""; $2 = ""; if (!seen[$0]) print orig }' first.txt second.txt

November 27, 2023bashoneliners

Print .bash_history with epoch timestamps converted to human-readable dates

awk '/^#[0-9]*$/ {split($0, arr, "#"); print "#", strftime("%c", arr[2]); getline; print }' < /path/to/.bash_history

March 10, 2020joeashcraft

Search man pages and present a PDF

man -k . | awk '{ print $1, $2 }' | dmenu -i -p man | awk '{ print $2, $1 }' | tr -d '()' | xargs man -t | ps2pdf - - | zathura -

December 18, 2018Jab2870

Generate a random number of up to 6 digits

python -c 'import random; print(random.randint(0, 10**6 - 1))'

September 19, 2018johntellsall

List open processes ordered by it's number of open files

ps -ef |awk '{ print $2 }' \ 	|tail -n +2 \ 	|while read pid; do echo "$pid	$(lsof -p $pid |wc -l)"; done \ 	|sort -r -n -k 2 \ 	|while read pid count; do echo "$pid	$count	$(ps -o command= -p $pid)"; done

August 22, 2018cddr

Generate a sequence of numbers, one number per line

perl -e 'print "$_\n" for (1..10)'

May 30, 2017abhinickz6

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

Shuffle lines

... | perl -MList::Util=shuffle -e 'print shuffle <>;'

January 31, 2016openiduser81

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

Shuffle lines

... | perl -MList::Util -e 'print List::Util::shuffle <>'

October 25, 2014bashoneliners

Show the 10 largest open files

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

February 28, 2014cellojoe

Counting the number of commas in CSV format

perl -ne 'print tr/,//, "\n"' < file.csv | sort -u

December 1, 2013bashoneliners

Count the lines of each file extension in a list of files

git ls-files | xargs wc -l | awk -F ' +|\\.|/' '{ sumlines[$NF] += $2 } END { for (ext in sumlines) print ext, sumlines[ext] }'

November 9, 2013bashoneliners

Get mac address from default interface OS X

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

August 21, 2013spotmac

Insert lines from one text file to another one

awk 'NR % 10 == 1 {getline f2 < "file1"; print f2} 1' file2 | cat -n

June 22, 2013openiduser102

Find distinct file extensions in a folder

find . -type f | perl -ne 'print $1 if /\.([^.\/]+)$/' | sort -u

May 17, 2013kowalcj0

Get streamed FLV from Chrome with lsof

export psid=$(pgrep -f libflashplayer.so); cp /proc/$psid/fd/$(lsof -p $psid | grep eleted | awk {' print $4 '} | sed -e "s/[a-z]//g") saved.flv

May 11, 2013GNA

Get only the latest version of a file from across mutiple directories

find . -name custlist\* | perl -ne '$path = $_; s?.*/??; $name = $_; $map{$name} = $path; ++$c; END { print $map{(sort(keys(%map)))[$c-1]} }'

February 23, 2013bashoneliners

Get load average in a more parse-able format

python -c 'import os; print os.getloadavg()[0]'

January 5, 2013FoxWilson

Rename files with numeric padding

perl -e 'for (@ARGV) { $o = $_; s/\d+/sprintf("%04d", $&)/e; print qq{mv "$o" "$_"\n}}'

October 6, 2012bashoneliners

Find all the unique 4-letter words in a text

cat ipsum.txt | perl -ne 'print map("$_\n", m/\w+/g);' | tr A-Z a-z | sort | uniq | awk 'length($1) == 4 {print}'

January 29, 2012bashoneliners

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

November 1, 2011bashoneliners

Find the most recently modified files in a directory and all subdirectories

find /path/to/dir -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail

October 4, 2011janos