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 using perl from sorted by
Terminal - Commands using perl - 335 results
perl -e 'print scalar localtime $ARGV[0],"\n" ' epoch
du -hS / | perl -ne '(m/\d{3,}M\s+\S/ || m/G\s+\S/) && print'
2009-03-25 18:06:53
User: Alioth
Functions: du perl
6

Finds all directories containing more than 99MB of files, and prints them in human readable format. The directories sizes do not include their subdirectories, so it is very useful for finding any single directory with a lot of large files.

perl -e 'print "Hello World!", "\n";'
history | perl -lane '$lsize{$_} = scalar(@F); if($longest<$lsize{$_}) { $longest = $lsize{$_}; print "$_"; };' | tail -n1
perl -e 'print map { $_ . "\n" } @INC;'
perl -0777 -ne 'print "$1\n" while /word-a(.*?)word-b/gs' filename.txt
2009-03-08 11:47:18
User: eleffie
Functions: perl
3

This command will display all lines between 2 patterns: word-a and word-b

useful for grepping command outputs from file

find $HOME -type f -print | perl -wnlaF'/' -e 'BEGIN{ print "#EXTM3U"; } /.+\.wmv$|.+\.mpg$|.+\.vob$/i and print "#EXTINF:$F[-1]\nfile://$&";' > movies.m3u
perl -pi -e's/<what to find>/<what to replace it with>/g' `grep -Rl <what to find> /<dir>/*`
2009-02-26 19:14:39
User: adampbell
Functions: perl
2

Finds the string in every file in an entire directory and all its subdirectories and replaces it with a new string. Especially useful when changing a machine's IP address or hostname - run it on /etc.

find . -type d | perl -nle 'print s,/,/,g," $_"' | sort -n | tail
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'
nmap -sS -O -oX /tmp/nmap.xml 10.1.1.0/24 -v -v && perl nmap2nagios.pl -v -r /tmp/10net.xml -o /etc/nagios/10net.cfg
perl -e 'print 1, 2, 3' > /dev/full
2009-02-19 17:08:13
User: mkc
Functions: perl
8

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.

perl -e 'use MIME::Base64; print encode_base64("encode me plz");'
2009-02-18 22:49:18
User: therek
Functions: perl
1

MIME::Base64 is a part of Perl5 distribution. You can also use decode_base64 for oposite result.

perl -lpe'1 while s/^([-+]?\d+)(\d{3})/$1.$2/'
perl -pe '$_=reverse;s/\d{3}(?=\d)(?!.*?\.)/$&,/g;$_=reverse'
2009-02-18 16:34:18
User: sil
Functions: perl
2

Insert a comma where necessary when counting large numbers. I needed to separate huge amounts of packets and after 12+ hours of looking in a terminal, I wanted it in readable form.

find . -iname "*.cpp" -exec perl -ni -e 'chomp; print "$_\n"' {} \;
2009-02-18 14:12:24
User: grundprinzip
Functions: find perl
0

Adds a newline to the end of all cpp files in the directory to avoid warnings from gcc compiler.

perl -pe 's/\d+/++$n/e' file.txt
perl -pi -e 's:^V^M::g' <filenames>
2009-02-16 21:17:40
User: starchox
Functions: perl
1

That "^M" is Ctrl-M, which is a carriage return, and is not needed in Unix file systems.

Where ^V is actually Ctrl-V and ^M is actually Ctrl-M (you must type these yourself, don't just copy and paste this command). ^V will not be displayed on your screen.

find . -type f -exec grep -l XXX {} \;|tee /tmp/fileschanged|xargs perl -pi.bak -e 's/XXX/YYY/g'
2009-02-16 02:55:23
User: drossman
Functions: find grep perl tee xargs
6

Find all files that contain string XXX in them, change the string from XXX to YYY, make a backup copy of the file and save a list of files changed in /tmp/fileschanged.

cat file.php | perl -p -e 's/(\$|->)(str|arr|obj|int|flt|boo|bool|mix|res)([A-Z])/$1\L$3/g'
2009-02-10 14:37:12
User: root
Functions: cat perl
0

This removes the type prefix used in Hungarian notation (v. bad) for PHP variables. Eg. variables of the form $intDays, $fltPrice, $arrItems, $objLogger convert to $days, $price, $Items, $logger.

perl -e "''=~('(?{'.('-^@.]|(;,@/{}/),[\\\$['^'],)@)[\`^@,@[*@[@?}.|').'})')"
find . -name "*.jpg" | perl -ne'chomp; $name = $_; $quote = chr(39); s/[$quote\\!]/_/ ; print "mv \"$name\" \"$_\"\n"'
perl -e "use SOAP::Lite"
2009-02-06 15:26:37
User: leprasmurf
Functions: perl
0

Quick command to check if Perl library is installed on your server.

perl -pe 's/.+;//' ~/.zsh_history | sort | uniq -c | sort -r|head -10
perl -le 'print join ", ", map { chomp; $_ } <>'
2009-02-06 12:50:43
User: jozef
Functions: join perl
-1

joins multiple lines to create single line with comma separated values. for example if we have an email addresses one per line (copy&paste from spreadsheet) it will oputput one line with comman separated addresses to put it to email client.