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

Commands by putnamhill from sorted by
Terminal - Commands by putnamhill - 75 results
echo $(openssl rand 4 | od -DAn)
ontouchdo(){ while :; do a=$(stat -c%Y "$1"); [ "$b" != "$a" ] && b="$a" && sh -c "$2"; sleep 1; done }
2010-10-22 23:25:12
User: putnamhill
Functions: sh sleep stat
Tags: stat
10

This is useful if you'd like to see the output of a script while you edit it. Each time you save the file the command is executed. I thought for sure something like this already exists - and it probably does. I'm on an older system and tend to be missing some useful things.

Examples:

ontouchdo yourscript 'clear; yourscript somefiletoparse'

Edit yourscript in a separate window and see new results each time you save.

ontouchdo crufty.html 'clear; xmllint --noout crufty.html 2>&1 | head'

Keep editing krufty.html until the xmllint window is empty.

Note: Mac/bsd users should use stat -f%m. If you don't have stat, you can use perl -e '$f=shift; @s=stat($f); print "$s[9]\n";' $1

tr A-Z a-z | tr -cs a-z '\n' | sort | uniq -c
cycle(){ while :;do((i++));echo -n "${3:$(($i%${#3})):1}";sleep .$(($RANDOM%$2+$1));done;}
2010-10-08 23:45:40
User: putnamhill
Functions: echo sleep
Tags: sleep random
1

Cycles continuously through a string printing each character with a random delay less than 1 second. First parameter is min, 2nd is max. Example: 1 3 means sleep random .1 to .3. Experiment with different values. The 3rd parameter is the string. The sleep will help with battery life/power consumption.

cycle 1 3 $(openssl rand 100 | xxd -p)

Fans of "The Shining" might get a kick out of this:

cycle 1 4 ' All work and no play makes Jack a dull boy.'
check(){ curl -sI $1 | sed -n 's/Location: *//p';}
tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
2010-09-23 02:31:12
User: putnamhill
Functions: sed
Tags: sed xxd openssl
14

Use the following variation for FreeBSD:

openssl rand 6 | xxd -p | sed 's/\(..\)/\1:/g; s/:$//'
ssh user@host 'gunzip - > file' < file.gz
2010-09-20 14:04:47
User: putnamhill
Functions: ssh
Tags: ssh gunzip
3

This version transfers gzipped data which is unzipped as it arrives at the remote host.

find . -iname \*.[ch] -exec indent "{}" \;
2010-09-10 13:25:10
User: putnamhill
Functions: find
-1

This version uses the indent C formatting utility. Doesn't appear to be included with ubuntu. But CentOS and MacOS have it.

jot -b '' 100
nc -zw2 www.example.com 80 || echo http service is down | mail -s 'http is down' admin@example.com
2010-09-08 13:18:16
User: putnamhill
Functions: echo mail
1

This version uses netcat to check a particular service.

lynx -dump http://www.domain.com | awk '/http/{print $2}'
find . -type f -exec fgrep -l $'\r' "{}" \;
2010-08-20 23:26:56
User: putnamhill
Functions: fgrep find
Tags: find fgrep
1

Looking for carriage returns would also identify files with legacy mac line endings. To fix both types:

perl -i -pe 's/\r\n?/\n/g' $(find . -type f -exec fgrep -l $'\r' "{}" \;)
man $(ls /bin | sed -n $((RANDOM % $(ls /bin | wc -l) + 1))p)
2010-08-20 17:15:33
User: putnamhill
Functions: ls man sed wc
Tags: man sed ls wc random
-2

Great idea camocrazed. Another twist would be to display a different man page based on the day of the year. The following will continuously cycle through all man pages:

man $(ls /bin | sed -n $(($(date +%j) % $(ls /bin | wc -l)))p)
ls | sed 's/.*/"&"/'
2010-08-17 15:38:51
User: putnamhill
Functions: ls sed
Tags: sed ls
-5

Looks like you're stuck with sed if your ls doesn't have a -Q option.

curl -sL 'www.commandlinefu.com/commands/random' | awk -F'</?[^>]+>' '/"command"/{print $2}'
2010-08-13 11:42:42
User: putnamhill
Functions: awk
Tags: awk curl random
0

Splitting on tags in awk is a handy way to parse html.

find /path/to/dir -type f -name '*.*' | sed 's@.*/.*\.@.@' | sort | uniq
2010-08-12 15:48:54
User: putnamhill
Functions: find sed sort
1

If your grep doesn't have an -o option, you can use sed instead.

netstat -rn | awk '/UG/{print $2}'
2010-08-09 15:56:02
User: putnamhill
Functions: awk netstat
-1

Tested on CentOS, Ubuntu, and MacOS.

ls -d $PWD/*
echo -n 'text' | perl -pe 's/(.)/sprintf("\\x%x", ord($1))/eg'
2010-07-14 12:20:42
User: putnamhill
Functions: echo perl
Tags: perl hex ascii
1

Here's a version that uses perl. If you'd like a trailing newline:

perl -pe 's/(.)/sprintf("\\x%x", ord($1))/eg; END {print "\n"}'
vimlint(){ eval $(xmllint --noout "$1" 2>&1 | awk -F: '/parser error/{print "vim \""$1"\" +"$2; exit}'); }
2010-06-23 15:55:02
User: putnamhill
Functions: awk eval
1

Validate a file using xmllint. If there are parser errors, edit the file in vim at the line of the first error.

echo -e "GET /ip HTTP/1.0\nUser-Agent: netcat\nHOST: ifconfig.me\n\n" | nc ifconfig.me 80 | sed -n '/^[0-9]/p'
2010-06-16 19:08:05
User: putnamhill
Functions: echo sed
-2

Here's a version that uses netcat (although I'd much rather use curl!).

find . -type d -exec du -sk '{}' \; | awk '($1 < 2048) {print $2}'
2010-06-16 11:53:14
User: putnamhill
Functions: awk du find
4

Just shortened the awk a bit and removed sed. Edit: I'm assuming there are no spaces in the path. To support white space in pathname try:

awk '($1 < 2048) {sub(/^[0-9]+[ \t]+/,""); print $0}'
ls -rl --time-style=+%s * | sed '/^$/,/^total [0-9]*$/d' | sort -nk6
perl -e 'foreach (@ARGV) {@T=stat($_); print localtime($T[8])." - ".$_."\n"}'