Hide

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.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged perl from sorted by
Terminal - Commands tagged perl - 174 results
mtr www.google.com
finger $(whoami) | perl -ne '/Name: ([a-zA-Z0-9 ]{1,})/ && print "$1\n"'
catmandu convert JSON --multiline 1 to YAML < file.json > file.yaml
2014-09-29 16:45:24
Tags: perl json yaml
0

This is based on __unixmonkey73469__ answer. You will need to supply `--multiline 1` option to JSON importer if your .json is multiline (i.e. it was prettyfied)

And you still need catmandu installed via `cpanm Catmandu`

perl -MPOSIX -le 'print strftime "%F", localtime 1234567890'
hl() { while read -r; do printf '%s\n' "$(perl -p -e 's/('"$1"')/\a\e[7m$1\e[0m/g' <<< "$REPLY")"; done; }
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
1

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.

rename 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' result_*.txt
2014-06-13 07:34:32
User: sucotronic
Functions: rename
Tags: perl rename
0

Given a bunch of files with "wrong" date naming, it renames them in a "good" format.

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
0

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
1

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
perldoc -m Some::Module
for i in $(find . -regex '.*\/C.*\.cpp'); do svn mv `perl -e 'my $s=$ARGV[0]; $s=~m/(.*\/)C(.*)/; print "$s $1$2"' "$i"`; done
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
0

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.

ls -1 | xargs ruby -e'puts ARGV.shuffle'
perl -Mojo -E 'say g("http://www.census.gov/popclock/data/population/world")->json->{'world'}{'population'};'
logger -tdnsupdate $(curl -s 'https://dynamicdns.park-your-domain.com/update?host=@&domain=xxx&password=xxx'|tee -a /root/dnsupdate|perl -pe'/Count>(\d+)<\/Err/;$_=$1eq"0"?"Update Sucessful":"Update failed"'&&date>>/root/dnsupdate)
2013-08-11 16:27:39
User: MagisterQuis
Functions: logger perl tee
1

Sets the @ A record for your domain hosted by namecheap to your current internet-facing IP address, logs success or failure with syslog, and logs the data returned to /root/dnsupdate.

Change the XXX's as appropriate.

More info at: http://www.namecheap.com/support/knowledgebase/article.aspx/29/

perl -le '$l=80;$l2="!" x $l;substr+($l2^=$l2),$l/2,1,"\xFF";{local $_=$l2;y/\0\xFF/ ^/;print;($lf,$rt)=map{substr $l2 x 2,$_%$l,$l;}1,-1;$l2=$lf^$rt;select undef,undef,undef,.1;redo}'
2013-06-21 06:00:24
User: l3v3l
Functions: perl
0

xmas lights for your terminal - switching the $l value to something like 1200 and zooming out on your terminal gives a great view ...

$text = do {local(@ARGV, $/) = $file ; <>; }; [or] sub read_file { local(@ARGV, $/) = @_ ; <>; }
2013-06-12 11:41:49
User: matya
0

Found it on:

http://stackoverflow.com/questions/318789/whats-the-best-way-to-open-and-read-a-file-in-perl

The yet most simple way to read all the contents of a file to a variable. I used it in a perl script to replace $text="`cat /sys/...`", and stipping down 9 secs of runtime due less forks

pwd|grep -o '/'|perl -ne '$x.="./.";print`readlink -f $x`'|xargs -tn1 chmod 755
2013-03-14 12:03:44
Functions: chmod grep perl pwd xargs
0

`pwd` returns the current path

`grep -o` prints each slash on new line

perl generates the paths sequence: './.', './../.', ...

`readlink` canonicalizes paths (it makes the things more transparent)

`xargs -tn1` applies chmod for each of them. Each command applied is getting printed to STDERR.

FULLPATH=$(perl -e "use Cwd 'abs_path';print abs_path('$0');")
2013-02-01 20:09:34
User: follier
Functions: perl
0

Since none of the systems I work on have readlink, this works cross-platform (everywhere has perl, right?).

Note: This will resolve links.

perl -MModule::Name\ 9999 -e 1
2013-01-15 22:51:39
User: three18ti
Functions: perl
Tags: bash perl zsh
-1

This attempts to load a Perl Module (-M flag) and use version 9999, since no module has a version this high, Perl exits either a) telling you the version of the module installed or b) tells you it can't find the module.

perldoc -l Module::Name 2>/dev/null
2013-01-11 23:28:13
User: MarxBro
Tags: bash perl
4

Shows the path if the module is installed or exit quietly (to simply avoid the 'No documentation found' msg).

module_exists(){ perl -e 'use '$1 2>/dev/null; }
2013-01-11 14:47:07
User: putnamhill
Functions: perl
Tags: bash perl
-2

This version uses a bash function and does not print the path to the module.

perl -E 'say $_,`tput setb $_`," "x(`tput cols`-length("$_")),`tput sgr0` for 0..(`tput colors`-1)'
2012-11-22 01:55:51
User: MarxBro
Functions: perl
Tags: perl color tput
3

Using perl and tput, show all the colors with numbers that your actual $TERM can handle.

If want to remove the numbers at beginning of new line, it should be something like this:

perl -E 'say `tput setb $_`," "x `tput cols`, `tput sgr0` for 0 .. (`tput colors` - 1)'