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 - 328 results
for f in $(ls -A ./dir); do echo -n $f && diff original.txt ./dir/$f | wc -l ; done | perl -ne 'my $h={}; while (<>) { chomp; if (/^(\S+?)\s*(\d+?)$/){$h->{$1}=$2;} }; for my $k (sort { $h->{$a} $h->{$b} } keys %$h ){ print "$k\t$h->{$k}\n"}'
perl -ane 'END{printf(" %d %d %d\n", $x, $y, $z)} $x+=1; $y+=@F; $z+=length' file.txt
perl -C -e 'print for sort { length $a <=> length $b or $a cmp $b } <>' < /usr/share/dict/words | tail
2011-10-20 01:43:25
User: dbr
Functions: cmp perl sort
3

making it "sound" more "natural" language like -- additionally sorting the longest words alphabetically:

this approach is using:

* to get at all lines of input

* post-"for" structure

* short-circuit-or in sort: if the lengths are the same, then sort alphabetically otherwise don't even evaluate the right hand side of the or

* -C sets all input and ouput channels to utf8

svn log | perl -pe 's/\n//g => s/^-.*/\n/g'
2011-10-20 01:20:52
User: dbr
Functions: perl
Tags: svn perl svn log
0

dirrrty: use -p to chomp automatically, substitute all newlines away and then replace the "---" by a newline ? bingo!

s/// => s/// is just a cooler way to write s///, s/// which is just the small brother of s///; s/// (comma is an operator!)

have fun!

svn log | perl -l40pe 's/^-+/\n/'
2011-10-14 16:02:22
User: bazzargh
Functions: perl
Tags: svn perl svn log
1

the output of svn log is annoying to grep, since it spreads the useful info over multiple lines. This compacts the output down to one line so eg you can grep for a comment and see the rev, date & committer straight away.

Updated: MUCH shorter, easier to remember. Now it just replaces newlines with spaces, except on '---' lines.

perl -MAcme::POE::Tree -e 'Acme::POE::Tree->new()->run()'
cat table-mv.txt | perl -pe 's{([^;]+);([^;]+)}{tbl$1/tbl$2}' | perl -pe 's{(\S+)}{perl -i -pe #s/$1/g# xxx.sql}' | tr "#" "\'" | bash
2011-10-05 15:55:34
User: hute37
Functions: cat perl tr
0

with a semicolon text file map, apply multiple replace to a single file

perl -i -pe 'BEGIN{undef $/;} s/START.*?STOP/replace_string/smg' file_to_change
perl -MPOSIX -e 'print strerror($ARGV[0])."\n";' ERRNO
2011-09-21 18:16:19
User: sethjust
Functions: perl
Tags: strerror errno
1

Calls the POSIX strerror() function to look up the meaning of integer ERRNOs set by some functions.

tail -f LOGFILE | perl -ne '`say "$_"`;'
2011-09-16 05:33:22
User: tamouse
Functions: perl tail
Tags: perl tail say
0

say only processes a complete file, at eof, so following a file isn't possible. Quick and dirty perl oneliner to feed each line from the tail -f to say. Yes, expensive to lauch a new process each line.

This little ditty was prompted by a discussion on how horrible it is to use VoiceOver on ncurses programs such as irssi.

perl -MTerm::ReadLine -wde'print "TheAnswer=42\n"'
sudo find / -type f | perl -MFile::Basename -ne '$counts{dirname($_)}++; END { foreach $d (sort keys %counts) {printf("%d\t%s\n",$counts{$d},$d);} }'|sort -rn | tee /tmp/sortedfilecount.out | head
2011-09-14 19:41:19
User: tamouse
Functions: find perl sort sudo tee
0

Find which directories on your system contain a lot of files.

Edit: much shorter and betterer with -n switch.

cd /path/to/pmwiki/wiki.d;/bin/ls -1 | perl -ne 'my ($group,$name)=split(/\./);$counts{$group}++;' -e 'END { foreach $group (sort keys %counts) {printf("%d\t%s\n",$counts{$group},$group);} }'|sort -rn
2011-09-14 19:33:39
User: tamouse
Functions: cd perl sort
Tags: sort perl pmwiki
-2

PmWiki stores wiki pages as Group.Name. Simply split the directory listing and count frequency of group occurances.

/usr/sbin/dmidecode | perl -lne 'print $1 if /Current\s+Speed:\s+(\d+\s+MHz)/'
route -n | perl -ne '$ANY="0.0.0.0"; /^$ANY/ and split /\s+/ and print "Gateway to the World: ",($_[1]!=$ANY)?$_[1]:(`ip address show $_[$#_]`=~/peer ([0-9\.]+)/ and $1),", via $_[$#_].\n"'
perl -ne '$. == 4 && print && exit'
2011-09-05 21:52:16
User: bashrc
Functions: perl
-1

Perl version - just for completeness sake ;)

cat file.csv | perl -pe 'if($. == 1) {@h = split(/;/); $i = 1 ; map { $_ = $i; $i++ } @h; print join(" ;", @h) , "\n"} ; s/(^|;);/$1 ;/g' | column -ts\; | less -S
perl -F'\s+' -anE 'push @w,$F[1];END{$r.=splice @w,rand @w,1 for(1..4);say $r}' diceware.wordlist.asc
echo "LINUX,DIR,FILE1,FILE2,FILE3" | perl -aF, -nle 'my ($fld1, $fld2, @fields) = @F; while(@fields) { print join ",", $fld1, $fld2, splice(@fields, 0, 1) }'
2011-08-22 18:27:12
User: mindrape
Functions: echo join perl
0

Lets say you have a file with the following layout:

LINUX,DIR,FILE1,FILE2,FILE3

You want the file to look like this:

LINUX,DIR,FILE1

LINUX,DIR,FILE2

LINUX,DIR,FILE3

This perl command does it for you.

perl -ne 'print "$1 " if /^Host (.+)$/' ~/.ssh/config
2011-08-21 14:51:20
User: bashrc
Functions: perl
4

I use this in my bashrc to expand hosts defined in ~/.ssh/config:

function _ssh_completion() {

perl -ne 'print "$1 " if /^Host (.+)$/' ~/.ssh/config

}

complete -W "$(_ssh_completion)" ssh

Here's a great article on how to setup your own ~/.ssh/config:

http://blogs.perl.org/users/smylers/2011/08/ssh-productivity-tips.html

find /var/spool/mqueue -type f -mtime +7 | perl -lne unlink
2011-08-19 15:22:02
User: mengesb
Functions: find perl
0

Find all files in /var/spool/mqueue older than 7 days, pass to perl to efficiently delete them (faster than xargs or -exec when you've got millions or hundreds of thousands to delete). Naturally the type, directory, and file age vars can be adjusted to meet your specific needs.

strace -ff -e trace=file my_command 2>&1 | perl -ne 's/^[^"]+"(([^\\"]|\\[\\"nt])*)".*/$1/ && print'
2011-08-16 15:00:18
Functions: perl strace
16

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.

prep () { perl -nle 'print if '"$1"';' $2 }
2011-08-13 14:29:26
User: dbbolton
Functions: perl
Tags: perl grep regex
2

If you've ever tried "grep -P" you know how terrible it is. Even the man page describes it as "highly experimental". This function will let you 'grep' pipes and files using Perl syntax for regular expressions.

The first argument is the pattern, e.g. '/foo/'. The second argument is a filename (optional).

cat subtitles.txt | perl -pe 's/} /}/g; s/{(\d+)}/=1=/; $f1=(24/25*$1); s/{(\d+)}/=2=/; $f2=(24/25*$1); $f1=~s/\..*//; $f2=~s/\..*//; s/=1=/{$f1}/; s/=2=/{$f2}/; ' > subtitles_newfps.txt
2011-07-30 15:34:43
User: ciekawy
Functions: cat perl
1

this command example converts to 25 fps subtitles that were originally created for 24 fps movie

perl -mText::Highlight -E 'say Text::Highlight->new(ansi => 1)->highlight(Perl => do { local (@ARGV,$/) = shift; <> }) ' path/to/perl-file.pl