What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.

If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.

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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands using perl from sorted by
Terminal - Commands using perl - 343 results
ir() { perl -pne 's/(.)(.*)/\[\1]\2/' <<< "$@" ;}
2015-07-25 14:13:33
User: bartonski
Functions: perl
Tags: ps

Note that `grep "$(ir foo)"` really doesn't save any typing, but wrapping this inside a second shell function will:

psg() { grep "$(ir \"$@\")" ;}
grep page.php /var/log/httpd/access_log|awk '{print $1}'|sort|uniq|perl -e 'while (<STDIN>){chomp; $cmd=`ipset add banned -! -q $_`; }'
perl -pE's/(\S+\s*){0,1}//'
2015-05-09 15:14:58
User: pung96
Functions: perl

An advantage is that this doesn't modify remained string at all. One can change {0,1} with {0,n} to drop several columns

echo FileName | perl -nlE'sleep 1 while time-(stat)[10]<10' && echo DONE
2015-05-09 14:58:41
User: pung96
Functions: echo perl

perl version of "Wait for file to stop changing"

When "FileName" has not been changed for last 10 seconds, then print "DONE"

"10" in "(stat)[10]" means ctime.

One have other options like atime, mtime and others. http://perldoc.perl.org/functions/stat.html

sleep 10 & perl -e '$|=@s=qw(-Ooooo \oOooo |ooOoo /oooOo -ooooO \oooOo |ooOoo /oOooo);while(kill 0,'$!'){ print "\r",$s[$t++%($#s+1)];select(undef,undef,undef,0.2);}'
tail -f /var/squid/logs/access.log | perl -pe 's/(\d+)/localtime($1)/e'
perl -e 'for(;;sleep 1){printf"\r"."%.4b "x6,split"",`date +%H%M%S`}'
du -sk -- * | sort -n | perl -pe '@SI=qw(K M G T P); s:^(\d+?)((\d\d\d)*)\s:$1." ".$SI[((length $2)/3)]."\t":e'
2015-04-26 08:07:27
Functions: du perl sort

Tested on MacOS and GNU/Linux.

It works in dirs containing files starting with '-'.

It runs 'du' only once.

It sorts according to size.

It treats 1K=1000 (and not 1024)

finger $(whoami) | perl -ne '/Name: ([a-zA-Z0-9 ]{1,})/ && print "$1\n"'
perl-rename -v 's/720p.+mkv/720p\.mkv/' *.mkv
2014-09-25 14:07:47
User: benkaiser
Functions: perl

I used this (along with a modified one replacing `mkv` with `srt`) to remove the slight differences in who the provider of the video / matching subtitle was (as they are the same contents and the subs match anyway).

So now VLC (and other video players) can easily guess the subtitle file.

perl -MPOSIX -le 'print strftime "%F", localtime 1234567890'
perl -M URI::Escape -lne 'print uri_unescape($_)'
/bin/echo -e '\002Hello, Folks\t!\r' | perl -pwle 'use v5.14; s/([\N{U+0000}-\N{U+0020}])/chr(9216+ord($1))/ge;'
2014-06-30 01:45:40
User: scruss
Functions: perl

Converts control codes and spaces (ASCII code ≤ 32) to visible Unicode Control Pictures, U+2400 ? U+2420. Skips \n characters, which is probably a good thing.

find -type f -iregex '.*\.\(mkv\|mp4\|wmv\|flv\|webm\|mov\|dat\|flv\)' -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
2014-06-07 15:50:41
User: powerinside
Functions: find perl printf tail xargs

Use case insensitive regex to match files ending in popular video format extensions and calculate their total time. (traverses all files recursively starting from the current directory)

<your command> | perl -ne '/(<your regex pattern>)/ && print "$1\n";'
2014-05-27 12:36:31
User: thorko
Functions: perl

Print only the matched pattern at the console

perl -ne 'if (/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} )/ ) { $t=$1; print $_ } else { print $t . $_ }'
2014-05-21 08:54:27
Functions: perl

If you have a logfile where some lines start with timestamps like "2014-05-01 12:34:56,123" but other lines are missing the timestamp (like stack traces or object dumps), then use this script to copy the most recent timestamp to any lines that are missing it.

This is useful for merging log files, since you can then safely sort by timestamp to merge the files.

find . -name '*.phtml' | xargs perl -pi -e 's/(?!(<\?(php|xml|=)))<\?/<\?php/g;'
2014-05-07 14:33:19
User: crashspeeder
Functions: find perl xargs

Tired of front end developers using short open tags in your views? This will replace all instances of

perl -nle 'print length,"\t",$_ if length > 37' < /path/to/input/file
find . -name "*.txt" | xargs -n 1 perl -pi -w -e "s/text([0-9])/other\$1/g;"
2014-02-28 06:38:38
User: kennethjor
Functions: find perl xargs

Does a search and replace across multiple files with a subgroup replacement.

du -g | perl -ne 'print if (tr#/#/# == <maximum depth>)'
2014-02-15 07:33:36
User: RAKK
Functions: du perl
Tags: perl du unix aix

Lists directory size up to a maximum traversal depth on systems like IBM AIX, where the du command doesn't have Linux's --max-depth option. AIX's du uses -g to display directory size on gigabytes, -m to use megabytes, and -k to use kilobytes. tr### is a Perl function that replaces characters and returns the amount of changed characters, so in this case it will return how many slashes there were in the full path name.

find . -type f | perl -ne 'chop(); $ext = substr($_, rindex($_, ".") + 1); print "$ext\n";' | sort | uniq --count | sort -n
2013-09-26 07:45:19
User: t3o
Functions: find perl sort uniq

When trying to find an error in a hosted project it's interesting to find out how the source is organized: Are there .inc files? Or .php files only? Or .xml files that probably contain translated texts?

perl -MYAML -MJSON -0777 -wnl -e 'print YAML::Dump(decode_json($_))' package.json
perl -MYAML -MJSON -0777 -wnl -e 'print YAML::Dump(decode_json($_))' package.json
echo -n 023135 | perl -pe 's/([0-9a-f]{2})/chr hex $1/gie' | nc -4u -q1 -p5001 2000
2013-09-18 14:31:47
User: sucotronic
Functions: echo perl

Use it to send raw data to a networked device. Used to interact with relay controller board whose documentation is lost, so use wireshark to sniff the sent data and replayed using the command.

perl -p -e 's/\$(\w+)/$ENV{$1}/g;' <files...>