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,588 results
time read (ctrl-d to stop)
2009-03-20 22:50:06
User: mrttlemonde
Functions: read time
124

time read -sn1 (s:silent, n:number of characters. Press any character to stop)

ls -1 static/images/ | while read line; do echo -n $line' '[; grep -rc $line *|grep -v ".svn"|cut -d":" -f2|grep -vc 0| tr "\n" -d; echo -n ]; echo ; done
2009-03-20 20:33:36
User: psytek
Functions: cut echo grep ls read tr
-5

This command will grep the entire directory looking for any files containing the list of files. This is useful for cleaning out your project of old static files that are no longer in use. Also ignores .svn directories for accurate counts. Replace 'static/images/' with the directory containing the files you want to search for.

curl -s -u user:password http://twitter.com/statuses/friends_timeline.rss | grep title | sed -ne 's/<\/*title>//gp' | head -n 4 | say -v Bruce
2009-03-20 20:14:13
User: sixtus
Functions: grep head sed
-7

You need curl.. and a Mac of course.

xvkbd -xsendevent -text "Hello world"
2009-03-20 18:58:05
User: Alanceil
15

This is a (last resort) way to automate applications that provide no other ways for automation, it would send 'Hello world' to the currently active window. See the manpage (and the -text and -window entries) for how to send special characters and target specific windows.

An example:

Using xwininfo, I get the id of my XPlanet background window:

alanceil@kvirasim:19:51:0:~> xwininfo

xwininfo: Please select the window about which you

would like information by clicking the

mouse in that window.

xwininfo: Window id: 0x3600001 "Xplanet 1.2.0"

Absolute upper-left X: 0

(..etc..)

Now I use xvkbd to tell it to close itself:

xvkbd -xsendevent -window 0x3600001 -text "Q"

Obviously, the best way is to put these commands in a shellscript - just make sure to include a short sleep (sleep .1 should suffice) after each xvkbd call, or some programs will become confused.

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -DgeneratePom=true
2009-03-20 16:29:08
User: virtualshock
Tags: maven java
0

If you have a JAR that is not in a central Maven repository this will add it to your repository.

set -o vi
2009-03-20 16:29:03
User: Viaken
Functions: set
Tags: readline vi
18

This lets you use your favorite vi edit keys to navigate your term.

To set it permanently, put "set editing-mode vi" in your ~/.inputrc or /etc/inputrc.

fc [history-number]
2009-03-20 15:09:43
User: haivu
Functions: fc
Tags: bash
8

If you would like to edit a previous command, which might be long and complicated, you can use the fc (I think it stands for fix command). Invoke fc alone will edit the last command using the default editor (specified by $FCEDIT, $EDITOR, or emacs, in that order). After you make the changes in the editor, save and exit to execute that command. The fc command is more flexible than what I have described. Please 'man bash' for more information.

CDPATH=:..:~:~/projects
2009-03-20 14:50:25
User: haivu
Tags: bash
81

CDPATH tells the cd command to look in this colon-separated list of directories for your destination. My preferred order are 1) the current directory, specified by the empty string between the = and the first colon, 2) the parent directory (so that I can cd lib instead of cd ../lib), 3) my home directory, and 4) my ~/projects directory.

watch 'iptables -vL'
2009-03-20 14:49:12
User: alanr723
Functions: watch
3

Watch the number of packets/bytes coming through the firewall. Useful in setting up new iptables rules or chains. Use this output to reorder rules for efficiency.

stty -F "/dev/ttyUSB0" 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke time 5 min 1 line 0
2009-03-20 14:48:32
User: Alanceil
Functions: stty time
1

I had a hard time in finding the correct settings to get reasonable output from a coin selector which sends its data over a serial line. In the end, minicom came to the rescue and pointed me on the right track.

So, if you need to do something similar, these settings may help you.

Replace ttyUSB0 with your device file, 9600 with your baud rate, 5 with your read timeout (10ths of a second), and 1 with the minimum numbers of characters you want to read.

You can then open the device file like you are used to do, example:

DATA="`xxd -ps -l 5 \"$DEV\"`"
sed /BEGIN/,/END/s/xxx/yyy/g input.txt
2009-03-20 14:34:32
User: kayowas
Functions: sed
0

Replace the words between BEGIN and END.

mount | column -t
2009-03-20 14:18:56
User: thechile
Functions: column mount
238

Particularly useful if you're mounting different drives, using the following command will allow you to see all the filesystems currently mounted on your computer and their respective specs with the added benefit of nice formatting.

nawk '{line[NR]=$0} END{for (; NR>=1; NR--){print line[NR]}}' FILENAME
echo ${file##*/}
ntpdate ntp.ubuntu.com pool.ntp.org
ls -l | sort -nk5
'ALT+.' or '<ESC> .'
2009-03-20 11:36:04
User: atoponce
254

When typing out long arguments, such as:

cp file.txt /var/www/wp-content/uploads/2009/03/

You can put that argument on your command line by holding down the ALT key and pressing the period '.' or by pressing <ESC> then the period '.'. For example:

cd 'ALT+.'

would put '/var/www/wp-content/uploads/2009/03/ as my argument. Keeping pressing 'ALT+.' to cycle through arguments of your commands starting from most recent to oldest. This can save a ton of typing.

ropened='p4 opened | awk -F# "{print \$1}" | p4 -x - revert'
alias opened='p4 opened | awk -F# "{print \$1}"'
2009-03-20 11:06:41
User: Alexander
Functions: alias awk
Tags: p4 SCM Perforce
0

Just type 'opened' and get all files currently opened for edit.

date --date=yesterday
alias pst='pstree -Alpha'
2009-03-20 10:53:37
User: Alexander
Functions: alias
Tags: Linux
0

By 'pst' you can print out process tree with all details about all processes (including a command line, PID, and the current process you are running in).

By 'pst username' you can get an information about processes belonging to the particular user 'username'.

alias ..='cd ..'
2009-03-20 09:57:28
User: eimantas
Functions: alias
Tags: bash unix shell cd
9

Alias two dots to move to parent directory. Put it into your .bashrc or .profile file.

$mkdir mydir -> mv !$ yourdir -> $cd !$
if [ "${vote}" = "down" ]; then echo leave comment; fi
2009-03-20 00:31:13
Functions: echo
-10

I think it would be wise if anyone voting down left a comment indicating the reason for that action. Don't keep it to yourself. Thanks.

cmp -l file1.bin file2.bin | wc -l