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

All commands from sorted by
Terminal - All commands - 11,581 results
lspci
svn revert .
play $audio_file
2009-03-17 11:30:02
User: mpb
-3

"play" is part of "SoX"

SoX - Sound eXchange, the Swiss Army knife of audio manipulation.

For details, see: man sox

O=$IFS;IFS=$'\n'; D=$(for f in *;do [[ -d $f ]] && du -sh "$f";done | sort -gr);F=$(for f in *;do [[ -f $f ]] && du -sh "$f";done | sort -gr);IFS=$O;echo "$D";echo "$F"
2009-09-03 11:39:50
User: Viperlin
Functions: du sort
-3

biggest->small directories, then biggest->smallest files

function duf { du -k $@ | sort -rn | perl -ne '($s,$f)=split(/\t/,$_,2);for(qw(K M G T)){if($s<1024){$x=($s<10?"%.1f":"%3d");printf("$x$_\t%s",$s,$f);last};$s/=1024}' }
vim file1 file2
echo -e '#!/bin/bash\nssh remote-user@remote-host $0 "$@"' >> /usr/local/bin/ssh-rpc; chmod +x /usr/local/bin/ssh-rpc; ln -s hostname /usr/local/bin/ssh-rpc; hostname
2011-12-28 17:43:34
User: mechmind
Functions: chmod echo hostname ln
Tags: ssh rpc
-3

It's useful mostly for your custom scripts, which running on specific host and tired on ssh'ing every time when you need one simple command (i use it for update remote apt repository, when new package have to be downloaded from another host).

Don't forget to set up authorization by keys, for maximum comfort.

taskkill /F /im notepad.exe
w: !mailx -s "Some subject" user@host.com
(set -e; while true; do TEST_COMMAND; done) | tee log
2011-09-06 12:29:11
User: wipu
Functions: set tee
Tags: tee while set
-3

If you need to fix a randomly failing test (race condition), you need to run it until you get that hard-to-reproduce failure.

ssh remotehosts;date
2012-11-09 01:14:24
User: kiiwii
Functions: ssh
-3

Run this within a steady screen session.

You can get the approximate time when the remote server went down or other abnormal behavior.

find /dev/ -name random -exec bash -c '[ -r $0 -a -w $0 ] && dd if=$0 | sort | dd of=$0' {} \;
dpkg-query -l| grep -v "ii " | grep "rc " | awk '{print $2" "}' | tr -d "\n" | xargs aptitude purge -y
2009-04-28 19:25:53
User: thepicard
Functions: awk grep tr xargs
-3

This will, for an application that has already been removed but had its configuration left behind, purge that configuration from the system. To test it out first, you can remove the last -y, and it will show you what it will purge without actually doing it. I mean it never hurts to check first, "just in case." ;)

s(){ sed 's/./\n\0/g'<<<$1|sort;};cmp -s <(s foobar) <(s farboo)||echo -n "not ";echo anagram
2011-02-17 12:42:45
User: flatcap
Functions: cmp echo sed
-3

Are the two strings anagrams of one another?

sed splits up the strings into one character per line

the result is sorted

cmp compares the results

Note: This is not pretty. I just wanted to see if I could do it in bash.

Note: It uses fewer characters than the perl version :-)

ls -alt /directory/ | awk '{ print $6 " " $7 " -- " $9 }'
tmpfs(){ cd /;for i in $@;do tar czvf /tmp/$i $i;mount -t tmpfs tmpfs /$i;tar xvzf /tmp/$i;cd ~ ;}# usage: tmpfs etc var
ps h -o pid,command | grep 'TEXT' | sed 's/^ \+//' | cut -d ' ' -f 1 | xargs -n 1 kill
col1(){ case $# in 0)echo col1 col-length;;*) sed 's/\(.\{'"$1"'\}\)\(.*\)/\1/' esac;}
2011-12-30 23:35:29
User: argv
Functions: sed
-3

for small output only

example usage:

jobs -l |col1 72

sudo pmset -a hibernatemode 1
ps ux|grep <process name>|awk '{print $2}'|xargs -n 1 kill
removedir () { echo "Deleting the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
2010-01-17 11:34:38
User: oshazard
Functions: basename cd echo read rm sed
-3

CHANGELOG

Version 1.1

removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }

BUG FIX:

Folders with spaces

Version 1.0

removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=`basename $PWD`; rm -Rf ../$blah/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }

BUG FIX:

Hidden directories (.dotdirectory)

Version 0.9

rmdir () { echo "You are about to delete the current directory $PWD. Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=`basename $PWD`; rm -Rf ../$blah/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }

Removes current directory with recursive and force flags plus basic human check. When prompted type yes

1. [user@host ~]$ ls

foo bar

2. [user@host ~]$ cd foo

3. [user@host foo]$ removedir

4. yes

5. rm -Rf foo/

6. [user@host ~]$

7. [user@host ~]$ ls

bar