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,492 results
cd ../"$(ls -F ..|grep '/'|grep -A1 `basename $PWD`|tail -n 1)"
2009-05-18 06:41:37
User: gwiener
Functions: cd
3

Another version based on linkinpark342's contribution.

Sometimes you have to browse your way through a lot of sub-directories. This command cd to the next sub-directory in alphabetical order. For example, if you have the directories "lectures/01-intro", "lectures/02-basic", "lectures/03-advanced" and so on, and your PWD is "02-basic", it jumps to "03-advanced".

function autoCompleteHostname() { local hosts; local cur; hosts=($(awk '{print $1}' ~/.ssh/known_hosts | cut -d, -f1)); cur=${COMP_WORDS[COMP_CWORD]}; COMPREPLY=($(compgen -W '${hosts[@]}' -- $cur )) } complete -F autoCompleteHostname ssh
2009-05-17 23:12:34
User: sbisordi
Functions: awk cut
6

This is meant for the bash shell. Put this function in your .profile and you'll be able to use tab-completion for sshing any host which is in your known_hosts file. This assumes that your known_hosts file is located at ~/.ssh/known_hosts. The "complete" command should go on a separate line as such:

function autoCompleteHostname() {

local hosts=($(awk '{print $1}' ~/.ssh/known_hosts | cut -d, -f1));

local cur=${COMP_WORDS[COMP_CWORD]};

COMPREPLY=($(compgen -W '${hosts[@]}' -- $cur ))

}

complete -F autoCompleteHostname ssh

cat -n FILE | grep -C3 "^[[:blank:]]\{1,5\}NUMBER[[:blank:]]"
2009-05-17 18:19:55
User: lv4tech
Functions: cat grep
-1

This is useful for displaying a portion of a FILE that contains an error at line NUMBER

mencoder -ovc copy -nosound ./movie.mov -o ./movie_mute.mov
2009-05-17 11:56:22
User: angleto
2

create a copy of a video file without the audio tracs

find . -name \*.mp3 -printf "%C+ %h/%f\n" | sort -r | head -n20 | awk '{print "\""$2"\""}' | xargs -I {} cp {} ~/tmp
2009-05-17 07:06:10
User: bkinsey
Functions: awk cp find head sort xargs
2

Change ~/tmp to the destination directory, such as your mounted media. Change -n20 to whatever number of files to copy. It should quit when media is full. I use this to put my most recently downloaded podcasts onto my phone.

lsb_release -a
2009-05-17 02:49:18
User: Vasudev
-7

Get the complete distro information from shell prompt like distributor ID,code name etc.

for d in `find .. -mindepth 1 -maxdepth 1 -type d | sort`; do if [[ `basename $d` > `basename $PWD` ]]; then cd $d; break; fi; done
2009-05-16 17:33:34
User: gwiener
Functions: cd
4

Sometimes you have to browse your way through a lot of sub-directories. This command cd to the next sub-directory in alphabetical order. For example, if you have the directories "lectures/01-intro", "lectures/02-basic", "lectures/03-advanced" and so on, and your PWD is "02-basic", it jumps to "03-advanced".

while true; do cat /usr/src/linux/kernel/signal.c > /dev/dsp; done
2009-05-16 14:44:57
Functions: cat
1

replace "/usr/src/linux/kernel/signal.c" with any file you want and listen to its output ! :P

you can also replace "cat" with "echo" or anything you can come up with

have fun :-}

find . -name '*.m4a' | xargs -I audiofile mplayer -ao pcm "audiofile" -ao pcm:file="audiofile.wav"
find /var -mount -ls -xdev | /usr/bin/sort -nr +6 | more
zpool create tank /path/to/file/a /path/to/file/b
2009-05-16 10:46:17
User: mnikhil
Tags: zfs zpool
-2

This command creates an unmirrored pool using

files. While not recommended, a pool based on files can be

useful for experimental purposes.

zpool create tank mirror c0t0d0 c0t1d0 mirror c0t2d0 c0t3d0
2009-05-16 10:44:56
User: mnikhil
Tags: zfs zpool
-2

command creates a pool with two mirrors, where each mirror contains two disks.

zpool create tank raidz c0t0d0 c0t1d0 c0t2d0 c0t3d0 c0t4d0 c0t5d0
2009-05-16 10:44:05
User: mnikhil
-2

The following command creates a pool with a single raidz root vdev that consists of six disks.

mencoder dvd://1 -aid 128 -o track-1.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4
2009-05-16 00:08:57
User: dstahlke
2

Substitute whatever track number you want into the "dvd://1" parameter. The "-aid 128" is to select the English language.

/sbin/dumpe2fs /dev/hda2 | grep 'Block size'
2009-05-15 22:23:21
User: rez0r
Functions: grep
Tags: size output block
0

Useful to know, especially if you are dealing with output configurations in block size.

Tested on 'Red Hat'.

curl -u USER:PASS -d status="NEW STATUS" http://identi.ca/api/statuses/update.xml
2009-05-15 19:57:00
4

identica is an open source social networking and micro-blogging service. Based on Laconica, a micro-blogging software package built on the OpenMicroBlogging specification.

http://identi.ca/

psql -U postgres -lAt | gawk -F\| '$1 !~ /^template/ && $1 !~ /^postgres/ && NF > 1 {print $1}'
gtar cpf - . | (cd /dest/directory; gtar xpf -)
2009-05-15 13:23:00
User: mnikhil
Functions: cd
Tags: gtar, gtar
1

It copies the entire current working directory to the destination directory with compression enabled.

mkdir `date --iso`
2009-05-15 12:30:10
User: hal
Functions: mkdir
2

Create a directory named with the current date in ISO 8601 format (yyyy-mm-dd). Useful for storing backups by date. The --iso switch may only work with GNU date, can use format string argument for other date versions.

svn log | grep "bodge\|fudge\|hack\|dirty"
2009-05-15 09:55:44
User: root
Functions: grep
-4

A good way to understand what you've let yourself in for. Potential project metric could be the count:

svn log | grep -c "bodge\|fudge\|hack\|dirty"
ffmpeg -i <filename>.flv -vn <filename>.mp3
$ mplayer -dumpaudio -dumpfile <filename>.mp3 <filename>.flv
clfu-seach <search words>
svn st | grep '^\?' | awk '{print $2}' | xargs svn add; svn st | grep '^\!' | awk '{print $2}' | xargs svn rm
2009-05-14 14:34:50
User: stedwick
Functions: awk grep xargs
0

automatically add and remove files in subversion so that you don't have to do it through the annoying svn commands anymore

find . -name "\.svn" -exec rm -rf {} ";"