Commands using perl (368)

  • Probably only works with GNU du and modern perls. Show Sample Output

    du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e'
    mrtheplague · 2009-02-20 06:11:31 3
  • parse `lsmod' output and pass to `dot' drawing utility then finally pass it to an image viewer

    lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -
    bandie91 · 2011-11-27 14:02:35 12
  • OK, not the most useful but a good way to impress friends. Requires the "display" command from ImageMagick.

    perl -e 'print "P1\n256 256\n", map {$_&($_>>8)?1:0} (0..0xffff)' | display
    dstahlke · 2009-07-08 17:50:23 7
  • This one-liner will the *delete* without any further confirmation all 100% duplicates but one based on their md5 hash in the current directory tree (i.e including files in its subdirectories). Good for cleaning up collections of mp3 files or pictures of your dog|cat|kids|wife being present in gazillion incarnations on hd. md5sum can be substituted with sha1sum without problems. The actual filename is not taken into account-just the hash is used. Whatever sort thinks is the first filename is kept. It is assumed that the filename does not contain 0x00. As per the good suggestion in the first comment, this one does a hard link instead: find . -xdev -type f -print0 | xargs -0 md5sum | sort | perl -ne 'chomp; $ph=$h; ($h,$f)=split(/\s+/,$_,2); if ($h ne $ph) { $k = $f; } else { unlink($f); link($k, $f); }' Show Sample Output

    find . -type f -print0|xargs -0 md5sum|sort|perl -ne 'chomp;$ph=$h;($h,$f)=split(/\s+/,$_,2);print "$f"."\x00" if ($h eq $ph)'|xargs -0 rm -v --
    masterofdisaster · 2009-06-07 03:14:06 2
  • This function takes a word or a phrase as arguments and then fetches definitions using Google's "define" syntax. The "nl" and perl portion isn't strictly necessary. It just makes the output a bit more readable, but this also works: define(){ local y="$@";curl -sA"Opera" "${y// /+}"|grep -Po '(?<=<li>)[^<]+';} If your version of grep doesn't have perl compatible regex support, then you can use this version: define(){ local y="$@";curl -sA"Opera" "${y// /+}"|grep -Eo '<li>[^<]+'|sed 's/<li>//g'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;} Show Sample Output

    define(){ local y="$@";curl -sA"Opera" "${y// /+}"|grep -Po '(?<=<li>)[^<]+'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;}
    eightmillion · 2010-01-29 05:01:11 5
  • tail with coloured output with the help of perl - need more colours? here is a colour table: Show Sample Output

    tail -f FILE | perl -pe 's/KEYWORD/\e[1;31;43m$&\e[0m/g'
    tuxifier · 2009-06-02 21:31:54 5
  • Can be run as a script `ftrace` if my_command is substrituted with "$@" It is useful when running a command that fails and you have the feeling it is accessing a file you are not aware of. Show Sample Output

    strace -ff -e trace=file my_command 2>&1 | perl -ne 's/^[^"]+"(([^\\"]|\\[\\"nt])*)".*/$1/ && print'
    unixmonkey8046 · 2011-08-16 15:00:18 2
  • Print out list of all branches with last commit date to the branch, including relative time since commit and color coding. Show Sample Output

    for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r
    brunost · 2009-06-03 08:25:00 55

  • 15
    perl -le 'print $!+0, "\t", $!++ for 0..127'
    voidpointer · 2010-01-12 17:33:50 4
  • attribution: Thanks to repellent on source:

    cat typescript | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > typescript-processed
    unixmonkey3987 · 2009-05-28 21:02:17 5
  • works on Linux and Solaris. I think it will work on nearly all *nix-es Show Sample Output

    ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'
    sneaker · 2009-07-31 09:49:17 4
  • This is a command that I find myself using all the time. It works like regular grep, but returns the paragraph containing the search pattern instead of just the line. It operates on files or standard input. grepp <PATTERN> <FILE> or <SOMECOMMAND> | grepp <PATTERN> Show Sample Output

    grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
    eightmillion · 2010-01-12 04:30:15 5
  • Fetches the IPs and ONLY the IPs from ifconfig. Simplest, shortest, cleanest. Perl is too good to be true... (P.S.: credit should go to Peteris Krumins at Show Sample Output

    ifconfig | perl -nle'/dr:(\S+)/ && print $1'
    xsawyerx · 2009-07-23 09:33:31 4
  • Silly Perl variant.

    perl -e '$|++; while (1) { print " " x (rand(35) + 1), int(rand(2)) }'
    MarxBro · 2012-09-27 22:46:39 5
  • the addition of ".bk" to the regular "pie" idiom makes perl create a backup of every file with the extension ".bk", in case it b0rks something and you want it back

    perl -pi.bk -e's/foo/bar/g' file1 file2 fileN
    xsawyerx · 2009-01-29 09:51:11 28
  • change the *.avi to whatever you want to match, you can remove it altogether if you want to check all files.

    find -type f -name "*.avi" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
    grokskookum · 2009-09-24 15:50:39 4
  • My Programming Languages professor assigned my class a homework assignment where we had to write a Perl interpreter using Perl. I really like Python's interactive command line interpreter which inspired this Perl script.

    perl -e 'while(1){print"> ";eval<>}'
    kzh · 2010-02-03 21:48:33 2
  • Fun idea! This one adds seconds and keeps running on the same line. Perl's probably cheating though. :)

    perl -e 'for(;;){@d=split("",`date +%H%M%S`);print"\r";for(0..5){printf"%.4b ",$d[$_]}sleep 1}'
    putnamhill · 2010-12-04 00:05:54 1
  • The Linux /dev/full file simulates a "disk full" condition, and can be used to verify how a program handles this situation. In particular, several programming language implementations do not print error diagnostics (nor exit with error status) when I/O errors like this occur, unless the programmer has taken additional steps. That is, simple code in these languages does not fail safely. In addition to Perl, C, C++, Tcl, and Lua (for some functions) also appear not to fail safely. Show Sample Output

    perl -e 'print 1, 2, 3' > /dev/full
    mkc · 2009-02-19 17:08:13 2
  • The above one-liner could be run against all HTML files in a directory. It renames the HTML files based on the text contained in their title tag. This helped me in a situation where I had a directory containing thousands of HTML documents with meaningless filenames.

    perl -wlne'/title>([^<]+)/i&&rename$ARGV,"$1.html"' *.html
    mhs · 2010-12-29 05:39:41 1

  • 9
    perl -e 'for(;;sleep 1){printf"\r"."%.4b "x6,split"",`date +%H%M%S`}'
    unixmonkey74668 · 2015-04-26 08:48:20 0
  • Each shell function has its own summary line, as a comment. If there are multiple shell functions with the same name, the function with the highest number of votes is put into the file. Note: added 'grep -v' to the end of the pipeline, to eliminate extraneous lines containing only '--'. Thanks to matthewbauer for pointing this out.

    export QQ=$(mktemp -d);(cd $QQ; curl -s -O[0-2400:25];for i in $(perl -ne 'print "$1\n" if( /^(\w+\(\))/ )' *|sort -u);do grep -h -m1 -B1 $i *; done)|grep -v '^--' >;rm -r $QQ
    bartonski · 2010-01-30 19:47:42 3
  • Based on the execute with timeout command in this site. A more complex script: #!/bin/sh # This script will check the avaliability of a list of NFS mount point, # forcing a remount of those that do not respond in 5 seconds. # # It basically does this: # NFSPATH=/mountpoint TIMEOUT=5; perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $NFSPATH" || (umount -fl $NFSPATH; mount $NFSPATH) # TIMEOUT=5 SCRIPT_NAME=$(basename $0) for i in $@; do echo "Checking $i..." if ! perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $i" > /dev/null 2>&1; then echo "$SCRIPT_NAME: $i is failing with retcode $?."1>&2 echo "$SCRIPT_NAME: Submmiting umount -fl $i" 1>&2 umount -fl $i; echo "$SCRIPT_NAME: Submmiting mount $i" 1>&2 mount $i; fi done

    NFSPATH=/mountpoint TIMEOUT=5; perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $NFSPATH" || (umount -fl $NFSPATH; mount $NFSPATH)
    keymon · 2010-06-04 07:59:00 1
  • Just want to post a Perl alternative. Does not count hidden files ('.' ones). Show Sample Output

    perl -le 'print ~~ map {-s} <*>'
    MarxBro · 2012-02-21 21:09:48 0
  • is a collection of funny quotes from IRC. WARNING: some of the quotes contain "adult" jokes... may be embarrassing if your boss sees them... Thanks to Chen for the idea and initial version! This script downloads a page with random quotes, filters the html to retrieve just one liners quotes and outputs the first one. Just barely under the required 255 chars :) Improvment: You can replace the head -1 at the end by: awk 'length($0)>0 {printf( $0 "\n%%\n" )}' > bash_quotes.txt which will separate the quotes with a "%" and place it in the file. and then: strfile bash_quotes.txt which will make the file ready for the fortune command and then you can: fortune bash_quotes.txt which will give you a random quote from those in the downloaded file. I download a file periodically and then use the fortune in .bashrc so I see a funny quote every time I open a terminal. Show Sample Output

    curl -s|grep -oE "<p class=\"quote\">.*</p>.*</p>"|grep -oE "<p class=\"qt.*?</p>"|sed -e 's/<\/p>/\n/g' -e 's/<p class=\"qt\">//g' -e 's/<p class=\"qt\">//g'|perl -ne 'use HTML::Entities;print decode_entities($_),"\n"'|head -1
    Iftah · 2009-05-07 13:13:21 6
  •  1 2 3 >  Last ›

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Check These Out

Base conversions with bc
Easily convert numbers to their representations in different bases. Passing "ibase=16; obase=8; F2A" to bc will convert F2A (3882 in decimal) from Hex to Octal, and so on.

An easter egg built into python to give you the Zen of Python

floating point operations in shell scripts
Calculator for shell. Similar performance and basic usage as 'bc', but with more advanced features. Not installed on most systems by default.

Show UDID of iPhone
Display the serial of the iPhone (aka UDID).

Show the command line of a process that use a specific port (ubuntu)

Copy a file over SSH without SCP

Find the package that installed a command

find files ignoring .svn and its decendents
for when find . -print | grep -v .svn | xargs doesnt cut it.

Generate SSH public key from the private key

Is today the last day of the month?
Nice simple example of something we can do in bash.

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: