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,951 results
svn log --stop-on-copy | grep r[0-9] | awk '{print $1}' | sed "s/r//" | sort -n | head -1
git branch -D `git branch | awk '{ if ($0 !~ /next|master/) printf "%s", $0 }'`
2011-04-14 17:43:21
User: denheck
Functions: awk printf
-1

This command will delete all branches in your git repository other than next and master. I use this to cleanup my git repos after making multiple branches and merging them back into next. It's much faster than individually deleting each expired branch using:

git branch -D <branch_name>
df -x tmpfs | grep -vE "(gvfs|procbususb|rootfs)"
2009-02-09 01:54:05
User: bones7456
Functions: df grep
-1

show physical disk using, except tmpfs, gvfs, and so on.

mkdir save && for f in *.mp3; do lame -b xxx "$f" ./save/"${f%.mp3}.mp3"; done
2011-05-30 17:19:36
User: o0110o
Functions: mkdir
-1

Batch Convert MP3 Bitrate to xxxkbps, all the new files will be placed in a folder called "save". Please replace xxx with the desired bitrate. WARNING!!! This will erase any tag information; this is where Picard or EasyTAG will come in handy.

Amharic software
tac $FILETOREVERSE
sleep 15 ; `echo "done" | mail -s "done" 4158575309@txt.att.net`
2012-04-16 01:28:16
User: vajrapani666
Functions: mail sleep
-1

Replace "4158575309@txt.att.net" with your carrier's SMS gateway identifier. The one in the sample is for AT&T. More here https://en.wikipedia.org/wiki/List_of_SMS_gateways. Helpful for getting notifications about long running commands. ";" executes regardless of exit status of last command. && will only notify you if the command succeeds.

find apps/ -name "*.svn-base" -prune -o -print -name "*.php" | xargs grep -E 'new .+Form\('
2009-02-17 14:56:01
User: ubermuda
Functions: find grep xargs
-1

finds all forms instanciated into a symfony project, pruning svn files.

top -bn 1 | awk '{if($1 ~ /^[0-9]+$/ && $9 > 97) {print $1;exit}}'|xargs kill
2010-06-02 13:51:40
User: chx
Functions: awk top xargs
-1

I found Flash eating one of my CPUs after resume, the command above will help with that. For optional kicks you can put it into a script in /etc/pm/sleep.d/ (aspect in #swhack wrote this for me)

sudo sh /usr/share/doc/libdvdread4/install-css.sh
dd of=output.txt if=input.txt ibs=1 skip=$(expr `stat -c%s input.txt` / 2)
find . -maxdepth 1 -type d | grep -Pv "^.$" | sort -rn --field-separator="-" | sed -n '3,$p' | xargs rm -rf
tb send xmpp:user.name@gmail.com
2013-04-08 00:29:43
-1

After installing Termbeamer (see termbeamer.com) you can use it to share a terminal session with one or more others even from behind a firewall or NAT.

hg st --added -n |xargs hg revert
2011-06-01 19:54:41
User: yrcjaya
Functions: xargs
Tags: mercurial
-1

Undo accidental file add to mercurial.

This command undo file adds to all recent adds

printenv
find ~ -type f -size +500M -exec ls -ls {} \; | sort -n
2013-11-17 13:13:14
User: marcanuy
Functions: find ls sort
Tags: size find
-1

Find all files larger than 500M in home directory and print them ordered by size with full info about each file.

ps gv [pid] | head -2
2009-02-12 05:04:55
User: ren
Functions: head ps
-1

sh as:

#! /bin/sh

while [ 1 -ne 6 ]; do

pid=`ps -ef | grep -v "grep" | grep "trans_gzdy" | cut -c10-17`

ps gv $pid | head -2

sleep 1

done

check changes of RSS.

snmpwalk -v2c -c <community> -m ALL <HOST_IP> .
export PS1='[\[\e[36;1m\]\u@\[\e[32;1m\]\h \[\e[31;1m\]\w]# \[\e[0m\]'
netstat -antu | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -n
2013-04-08 19:46:41
User: wejn
Functions: awk netstat sort uniq
-1

Output contains also garbage (text parts from netstat's output) but it's good enough for quick check who's overloading your server.

find <dir> -type f -mtime +<days> -exec scp -r {} user@backuphost:/data/bkup \;
2009-02-12 07:43:29
User: rbattu
Functions: find scp
-1

U have to make key exchange in order to avoid continuous password prompt.

findopen() { local PS3="select file: "; select file in $(find "$1" -iname "$2"); do ${3:-xdg-open} $file; break; done }
2010-02-28 02:28:59
User: quigybo
Functions: file find
-1

lists the files found by find, waits for user input then uses xdg-open to open the selected file with the appropriate program.

usage: findopen path expression [command]

With the third optional input you can specify a command to use other than xdg-open, for example you could echo the filename to stdout then pipe it to another command.

To get it to work for files with spaces it gets a bit messier...

findopen() { files=( $(find "$1" -iname "$2" | tr ' ' '@') ); select file in "${files[@]//@/ }"; do ${3:-xdg-open} "$file"; break; done }

You can replace the @ with any character that probably wont be in a file name.

watch "ss -nat | awk '"'{print $1}'"' | sort | uniq -c"
2012-12-07 19:07:33
User: ricardofunke
Functions: watch
-1

Monitoring TCP connections number showing each state. It uses ss instead of netstat because it's much faster with high trafic.

You can fgrep specific ports by piping right before awk:

watch "ss -nat | fgrep :80 | awk '"'{print $1}'"' | sort | uniq -c"

lynx --dump --source http://www.xkcd.com | grep `lynx --dump http://www.xkcd.com | egrep '(png|jpg)'` | grep title | cut -d = -f2,3 | cut -d '"' -f2,4 | sed -e 's/"/|/g' | awk -F"|" ' { system("display " $1);system("echo "$2); } '
2009-12-03 18:53:57
Functions: awk cut egrep grep
-1

Same thing just a different way to get there. You will need lynx

lsof | awk '/*:https?/{print $2}' | sort -u
2011-02-04 01:37:17
User: sugitaro
Functions: awk sort
Tags: sort awk lsof
-1

% lsof -v

lsof version information:

revision: 4.78