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
tar -C <source_dir> -cf . | tar -C <dest_dir> -xf
:!>test.txt
get_absolute_path() { echo $1 | sed "s|^\([^/].*/.*\)|$(pwd)/\1|;s|^\([^/]*\)$|$(which -- $1)|;s|^$|$1|"; }
2011-09-13 11:06:55
User: keymon
Functions: echo sed
-4

It will return the absolute location of the called a script. If is in $PATH, it will search it using which.

You can combine this function with this other one: http://www.commandlinefu.com/commands/view/9252/readlink-equivalent-using-shell-commands-and-following-all-links, to get a way to know where is the real location of a called script:

# Returns the realpath of a called command. whereis_realpath() { local SCRIPT_PATH=$(whereis $1); myreadlink ${SCRIPT_PATH} | sed "s|^\([^/].*\)\$|$(dirname ${SCRIPT_PATH})/\1|"; }
ip a
perl -e 'print crypt("PASSWORD",int(rand(128))).$/;'
wget -q -O- http://www.gutenberg.org/dirs/etext96/cprfd10.txt | sed '1,419d' | tr "\n" " " | tr " " "\n" | perl -lpe 's/\W//g;$_=lc($_)' | grep "^[a-z]" | awk 'length > 1' | sort | uniq -c | awk '{print $2"\t"$1}'
2009-05-04 16:00:39
User: alperyilmaz
Functions: awk grep perl sed sort tr uniq wget
-4

This command might not be useful for most of us, I just wanted to share it to show power of command line.

Download simple text version of novel David Copperfield from Poject Gutenberg and then generate a single column of words after which occurences of each word is counted by sort | uniq -c combination.

This command removes numbers and single characters from count. I'm sure you can write a shorter version.

grep '' -m X file1 > file2
2009-03-22 04:34:43
User: sardanapalos
Functions: grep
-4

It extracts X number of lines from file1 and dumps them to file2.Using grep with the empty string '' extracts the complete lines (i.e. no filtering takes place) and the -m flag indicates how many lines to extract out from the given file. Then using the redirect > operator we send the extracted lines to a new file.

while (( $i != 0 )) { smbstatus; sleep 5; clear }
2009-06-03 13:26:30
Functions: clear sleep
Tags: unix samba zsh
-4

See smbstatus Output within a 5 second interval (for monitoring smb access)

svn status app/models/foo.rb; svn commit -m "Changed file" !$
2009-03-22 23:14:06
User: ggoodale
-4

After a command is run in bash, !$ is set to the last (space-delimited) argument of the command. Great for running several commands against the same file in a row.

sh default values
PID=httpd ; ps aux | grep $PID | grep -v grep | awk '{print $2}' | xargs kill -9
2012-02-21 23:27:47
User: esaenz
Functions: awk grep kill ps xargs
-4

# define user pid to kill

PID=httpd ;

# kill all pids

ps aux | grep $PID | grep -v grep | awk '{print $2}' | xargs kill -9

if lynx --dump http://twitter.com/xmuda | grep -q "Sorry, that page does"; then echo "Dont Exist"; else echo "Exist"; fi
2013-03-23 16:12:24
User: xmuda
Functions: echo grep
-4

I use these command to validate twitter accounts, we can use a "for a in $(cat list.txt)" to validate a complete list of twitter accounts.

dm ?
2010-03-26 02:42:21
User: rootgeek
Tags: foundry
-4

The sample output, is the command with a ?, to show you all the stuff you can look at.

Place a filename at the beginning of the line to make it easier to edit the search at the end of the command.
alias w3m='w3m -cookie'
ntfsfix /dev/hda1
2011-07-02 17:49:48
User: 0disse0
Tags: ntfs
-4

This command marks it as "dirty" NTFS partition, forcing Windows to do the scandisk during boot. It is convenient if you work hard to NTFS partitions under Linux

newhostname=$(hostname | awk -F. '{print $1 "." $2}'); ipaddress=$(nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F. '{print $3 "." $4}' | grep -v 64.142);PS1="[`id -un`.$newhostname.$ipaddress]"' (${PWD}): '; export PS1
2009-02-16 20:11:53
User: simardd
-4

changes the PS1 to something better than default.

[username.hostname.last-2-digits-of-ip] (current directory)

uuencode -m <filename> <filename>
2009-12-22 15:28:59
User: leprasmurf
Functions: uuencode
-4

I have come across multiple situations where I could only get to a server from a very restrictive bounce point which would not allow file transfers. I was able to uuencode a file, copy the output to a text file over putty, and then uudecode the file to transfer it. This works with any kind of file.

grep --exclude-dir=.svn --exclude=*~ -i "TODO" -rl .
sed '1,55d'
2009-02-16 20:36:25
User: grep
Functions: sed
-4

deletes first 55 lines; change the 55 to whatever number

ping -c 10 hostname
2009-03-04 06:14:52
User: haivu
Functions: ping
Tags: Network
-4

On Linux and Mac systems (I have not tested with other Unix systems), the ping command will keep on pinging until the user interrupts it with Ctrl+C. On Windows system, ping will execute for a number of times then quit. The -c flag on Linux and Mac will make this happen

netstat -ntu | awk ' $5 ~ /^[0-9]/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
2011-07-04 20:23:21
User: letterj
Functions: awk cut netstat sort uniq
Tags: netstat
-4

netstat has two lines of headers:

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

Added a filter in the awk command to remove them

find . -name .svn -type d | parallel rm -rf
w ; pkill -kill -t pts/1
marbles () { c=''; for i in $(seq $1); do c+='{b,r}'; done; x=$(eval echo $c); p=''; for i in $(seq $2); do p+='b*r'; done; y=$(grep -wo "${p}b*" <<< $x); wc -l <<< "$y"; grep -vc 'rr' <<< "$y"; }
2010-08-27 23:04:33
User: quintic
Functions: echo eval grep seq wc
-4

Suppose you have 11 marbles, 4 of which are red, the rest being blue. The marbles are indistinguishable, apart from colour. How many different ways are there to arrange the marbles in a line? And how many ways are there to arrange them so that no two red marbles are adjacent?

There are simple mathematical solutions to these questions, but it's also possible to generate and count all possibilities directly on the command line, using little more than brace expansion, grep and wc!

The answer to the question posed above is that there are 330 ways of arranging the marbles in a line, 70 of which have no two red marbles adjacent. See the sample output.

To follow the call to marbles 11 4: after c=''; for i in $(seq $1); do c+='{b,r}'; done;, $c equals {b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}

After x=$(eval echo $c), and brace expansion, $x equals bbbbbbbbbbb bbbbbbbbbbr ... rrrrrrrrrrb rrrrrrrrrrr, which is all 2^11 = 2048 strings of 11 b's and r's.

After p=''; for i in $(seq $2); do p+='b*r'; done;, $p equals b*rb*rb*rb*r

Next, after y=$(grep -wo "${p}b*"

Finally, grep -vc 'rr'