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
git status | perl -F'\s' -nale 'BEGIN { $a = 0 }; $a = 1 if $_ =~ /changed but not updated/i; print $F[-1] if ( $a && -f $F[-1] )'
2010-10-15 07:58:14
Functions: perl
Tags: status git perl
0

Parse the output of git status.

Once the line '# Changed but not updated:' has passed print every last part of the line if it exists on disk.

diff <(perl -wpl -e '$_ =~ s/^\s+|\s+$//g ;' file1) <(perl -wpl -e '$_ =~ s/^\s+|\s+$//g ;' file2)
2010-10-06 19:14:42
User: jemptymethod
Functions: diff perl
Tags: bash diff perl
2

**NOTE** Tekhne's alternative is much more succinct and its output conforms to the files actual contents rather than with white space removed

My command on the other hand uses bash process substitution (and "Minimal" Perl), instead of files, to first remove leading and trailing white space from lines, before diff'ing the streams. Very useful when differences in indentation, such as in programming source code files, may be irrelevant

perl -e 'print crypt("PASSWORD",int(rand(128))).$/;'
find . -iname '*.jpg' -type f -print0 |perl -0 -ne '$a+=-s $_;END{print "$a\n"}'
2010-09-12 13:14:12
Functions: find perl
1

This deals nicely with filenames containing special characters and can deal with more files than can fit on a commandline. It also avoids spawning du.

perl -e 'print "\n"x100'
2010-09-07 10:11:10
User: SunX
Functions: perl
-2

Perl variant of echo several blank lines command

useradd -m -p $(perl -e'print crypt("pass", "mb")') user
2010-09-03 19:00:56
User: mariusbutuc
Functions: perl useradd
0

Function: char * crypt (const char *key, const char *salt)

The crypt function takes a password, key, as a string, and a salt character array which is described below, and returns a printable ASCII string which starts with another salt. It is believed that, given the output of the function, the best way to find a key that will produce that output is to guess values of key until the original value of key is found.

The salt parameter does two things. Firstly, it selects which algorithm is used, the MD5-based one or the DES-based one. Secondly, it makes life harder for someone trying to guess passwords against a file containing many passwords; without a salt, an intruder can make a guess, run crypt on it once, and compare the result with all the passwords. With a salt, the intruder must run crypt once for each different salt.

For the MD5-based algorithm, the salt should consist of the string $1$, followed by up to 8 characters, terminated by either another $ or the end of the string. The result of crypt will be the salt, followed by a $ if the salt didn't end with one, followed by 22 characters from the alphabet ./0-9A-Za-z, up to 34 characters total. Every character in the key is significant.

For the DES-based algorithm, the salt should consist of two characters from the alphabet ./0-9A-Za-z, and the result of crypt will be those two characters followed by 11 more from the same alphabet, 13 in total. Only the first 8 characters in the key are significant.

perl -MURI::Escape -e 'print uri_escape("String encoded to a url");'
ls | perl -lne '++$x{lc $1} if /[.](.+)$/ }{ print for keys %x'
2010-08-13 20:05:15
User: recursiverse
Functions: ls perl
-3

All with only one pipe. Should be much faster as well (sort is slow). Use find instead of ls for recursion or reliability.

Edit: case insensitive

perl -e 'print 1+1 ."\n";'
perl -MFile::Find=find -MFile::Spec::Functions -Tlwe 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'
2010-08-02 15:18:51
User: randy909
Functions: perl
2

This version works on an AIX system on which I have very limited permissions. The other version fails with "Can't open file /usr/opt/perl588/lib/site_perl/5.8.8/aix/auto/DBI/.packlist".

perl -le 'chomp($w=`which $ARGV[0]`);$_=`file $w`;while(/link\b/){chomp($_=(split/`/,$_)[1]);chop$_;$w.=" -> $_";$_=`file $_`;}print "\n$w";' COMMAND_NAME
2010-07-30 19:26:35
User: dbbolton
Functions: perl
0

This will show you any links that a command follows (unlike 'file -L'), as well as the ultimate binary or script.

Put the name of the command at the very end; this will be passed to perl as the first argument.

For obvious reasons, this doesn't work with aliases or functions.

perl -pi -e "s/\r/\n/g" <file>
2010-07-29 16:07:36
User: din7
Functions: perl
-1

Replace DOS character ^M with newline using perl inline replace.

perl -MExtUtils::Installed -e '$inst = ExtUtils::Installed->new(); @modules = $inst->modules(); print join("\n", @modules);'
2010-07-20 15:47:32
User: braak
Functions: perl
6

Works only if modules are installed "the right way"

ls -l $(find ./ -type l | perl -ne 'chomp; if (-d) { print "$_\n" }')
2010-07-16 19:31:28
User: rwadkins
Functions: find ls perl
-1

This will list all symlinks that are directories under the current directory. This will help you distinguish them from regular files.

echo -n 'text' | perl -pe 's/(.)/sprintf("\\x%x", ord($1))/eg'
2010-07-14 12:20:42
User: putnamhill
Functions: echo perl
Tags: perl hex ascii
1

Here's a version that uses perl. If you'd like a trailing newline:

perl -pe 's/(.)/sprintf("\\x%x", ord($1))/eg; END {print "\n"}'
google contacts list name,name,email|perl -pne 's%^((?!N\/A)(.+?)),((?!N\/A)(.+?)),([a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+))%${1}:${3} <${5}>%imx' #see below for full command
2010-07-12 16:50:44
User: Raymii
Functions: perl
1

Full Command:

google contacts list name,name,email|perl -pne 's%^((?!N\/A)(.+?)),((?!N\/A)(.+?)),([a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+))%${1}:${3} <${5}>%imx'|grep -oP '^((?!N\/A)(.+?)) <[a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+)>' | sort

You'll need googlecl and python-gdata. First setup google cl via:

google

Then give your PC access

google contacts list name,email

Then do the command, save it or use this one to dump it in the cone-address.txt file in your home dir:

google contacts list name,name,email | perl -p -n -e 's%^((?!N\/A)(.+?)),((?!N\/A)(.+?)),([a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+))%${1}:${3} <${5}>%imx' | grep -o -P '^((?!N\/A)(.+?)) <[a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+)>' | sort > ~/cone-adress.txt

Then import into cone.

It filters out multiple emails, and contacts with no email that have N/A. (Picasa photo persons without email for example...)

perl -e 'print "\x41\x72\x74\x20\x6f\x66\x20\x68\x61\x63\x6b\x69\x6e\x67\x2e\x2e\x2e\n" x 100'
cat file_with_tabs.txt | perl -pe 's/\t/ /g'
2010-07-11 13:01:22
User: nikc
Functions: cat perl
Tags: cat perl replace
-3

Replaces tabs in output with spaces. Uses perl since sed seems to work differently across platforms.

find . | perl -wne 'chomp; print qq|<img src="$_" title="$_" /><br />| if /\.(jpg|gif|png)$/;'> gallery.html
2010-07-04 01:43:50
User: spotrick
Functions: find perl
1

This includes a title attribute so you can see the file name by hovering over an image. Also will hoover up any image format - jpg, gif and png.

ifconfig eth0 | perl -ne "print if m/inet addr:((\d+\.){3})+/" | sed "s/inet addr//" | sed "s/Bcast//" |awk -F: '{print $2}'
2010-06-26 12:07:15
User: aceiro
Functions: awk ifconfig perl sed
0

only output the ip addres. I put double pipe with sed because not parse with operator OR (|) in redex.

ifconfig eth0 | perl -ne 'print $1 if m/addr:((?:\d+\.){3}\d+)/'
for pid in `screen -ls | grep -v $STY | grep tached | awk '{print $1;}' | perl -nle '$_ =~ /^(\d+)/; print $1;'`; do screen -x $pid; done
2010-06-22 23:06:31
User: tmsh
Functions: awk grep perl screen
0

Personally, I save this in a one line script called ~/bin/sci:

#!/bin/bash

for pid in `screen -ls | grep -v $STY | grep tached | awk '{print $1;}' | perl -nle '$_ =~ /^(\d+)/; print $1;'`; do screen -x $pid; done

I also use:

alias scx='screen -x'

alias scl='screen -ls | grep -v $STY'

perl -p -e 's/^([0-9]*)/"[".localtime($1)."]"/e' < /var/log/squid/access.log
2010-06-22 08:42:40
User: KoRoVaMiLK
Functions: perl
1

On-the-fly conversion of Unix Time to human-readable in Squid's access.log

perl -MNet::Twitter -e '$nt = Net::Twitter->new(traits => [qw/API::REST/], username => "YOUR USERNAME", password => "YOUR PASSWORD"); $ud = $nt->update("YOUR TWEET");'
2010-06-16 19:46:05
User: dbbolton
Functions: perl
2

Requires Net::Twitter. Just replace the double quoted strings with the appropriate info.

git log -p -z | perl -ln0e 'print if /[+-].*searchedstring/'