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,930 results
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

ls `echo $PATH | sed 's/:/ /g'`
2009-03-09 19:01:41
User: archlich
Functions: ls sed
4

If run in bash, this will display all executables that are in your current $PATH

find . -iname \*.mp3 -print0 | xargs -0 mp3gain -krd 6 && vorbisgain -rfs .
2009-03-09 18:11:35
User: Viaken
Functions: find xargs
9

Run this in the directory you store your music in.

mp3gain and vorbisgain applies the ReplayGain normalization routine to mp3 and ogg files (respectively) in a reversible way.

ReplayGain uses psychoacoustic analysis to make all files sound about the same loudness, so you don't get knocked out of your chair by loud songs after cranking up the volume on quieter ones.

nohup <command> 2> /dev/null > /dev/null &
2009-03-09 16:54:04
User: AmadeusZull
Functions: nohup
2

I'm sure this can be piped into another way. Please share!

for f in *;do mplayer $f;read $n;mv $f $n;done
2009-03-09 16:08:14
User: cobrajs272
5

I used this when I had a directory of movies from a camera. I wanted to watch a little of each movie, then rename it depending on what was in the movie. This did the trick for me.

for person in Alex Bruce Fred Kathy Vicki Victoria ; do say -v $person "Hello, my name is $person"; sleep 1; done
2009-03-09 15:42:13
User: haivu
Functions: sleep
Tags: Os X
-5

Listen to different voices in the system--useful for picking the voice you like

mv file_name.extension ..
2009-03-09 15:35:58
User: takealeft
Functions: mv
-3

cd into the directory that contains the file.

this is just the usual move command but shortcut'd.

say you wanted to move a photo img1.png from ~/photos/holidayphotos into the parent directory which is ~/photos

command would be:

~/photos/holidayphotos$ mv img1.png ..

I use Ubuntu so this'll work in debian but not sure what else.

export PS1='\n[\u@\h \! \w]\n\[\e[32m\]$ \[\e[0m\]'
2009-03-09 15:34:22
User: haivu
Functions: export
Tags: bash
7

I put that line in my .bash_profile (OS X) and .bashrc (Linux).

Here is a summary of what the \char means: n=new line, u=user name, h=host, !=history number, w=current work directory

The \[\e[32m\] sequence set the text to bright green and \[\e[0m\] returns to normal color.

For more information on what you can set in your bash prompt, google 'bash prompt'

find . -name .DS_Store -exec rm {} \;
2009-03-09 13:59:30
User: Svish
Functions: find rm
Tags: Os X
2

Recursively removes all those hidden .DS_Store folders starting in current working directory.

defaults write com.apple.Finder AppleShowAllFiles TRUE
2009-03-09 13:55:31
User: Svish
Functions: write
Tags: Os X
0

Swap TRUE with FALSE to turn it off again. Note: Finder must be relaunched afterwards to see the effect. For example like this:

killall Finder && open /System/Library/CoreServices/Finder.app
touch {1..10}.txt
2009-03-09 12:54:45
User: slaney
Functions: touch
10

Sometimes I need to create a directory of files to operate on to test out some commandlinefu I am cooking up. The main thing is the range ({1..N}) expansion.

svn diff ARGUMENTS_FOR_DIFF | source-highlight --out-format=esc --src-lang=diff
firefox -no-remote -P
2009-03-09 07:03:55
User: asmoore82
14

even when another instance is already open.

Great for testing purposes when you need

to be 2 people at once on the same site.

wget -S -O/dev/null "INSERT_URL_HERE" 2>&1 | grep Server
2009-03-09 06:54:54
User: asmoore82
Functions: grep wget
8

the good:

Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.4 with Suhosin-Patch

the bad:

Server: Microsoft-IIS/6.0

and the ugly:

Server: Apache/2.2.10 (Win32) mod_ssl/2.2.10 OpenSSL/0.9.8i PHP/5.2.6

i="8uyxVmdaJ-w";mplayer -fs $(curl -s "http://www.youtube.com/get_video_info?&video_id=$i" | echo -e $(sed 's/%/\\x/g;s/.*\(v[0-9]\.lscache.*\)/http:\/\/\1/g') | grep -oP '^[^|,]*')
2009-03-09 03:57:44
User: lrvick
Functions: echo grep sed
56

This is the result of a several week venture without X. I found myself totally happy without X (and by extension without flash) and was able to do just about anything but watch YouTube videos... so this a the solution I came up with for that. I am sure this can be done better but this does indeed work... and tends to work far better than YouTube's ghetto proprietary flash player ;-)

Replace $i with any YouTube ID you want and this will scrape the site for the _real_ URL to the full quality .FLV file on Youtube's server and will then will hand that over to mplayer (or vlc or whatever you want) to be streamed.

In some browsers you can replace $i with just a % or put this in a shell script so all YouTube IDs can be handed directly off to your media player of choice for true streaming without the need for Flash or a downloader like clive. (I do however fully recommend clive if you wish to archive videos instead of streaming them)

If any interest is shown I would be more than happy to provide similar commands for other sites. Most streaming flash players use similar logic to YouTube.

Edit: 05/03/2011 -

Updated line to work with current YouTube. It could be a lot prettier but I will probably follow up with another update when I figure out how to get rid of that pesky Grep. Sed should take that syntax... but it doesn't.

Original (no longer working) command:

mplayer -fs $(echo "http://youtube.com/get_video.php?$(curl -s $youtube_url | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p")")

sudo cat /proc/kcore | strings | awk 'length > 20' | less
2009-03-09 02:19:47
User: nesquick
Functions: awk cat strings sudo
Tags: cat ram strings
15

This command lets you see and scroll through all of the strings that are stored in the RAM at any given time. Press space bar to scroll through to see more pages (or use the arrow keys etc).

Sometimes if you don't save that file that you were working on or want to get back something you closed it can be found floating around in here!

The awk command only shows lines that are longer than 20 characters (to avoid seeing lots of junk that probably isn't "human readable").

If you want to dump the whole thing to a file replace the final '| less' with '> memorydump'. This is great for searching through many times (and with the added bonus that it doesn't overwrite any memory...).

Here's a neat example to show up conversations that were had in pidgin (will probably work after it has been closed)...

sudo cat /proc/kcore | strings | grep '([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\})'

(depending on sudo settings it might be best to run

sudo su

first to get to a # prompt)

echo "Whatever you need" | sudo tee [-a] /etc/system-file.cfg
2009-03-09 01:33:31
User: asmoore82
Functions: echo sudo tee
4

only for sudo-style systems.

Use this construct instead of I/O re-directors ``>'' or ``>>'' because

sudo only elevates the commands and *not* the re-directors.

***warning: remember that the `tee` command will clobber

file contents unless it is given the ``-a'' argument

Also, for extra security, the "left" command is still run unprivileged.

cat /path/to/7z.sfx /path/to/archive > archive.exe
2009-03-09 00:54:33
User: asmoore82
Functions: cat
4

using `cat` under *NIX - just because you help manage M$ Windoze

*doesn't* mean you should have to resort to using it!

You can also make custom win32 installers with the 7zip "extras" package:

cat /path/to/7zSD.sfx /path/to/config.txt /path/to/archive > setup.exe