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,847 results
ctrl-x e
2009-03-11 09:26:05
User: fool
404

Next time you are using your shell, try typing ctrl-x e (that is holding control key press x and then e). The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR. Then you can edit at leisure using all the powerful macros and commands of vi, emacs, nano, or whatever.

sudo lsof -i :<port>
vim $( ls -t | head -n1 )
rpm2cpio /path/to/file.rpm | cpio -i -d
2009-03-10 21:02:14
User: atoponce
Functions: cpio
1

This assumes you have the 'rpm', 'rpm2cpio' and 'cpio' packages installed. This will extract the contents of the RPM package to your current directory. This is useful for working with the files that the package provides without installing the package on your system. Might be useful to create a temporary directory to hold the packages before running the extraction:

mkdir /tmp/new-package/; cd /tmp/new-package
echo -n "string" | md5sum -
2009-03-10 21:01:36
User: cryptema
Functions: echo md5sum
0

A useful way to generate the MD5 hash for a string by command line

for i in $(ls *.od{tp}); do unoconv -f pdf $i; done
function cdls { cd $1; ls; }
2009-03-10 19:13:47
User: joem86
Functions: cd
Tags: terminal
7

After typing cd directory [enter] ls [enter] so many times, I figured I'd try to make it into a function. I was surprised how smoothly I was able to integrate it into my work on the command line.

Just use cdls as you would cd. It will automatically list the directory contents after you cd into the directory. To make the command always available, add it to your .bashrc file.

Not quite monumental, but still pretty convenient.

for x in *s.yml; do mv $x `echo $x | sed 's/s\.yml/\.yml/'`; done
2009-03-10 17:51:19
Functions: mv sed
-1

Jan Nelson from Grockit came up with this for us when we needed to rename all of our fixtures.

svn status | grep ^? | awk '{print $2}' | xargs rm -rf
2009-03-10 17:01:40
User: Highwayman
Functions: awk grep rm xargs
1

Removes all unversioned files and folders from an svn repository. Also:

svn status --no-ignore | grep ^I | awk '{print $2}' | xargs rm -rf

will remove those files which svn status ignores. Handy to add to a script which is in your path so you can run it from any repository (a la 'svn_clean.sh').

grep Mar/2009 /var/log/apache2/access.log | awk '{ print $1 }' | sort -n | uniq -c | sort -rn | head
echo "\"\e[5~\": history-search-backward" >> ~/.inputrc
2009-03-10 15:32:52
User: silbermm
Functions: echo
0

What this does is, if I type ?ssh ? then hit the page-up key, it will complete the line to the last time in my history file that I typed ssh. Hitting page up again will go to the 2nd to last time I typed it. Incredibly handy if you ever type the same commands more than once.

credit goes to http://www.sharms.org/blog/2009/03/10/make-your-bash-shell-cool-again/

echo "\"\e[6~\": history-search-forward" >> ~/.inputrc
ssh -NL 2000:remotehost:22 remotehost
rename .JPG .jpg *.JPG
2009-03-10 15:07:24
User: psytek
Functions: rename
3

This will change all files ending in .JPG to .jpg and will work with any file extension

ssh -fX <user>@<host> <program>
2009-03-10 15:02:35
User: prayer
Functions: ssh
Tags: ssh
15
ssh -X example.org xeyes

The SSH server configuration requires:

X11Forwarding yes # this is default in Debian

And it's convenient too:

Compression delayed

gconftool-2 -t bool -s /desktop/gnome/interface/can_change_accels true
2009-03-10 14:53:33
User: prayer
Tags: gtk
-1

Then, you can assign new accels to GTK menu actions pressing the keys while the option is active.

echo -n $mypass | md5sum | awk {'print $1'}
2009-03-10 13:12:21
User: tororebelde
Functions: awk echo md5sum
1

This was useful to generate random passwords to some webpage users, using the sample code, inside a bash script

unalias ls
2009-03-10 11:23:31
Functions: unalias
1

Sometimes you have a situation where you cannot properly see the ls output when you are using a terminal w/a dark background. Usually bash has ls aliased to use colors, and you can easily get ls to use the default foreground color via simply unaliasing the command.

awk '{ for (f = 1; f <= NF; f++) a[NR, f] = $f } NF > nf { nf = NF } END { for (f = 1; f <= nf; f++) for (r = 1; r <= NR; r++) printf a[r, f] (r==NR ? RS : FS) }'
2009-03-10 05:35:22
User: MSBF
Functions: awk printf
0

works the same as R's t()

yes "text" | annoying_installer_program # "text" defaults to the letter y
set -o noclobber
2009-03-10 01:39:18
User: vududevil
Functions: set
6

Use set +o noclobber and you will be able to replace files again

sudo find / -iname "*.lproj" -and \! -iname "en*" -print0 | tee /dev/stderr | sudo xargs -0 rm -rfv
2009-03-09 22:08:45
User: asmoore82
Functions: find rm sudo tee xargs
3

This will get the job done in the most efficient way -

spawning only one `rm` process.

"On-the-fly" find data is displayed through `tee` and

you should have plenty of time to ctrl-c if needed before it's too late.

You may need to re-run this after major Software Updates.

To leave more languages in, add more ``-and \! -iname "lang*"'' statements:

sudo find / -iname "*.lproj" -and \! -iname "en*" -and \! -iname "spanish*" -print0 | tee /dev/stderr | sudo xargs -0 rm -rfv

**Edit: note the 2nd sudo near the end of the pipeline - this is necessary.

tar cf - . | (cd /new/dir; tar xvf -)
2009-03-09 20:30:34
User: jauderho
Functions: cd tar
-1

Add z to the flags to enable compression.

mtree -c -K sha256digest -X mtree.exclude -p /path > host.mtree
wget -r ftp://user:pass@ftp.example.com
2009-03-09 19:39:30
User: movaxes
Functions: wget
Tags: wget
6

If the username includes an @ you can use this one:

wget -r --user=username_here --password=pass_here ftp://ftp.example.com