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.

Check if a version string is in valid SemVer format

re_semver=...; perl -wln -e "/$re_semver/ or exit(1)" <<< "$version"

October 4, 2023bashoneliners

Generate a sequence of numbers, one number per line

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

May 30, 2017abhinickz6

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

Counting the number of commas in CSV format

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

December 1, 2013bashoneliners

Find distinct file extensions in a folder

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

May 17, 2013kowalcj0

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

Rename files with numeric padding

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

October 6, 2012bashoneliners

Group count sort a log file

A=$(FILE=/var/log/myfile.log; cat $FILE | perl -p -e 's/.*,([A-Z]+)[\:\+].*/$1/g' | sort -u | while read LINE; do grep "$LINE" $FILE | wc -l | perl -p -e 's/[^0-9]+//g'; echo -e "\t$LINE"; done;);echo "$A"|sort -nr

January 31, 2012openiduser14

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