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 bash from sorted by
Terminal - Commands tagged bash - 706 results
find /path/to/dir -type f -printf "%T@|%p\n" 2>/dev/null | sort -n | tail -n 1| awk -F\| '{print $2}'
newest () { find ${1:-\.} -type f |xargs ls -lrt ; }
newest () { DIR=${1:-'.'}; CANDIDATE=`find $DIR -type f|head -n1`; while [[ ! -z $CANDIDATE ]]; do BEST=$CANDIDATE; CANDIDATE=`find $DIR -newer "$BEST" -type f|head -n1`; done; echo "$BEST"; }
2010-02-04 12:40:44
User: shadycraig
Functions: echo head
1

Works recusivley in the specified dir or '.' if none given.

Repeatedly calls 'find' to find a newer file, when no newer files exist you have the newest.

In this case 'newest' means most recently modified. To find the most recently created change -newer to -cnewer.

fbemailscraper YourFBEmail Password
2010-01-31 00:44:35
User: dabom
4

(Apparently it is too long so I put it in sample output, I hope that is OK.)

Run the long command (or put it in your .bashrc) in sample output then run:

fbemailscraper YourFBEmail Password

Voila! Your contacts' emails will appear.

Facebook seems to have gotten rid of the picture encoding of emails and replaced it with a text based version making it easy to scrape!

Needs curl to run and it was made pretty quickly so there might be bugs.

echo alias xkcd="gwenview `w3m -dump http://xkcd.com/|grep png | awk '{print $5}'` 2> /dev/null" >> .bashrc
2010-01-30 20:38:16
User: GinoMan2440
Functions: alias echo
-5

Add an alias to your .bashrc that allows you to issue the command xkcd to view (with gwenview) the newest xkcd comic... I know there are thousands of them out there but this one is at least replete with installer and also uses a more concise syntax... plus, gwenview shows you the downloading progress as it downloads the comic and gives you a more full featured viewing experience.

define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Eo '<li>[^<]+'|sed 's/^<li>//g'|nl|/usr/bin/perl -MHTML::Entities -pe 'decode_entities($_)';}
2010-01-30 13:08:03
User: gthb
Functions: grep sed
7

This version works on Mac (avoids grep -P, adding a sed step instead, and invokes /usr/bin/perl with full path in case you have another one installed).

Still requires that you install perl module HTML::Entities ? here's how: http://www.perlmonks.org/?node_id=640489

cp -arv ~/Documents/{foo,bar} --target-directory=~/buzz/
rename 's/ /_/g' *
2010-01-29 21:15:03
User: tuxilicious
Functions: rename
Tags: bash
8

It's the rename-tool from debians perl-package.

for f in *;do mv "$f" "${f// /_}";done
2010-01-29 19:57:16
User: ethanmiller
Functions: mv
Tags: bash
9

I realize there's a few of these out there, but none exactly in this form, which seems the cleanest to me

define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Po '(?<=<li>)[^<]+'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;}
2010-01-29 05:01:11
User: eightmillion
Functions: grep perl
18

This function takes a word or a phrase as arguments and then fetches definitions using Google's "define" syntax. The "nl" and perl portion isn't strictly necessary. It just makes the output a bit more readable, but this also works:

define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Po '(?<=<li>)[^<]+';}

If your version of grep doesn't have perl compatible regex support, then you can use this version:

define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Eo '<li>[^<]+'|sed 's/<li>//g'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;}
a="www.commandlinefu.com";b="/index.php";for n in $(seq 1 7);do echo -en "GET $b HTTP/1.0\r\nHost: "$a"\r\n\r\n" |nc $a 80 2>&1 |grep Set-Cookie;done
2010-01-28 14:19:43
User: vlan7
Functions: echo grep seq
Tags: bash cookies
3

The loop is to compare cookies. You can remove it...

Maybe you wanna use curl...

curl www.commandlinefu.com/index.php -s0 -I | grep "Set-Cookie"
svn add $(svn st|grep ^\?|cut -c2-)
2010-01-28 09:48:46
User: inkel
Functions: cut grep
Tags: bash svn grep cut
0

This version makes uses of Bash shell expansion, so it might not work in all other shells.

<ALT> .
<CTRL+w>
<ALT> <BACKSPACE>
2010-01-27 19:52:51
User: wincus
2

hit BACKSPACE more than once to delete more words

!$
2010-01-24 17:59:52
User: ringlerun
9

for example if you did a:

ls -la /bin/ls

then

ls !$

is equivalent to doing a

ls /bin/ls
<ALT> .
<ESC> .
removedir(){ read -p "Delete the current directory $PWD ? " human;if [ "$human" = "yes" ]; then [ -z "${PWD##*/}" ] && { echo "$PWD not set" >&2;return 1;}; rm -Rf ../"${PWD##*/}"/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
echo -e "swap=me\n1=2"|sed 's/\(.*\)=\(.*\)/\2=\1/g'
alias PS1="man bash | sed -n '/ASCII bell/,/end a sequence/p'"
2010-01-15 23:39:28
User: haivu
Functions: alias
Tags: bash prompt ps1
3

I use this command (PS1) to show a list bash prompt's special characters. I tested it against A flavor of Red Hat Linux and Mac OS X

exipick -zi | xargs exim -Mrm
for i in `mailq | awk '$6 ~ /^frozen$/ {print $3}'`; do exim -Mrm $i; done
2010-01-13 21:28:45
User: rjamestaylor
Functions: awk
Tags: bash awk exim
-2

Although Exim will purge frozen (undeliverable) messages over time, the command "exim -Mrm #id#" where #id# is a particular message ID will purge a message immediately. Being lazy, I don't want to type the command for each frozen message, so I wrote the one-liner to do it for me.

echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
2010-01-13 09:37:56
User: phaidros
Functions: cd echo mkdir
-4

combines mkdir and cd

added quotes around $_, thanx to flatcap!

for file in $(seq -f '%03.f' 1 $TOTAL ); do echo "($file/$TOTAL)"; curl -f -O http://domain.com/Name_$file.ext; done
2010-01-12 15:23:44
User: nordri
Functions: echo file seq
-4

With counter format [001, 002, ..., 999] , nice with pictures or wallpapers collections.