Perl one-liners are small Perl programs that are run directly from a command line (ex. on Unix/Linux, Cygwin). For a deeper dive see Famous Perl One-Liners Explained. If you want a book, have a look at Minimal Perl for UNIX and Linux People.

Perl command line switches

See perlrun for more.

Unix tools replacements

grep replacement

sed replacement

awk replacement

dos2unix replacement


Total size of files (using the Eskimo Greeting Operator as suggested by PerlMonks (-:)

find /opt/splunk/syslog/ -iname "*log*" -type f -mtime +30 | perl -lne '$sum += (stat)[7]}{print $sum'

Find big palindromes

perl -lne 'print if $_ eq reverse and length >= 5' /usr/share/dict/words

Print a file system tree on UNIX like systems (source)

ls -R | perl -ne'if(s/:$//){s{[^/]*/}{--}g;s/^-/\t|/;print}'

Remove comments and compress all consecutive blank lines into one (more)

cat /etc/ssh/sshd_config | perl -nle '!/^#/ and print' | perl -00 -pe ''

Create HTML anchor element

perl -le 'print "<a href=\"$ARGV[1]\">$ARGV[0]</a>"' 'Perl programming documentation'

Greet user (stolen from Utilitarian :-) )

perl -E 'say "Good " . qw(night morning afternoon evening)[(localtime)[2]/6] . ", $ENV{USER}!"'



PerlOneLiners (last edited 2016-09-27 09:48:35 by JozefReisinger)