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,620 results
mv -vi file{,~}
2009-08-14 19:29:59
User: fsilveira
Functions: mv
1

Very helpful when you've got complex filenames and needs to change just some small parts of it.

Renaming a file called "i-made-a-small-typo-right-here" to "i-made-a-big-typo-right-here":

mv -vi i-made-a-{small,big}-typo-right-here

You could also copy multiple files, edit, remove, process, etc.

SERVER: nc -l p 666 CLIENT: telnet -l -p 666
2009-08-14 17:01:57
User: Cowboy
Functions: telnet
1

This will start a netcat process listening on port 666. If you are able connect to your your server, netcat will receive the data being sent and spit it out to the screen (it may look like random garbage, so you might want to redirect it to a file).

mkdir 0{0..9}{0..9};mv 000 100
2009-08-14 16:33:20
User: sitaram
Functions: mkdir
-4

no external commands, but can only do 0-99, not 1-100, so we adjust it later

host -t a dartsclink.com | sed 's/.*has address //'
ps -C command
2009-08-14 15:30:42
User: recursiverse
Functions: ps
5

preferred way to query ps for a specific process name (not supported with all flavors of ps, but will work on just about any linux afaik)

uuid
2009-08-14 14:02:37
-1

http://en.wikipedia.org/wiki/Universally_Unique_Identifier

http://www.ossp.org/pkg/lib/uuid/

You'll first need to install the uuid package. Available immediately from apt in Ubuntu, Debian, or other myriad Debian variants. You could always compile it from source as well.

IFS=:; find $PATH | grep pattern
2009-08-14 13:38:58
User: camspiers
Functions: find grep
Tags: bash find grep
1

Best to put it in a file somewhere in your path. (I call the file spath)

#!/bin/bash

IFS=:; find $PATH | grep $1

Usage: $ spath php

watch -n 10 'du -sk testfile'
2009-08-14 12:35:21
User: ianux
Functions: watch
2

watch is a command especially designed for doing this job

while true ; do du -sk testfile ; sleep 10 ; done
2009-08-14 11:39:52
User: Solaris
Functions: du sleep true
-2

This command produces the output of "du -sk testfile" in every 10 seconds. You can change the command to be whatever you want.

mkdir $(printf '%03d\n' {1..100})
wget -O - -q icanhazip.com
2009-08-14 10:24:30
User: andrepuel
Functions: wget
5

I dont have curl or links installed, so I use wget with write file as standard out.

host -t mx foo.org
2009-08-14 09:55:19
User: peshay
Functions: host
1

command is shorter, output unnecessary longer

sed -e :a -e 's/<[^>]*>//g;/</N;//ba' index.html
2009-08-14 09:55:00
User: Blackbit
Functions: sed
6

This regular expression removes all HTML-Tags from the file.

egrep "^lease" /var/lib/dhcp/db/dhcpd.leases |awk '{ print $2 }'
2009-08-14 07:31:00
User: peshay
Functions: awk egrep
0

You can use that to create a excludefile for nmap, to find hosts, with no DHCP lease in your DHCP range.

smartypants | php -r "echo mb_decode_numericentity(file_get_contents('php://stdin'),array(0x0000,0xFFFF,0x0000,0xFFFF),'UTF-8');"
2009-08-14 04:02:38
User: infinull
-2

Turns regular quotes into curly quotes, also converts hyphens to dashes using a heuristic and outputs the result as UTF-8, suitable to copy/paste into wordprocessor.

requires: http://daringfireball.net/projects/smartypants/ (which does most of the work)

(I renamed smartypants.pl to smartypants before adding it to my $PATH)

Also requrires PHP with the multibyte module installed (its installed by default with PHP, but the sysadmin on one server I use disabled it... presumadly to increase performance or something).

git show --relative --pretty=format:'' --name-only HASH
2009-08-14 03:36:03
User: lingo
Tags: git files list
2

Lists ONLY the files changed by the given HASH/HEAD/list of hashes, etc. The message, commit ID, author, etc. is not included

rename 's/^/prefix/' *
2009-08-14 03:26:45
User: lingo
Functions: rename
12

Best to try first with -n flag, to preview

wget -qO - http://www.commandlinefu.com/feed/tenup | xmlstarlet sel -T -t -o '&lt;x&gt;' -n -t -m rss/channel/item -o '&lt;y&gt;' -n -v description -o '&lt;/y&gt;' -n -t -o '&lt;/x&gt;' | xmlstarlet sel -T -t -m x/y -v code -n
2009-08-14 02:44:00
User: fsilveira
Functions: wget
0

This lengthy cryptic line will print the latest top 10 commandlinefu.com posts without their summaries. To print also their respective summaries use the following (even bigger) command line:

wget -qO - http://www.commandlinefu.com/feed/tenup | xmlstarlet sel -T -t -o '<doc>' -n -t -m rss/channel/item -o '<item>' -n -o '<title>' -v title -o '</title>' -n -o '<description>' -v description -o '</description>' -n -o '</item>' -n -t -o '</doc>' | xmlstarlet sel -T -t -m doc/item -v description/code -n -v title -n -n

It is recommended to include this line into a shell script to be easily run, as I do myself. You could also use the following URLs to browse the top 3 commands:

wget -qO - http://www.commandlinefu.com/feed/threeup | xmlstarlet ...

.. or all others:

wget -qO - http://feeds2.feedburner.com/Command-line-fu | xmlstarlet ...

PS: You need to install "xmlstarlet" to run it. It is found in Debian APT repositories (apt-get install xmlstarlet) or under the http://xmlstar.sourceforge.net/ URL.

tr " " "\n" | nl
2009-08-13 14:36:18
User: dendoes
Functions: tr
1

translate and number lines is simpler and you use tr to choose your delimiter (eg for csv files)

id="dMH0bHeiRNg";mplayer -fs http://youtube.com/get_video.php?video_id=$id\&t=$(curl -s http://www.youtube.com/watch?v=$id | sed -n 's/.*, "t": "\([^"]*\)", .*/\1/p')
2009-08-13 14:16:01
User: matthewbauer
Functions: id sed
12

The original doesn't work for me - but this does. I'm guessing that Youtube updated the video page so the original doesn't work.

xwininfo
2009-08-13 14:15:39
User: jackhab
Tags: xwindows
2

A lot of X applications accept --geometry parameter so that you can set application size and position. But how can you figure out the exact arguments for --geometry? Launch an application, resize and reposition its window as needed, then launch xwininfo in a terminal an click on the application window. You will see some useful window info including its geometry.

perl -F' ' -MDate::Format -pale 'substr($_, index($_, $F[1]), length($F[1]), time2str("%C", $F[1]))' file.log
2009-08-13 13:57:33
Functions: perl
Tags: perl log
2

When you have one of those (log)files that only has epoch for time (since no one will ever look at them as a date) this is a way to get the human readable date/time and do further inspection.

Mostly perl-fu :-/

find . -type f -printf '%20s %p\n' | sort -n | cut -b22- | tr '\n' '\000' | xargs -0 ls -laSr
2009-08-13 13:13:33
User: fsilveira
Functions: cut find ls sort tr xargs
Tags: sort find ls
10

This command will find the biggest files recursively under a certain directory, no matter if they are too many. If you try the regular commands ("find -type f -exec ls -laSr {} +" or "find -type f -print0 | xargs -0 ls -laSr") the sorting won't be correct because of command line arguments limit.

This command won't use command line arguments to sort the files and will display the sorted list correctly.

git checkout -
2009-08-13 10:43:08
User: sunny256
3

Very useful if you keep switching between the same two branches all the time.

pwdx pid