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,585 results
sort -nt . -k 1,1 -k 2,2 -k 3,3 -k 4,4
2009-02-26 20:22:57
User: sysadmn
Functions: sort
14

Sort a list of IPV4 addresses in numerical order. Great as a filter, or within vim using !}

git diff --cached
sort -bdf
2009-02-26 19:55:41
User: sysadmn
Functions: sort
2

Often, when sorting you want the sort to ignore extraneous characters. The b, d, and f tell sort to ignore leading blanks, use 'dictionary order' (ignore punctuation), and ignore (fold) case. Add a "u" if you only want one copy of duplicate lines.

This is a great command to use within vim to sort lines of text, using !}sort -bdf

perl -pi -e's/<what to find>/<what to replace it with>/g' `grep -Rl <what to find> /<dir>/*`
2009-02-26 19:14:39
User: adampbell
Functions: perl
2

Finds the string in every file in an entire directory and all its subdirectories and replaces it with a new string. Especially useful when changing a machine's IP address or hostname - run it on /etc.

time vmware-cmd -l | while read x; do printf "$x"; vmware-cmd "$x" removesnapshots; done
2009-02-26 18:51:29
User: jcgam69
Functions: printf read time
4

Old snapshots can cause problems. It's best to remove them when finished. I use this script to remove all snapshots. The "while read" command is necessary because my vm names contain spaces. The "time" command reports how long the process runs.

for vm in $(vmware-cmd -l);do echo -n "${vm} ";vmware-cmd ${vm} getstate|awk '{print $2 " " $3}';done
2009-02-26 16:45:04
User: dbart
Functions: echo
3

I use this command on my machines running VMware Server to print out the state of all registered Virtual machines.

rpm -qa | grep PACKAGENAME | xargs rpm -q --filesbypkg
2009-02-26 14:32:12
User: piscue
Functions: grep rpm xargs
1

rpm, sometimes, is not wildcard friendly. To search files installed from package this could be useful.

change PACKAGENAME to any package do you want to search

ssh -MNf <user>@<host>
2009-02-26 14:11:19
User: raphink
Functions: ssh
40

Create a persistent SSH connection to the host in the background. Combine this with settings in your ~/.ssh/config:

Host host

ControlPath ~/.ssh/master-%r@%h:%p

ControlMaster no

All the SSH connections to the machine will then go through the persisten SSH socket. This is very useful if you are using SSH to synchronize files (using rsync/sftp/cvs/svn) on a regular basis because it won't create a new socket each time to open an ssh connection.

mount -t proc{,,}
2009-02-26 12:53:58
User: alexfoo
Functions: mount
5

Run this in / in a chroot to get your own proc there.

!?192
2009-02-26 12:52:45
User: alexfoo
-5

Might match "ping 192.168.0.1" and then run it.

sudo ufw allow proto tcp from 1.2.3.4 to any port 80
ls -t1 | head -n1
cat /etc/*-release
2009-02-26 08:22:01
User: sudopeople
Functions: cat
10

Works for most distributions, tested on Ubuntu, Fedora, CentOS, Gentoo, SUSE, RedHat.

Debian and Slackware:

cat /etc/*version
fold -s <filename>
2009-02-26 05:06:51
User: neutral
Functions: fold
9

fold wraps text at 80 characters wide, and with the -s flag, only causes wrapping to occur between words rather than through them.

:mksession! <filename>
2009-02-26 05:03:07
User: neutral
Tags: vim
26

Creates a full snapshot of your current vim session, including tabs, open buffers, cursor positions, everything. Can be resumed with vim -S . Useful for those times when you HAVE to close vim, but you don't want to lose all your hard-opened buffers and windows. The ! will cause vim to overwrite the file if it already exists. It is not necessary, but useful if you frequently save to the same file (like session.vim or something).

for f in $(ls -d /base/*); do ln -s $f /target; done && ls -al /target
2009-02-26 04:48:19
Functions: ln ls
3

Symlinks all files in the base directory to the target directory then lists all of the created symlinks.

alias wordpress='mkdir wordpress && cd wordpress && wget http://wordpress.org/latest.tar.gz && tar -xvzf latest.tar.gz && mv wordpress/* . && rm -rf latest.tar.gz wordpress && cp wp-config-sample.php wp-config.php'
tar cpfP - $(find <somedir> -type f -name *.png) | ssh user@host | tar xpfP -
2009-02-26 00:01:30
User: raphink
Functions: find ssh tar
7

Notes:

* Adjust the find command to your own filters.

* The -P flag forces to keep absolute paths in the tarball, so that you can be sure that the exact same file hierarchy will be created on the second machine.

mii-tool [if]
eject -T [cdrom_device]
2009-02-25 23:53:03
User: raphink
Functions: eject
Tags: device
0

Opens or closes the cdrom device.

date --iso
2009-02-25 23:50:40
User: raphink
Functions: date
0

Useful to archive files once a day:

cp file file.$(date --iso)
^Z then bg
^A ?
2009-02-25 23:42:33
User: raphink
Tags: screen
19

Displays a list of all the basic keyboard shortcuts in screen.

:split <file>
2009-02-25 23:41:22
User: raphink
4

You can then switch from a file to another with ^W^W

dmidecode | grep -i prod
2009-02-25 23:05:17
User: rockon
Functions: grep
6

This command gives a model information of a computer. Also useful in determining the host is a VM machine or actual physical machine.