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,605 results
head -$(($RANDOM % $(wc -l < file.txt) +1 )) file.txt | tail -1
export HISTFILE=/dev/null/
2010-06-10 18:48:43
User: thund3rpants
Functions: export
-4

This command is great for dumping your history so that when you're working on a system that is under a shared account, you can keep others from reviewing your history file. I use this when I'm just visiting a system to diagnose a problem, or to make a quick fix.

ps aux | grep process-name | grep -v "grep"
dd if=/dev/sda of=~/backup-disk-YY-MM-DD.img
2010-09-14 11:14:14
Functions: dd
-4

Change /dev/sda with the Disk or partition you want to back-up

To restore use:

dd if=~/backup-disk-YY-MM-DD.img of=/dev/sda

Once again change /dev/sda accordingly

Source: http://www.go2linux.org/linux/2010/09/small-tip-back-disks-clone-hard-disk-773

/bin/ls *.png | xargs -l1 -I {} convert {} -modulate 100,100,70 ../../icons/32x32/{}
mvn -Dmaven.test.skip=true install
2009-05-20 12:55:10
User: sharfah
Tags: maven
-4

the maven.test.skip property can be added to other goals too.

ls | while read -r FILE; do mv -v "$FILE" `echo "prependtext$FILE" `; done
2010-08-14 14:19:18
User: IgnitionWeb
Functions: ls mv read
Tags: echo mv prepen
-4

Prepends all directory items with "prependtext"

for i in *; do mv $i prependtext$i; done
grep -iR find_me ./
2009-02-18 15:38:59
User: lfamorim
Functions: grep
-4

Searching for a String in Multiple Files With Grep

m-a a-i open-vm
2009-04-22 20:08:00
User: m03hr3
-4

compile and install openvm-tools module in debian lenny

finger @www.linuxbanks.cn | grep -oE '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | head -n1
2010-05-05 14:58:55
User: zhangweiwu
Functions: finger grep head
Tags: ip address
-4

This is useful when you got a reserved IP address like 192.168.0.100 and want to find out what IP address is used to access the Internet. You have to know a server with 'efingerd -n' configured, like www.linuxbanks.cn as above.

Other method to find out this information are for example access www.tell-my-ip.com and grep the output. The finger method have the advantage that it is easy to deploy a service like www.tell-my-ip.com, as you only need to get efingerd installed.

for /F %G in ('dir /b c:\Windows\system32\notepad.exe') do ( echo %G )
shuf file.txt | head -n 1
xterm -e "cd /my/directory; bash"
2009-10-13 12:06:14
User: kekschaot
-4

Usefull e.g. in krusader open terminal function

( cd /my/directory; xterm& )
2009-10-13 13:07:21
User: ashawley
Functions: cd
Tags: subshells
-4

Perfect time for the rarely used sub shell.

aptitude purge linux-image | grep ^i | grep -v $(uname -r)
sleep 4h && halt
uname -m # display machine "hardware name"
2013-01-04 11:46:43
User: mpb
Functions: uname
-4

Display the machine "hardware name" 32 or 64 bit.

"x86_64" is shown on 64 bit machines

"i686" is typically shown on 32 bit machines (although, you might also see "i386" or "i586" on older Linuxen).

On other "unix-like" systems, other hardware names will be displayed.

For example, on AIX, "uname -m" gives the "machine sequence number".

For whatever reason, IBM decided that "uname -M" would give the machine type and model.

(ref: http://www.ibm.com/developerworks/aix/library/au-aix-systemid.html )

On Sun Solaris, "uname -m" can be used to determine the chip type and "isainfo -v" will reveal

if the kernel is 64 or 32 bit.

(ref: http://www.ibiblio.org/pub/packages/solaris/sparc/html/32.and.64.bit.packages.html )

A more reliable way to determine "64-bit ness" across different Unix type systems is to compile the following simple C program:

cat <<eeooff > bits.c

/*

* program bits.c

* purpose Display "32" or "64" according to machine type

* written January 2013

* reference http://www.unix.org/whitepapers/64bit.html

*/

/* hmm, curious that angle-brackets removed by commandlinefu.com data input processing? */

#include "/usr/include/stdio.h"

long lv = 0xFFFFFFFF;

main ( ) {

printf("%2d\n",(lv < 0)?32:64);

}

eeooff

Compile and run thusly: cc -o bits bits.c; ./bits

<command> 2> <file>
ps aux | grep -v `whoami`
echo '#!'$(which bash) > script.sh
2012-02-06 08:25:27
User: sharfah
Functions: echo which
Tags: bash
-4

Writes out the shebang line (#!/bin/bash) to the script.

ls | xargs WHATEVER_COMMAND
2014-03-12 18:00:21
User: pdxdoughnut
Functions: ls xargs
-4

xargs will automatically determine how namy args are too many and only pass a reasonable number of them at a time. In the example, 500,002 file names were split across 26 instantiations of the command "echo".

php -r 'echo md5("password") . "\n";'
ls -l|awk '{print $6,$8}'|sort -d
2009-03-13 19:00:18
User: archlich
Functions: awk ls sort
-4

Can pipe to tail or change the awk for for file size, groups, users, etc.

10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 | grep '1 received' - || mail -ne -s'Host 192.168.0.14 not reachable' admin@example.com
2012-02-06 10:42:46
User: knoppix5
Functions: grep mail ping
-4

Every 20 minutes test if host with IP 192.168.0.14 is 'dead' or not reachable.

The line should be put in your crontab file.