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 dooblem from sorted by
Terminal - Commands by dooblem - 26 results
convert image1 image2 -resize '400x300!' MIFF:- | compare -metric AE -fuzz '10%' - null:
2012-04-17 11:25:34
User: dooblem
2

Outputs the number of different pixels.

2 params to increase tolerance:

* thumbnails size

* fuzz, the color distance tolerance

See http://en.positon.org/post/Compare-/-diff-between-two-images for more details.

ifconfig | grep -o "inet [^ ]*" | cut -d: -f2
2010-12-06 10:36:52
User: dooblem
Functions: cut grep ifconfig
Tags: ifconfig grep cut
-2

This is what we use.

You can grep -v 127.0.0.1 if you wish.

zcat -f $(ls -tr access.log*)
2010-10-30 22:54:54
User: dooblem
Functions: ls zcat
Tags: logs zcat
1

with zcat force option it's even simpler.

inotifywait -mrq -e CREATE --format %w%f /path/to/dir | while read FILE; do chmod g=u "$FILE"; done
2010-10-21 23:36:02
User: dooblem
Functions: chmod read
6

Listens for events in the directory. Each created file is displayed on stdout. Then each fileline is read by the loop and a command is run.

This can be used to force permissions in a directory, as an alternative for umask.

More details:

http://en.positon.org/post/A-solution-to-the-umask-problem%3A-inotify-to-force-permissions

shuf SOMEFILE
man $(ls -1 /usr/share/man/man?/ | shuf -n1 | cut -d. -f1)
2010-08-20 23:36:10
User: dooblem
Functions: cut ls man
Tags: man sort shuf
-3

Another one.

Maybe not the quicker because of the sort command, but it will also look in other man sections.

updated with goodevilgenius 'shuf' idea

echo "uptime" | pee "ssh host1" "ssh host2" "ssh host3"
2010-08-20 11:42:40
User: dooblem
Functions: echo
Tags: ssh parallel pee
15

The pee command is in the moreutils package.

xargs -n1 -P100 -I{} sh -c 'ssh {} uptime >output/{} 2>error/{}' <hostlist
2010-08-20 11:03:11
User: dooblem
Functions: sh uptime xargs
3

Do the same as pssh, just in shell syntax.

Put your hosts in hostlist, one per line.

Command outputs are gathered in output and error directories.

find /path/to/dir -type f | grep -o '\.[^./]*$' | sort | uniq
getent hosts positon.org | cut -d' ' -f1
2010-08-11 09:06:39
User: dooblem
Functions: cut getent
Tags: getent
1

I just wanted a simple DNS request.

Because host and nslookup commands are not on all systems, we use getent instead.

Thanks aulem for that tip.

echo "$(od -An -N4 -tu4 /dev/urandom) % 5 + 1" | bc
echo $PWD/*
rsync -av --link-dest=$(ls -1d /backup/*/ | tail -1) /data/ /backup/$(date +%Y%m%d%H%M)/
2010-08-05 19:36:24
User: dooblem
Functions: date ls rsync tail
Tags: backup rsync
1

'data' is the directory to backup, 'backup' is directory to store snapshots.

Backup files on a regular basis using hard links. Very efficient, quick. Backup data is directly available.

Same as explained here :

http://blog.interlinked.org/tutorials/rsync_time_machine.html

in one line.

Using du to check the size of your backups, the first backup counts for all the space, and other backups only files that have changed.

find -type l -xtype d
ifconfig eth0 | grep -o "inet [^ ]*" | cut -d: -f2
zgrep -h "" `ls -tr access.log*`
2010-06-19 09:44:05
User: dooblem
Functions: zgrep
2

I use zgrep because it also parses non gzip files.

With ls -tr, we parse logs in time order.

Greping the empty string just concatenates all logs, but you can also grep an IP, an URL...

find ~/.thunderbird/*.default/ -name *.msf -exec rm -f {} \;
find -type f -print0 | xargs -r0 stat -c %y\ %n | sort
2010-05-29 13:40:18
User: dooblem
Functions: find stat xargs
2

Works with files containing spaces and for very large directories.

vix(){ vim +'w | set ar | silent exe "!chmod +x %" | redraw!' $@; }
2010-05-27 21:12:48
User: dooblem
Functions: ar set vim
Tags: vim vi chmod
2
vix /tmp/script.sh

Open a file directly with execution permission.

Put the function in your .bashrc

You can also put this in your vimrc:

command XX w | set ar | silent exe "!chmod +x %" | redraw!

and open a new file like this:

vi +XX /tmp/script.sh
tail -n0 -f access.log>/tmp/tmp.log & sleep 10; kill $! ; wc -l /tmp/tmp.log
2010-04-29 21:23:46
User: dooblem
Functions: kill sleep tail wc
Tags: tail kill wc sleep
1

Another way of counting the line output of tail over 10s not requiring pv.

Cut to have the average per second rate :

tail -n0 -f access.log>/tmp/tmp.log & sleep 10; kill $! ; wc -l /tmp/tmp.log | cut -c-2

You can also enclose it in a loop and send stderr to /dev/null :

while true; do tail -n0 -f access.log>/tmp/tmp.log & sleep 2; kill $! ; wc -l /tmp/tmp.log | cut -c-2; done 2>/dev/null

tail -f access.log | pv -l -i10 -r >/dev/null
2010-04-29 21:02:01
User: dooblem
Functions: tail
Tags: tail pv
6

Displays the realtime line output rate of a logfile.

-l tels pv to count lines

-i to refresh every 10 seconds

-l option is not in old versions of pv. If the remote system has an old pv version:

ssh tail -f /var/log/apache2/access.log | pv -l -i10 -r >/dev/null

very_long_command& sleep 10; kill $!
2010-04-29 20:43:13
User: dooblem
Functions: kill sleep
5

or "Execute a command with a timeout"

Run a command in background, sleep 10 seconds, kill it.

! is the process id of the most recently executed background command.

You can test it with:

find /& sleep10; kill $!

df -l | grep -e "9.%" -e "100%"
2010-04-26 17:57:54
User: dooblem
Functions: df grep
2

Reports all local partitions having more than 90% usage.

Just add it in a crontab and you'll get a mail when a disk is full.

(sending mail to the root user must work for that)

aptitude keep-all
2010-04-20 09:24:20
User: dooblem
1

Very handy if you have done a package selection mistake in aptitude.

Note that it's better to do a Ctrl+U (undo) in aptitude if possible, because the keep-all will clear some package states (like the 'hold' state).

dig +short myip.opendns.com @resolver1.opendns.com
2010-04-02 21:53:26
User: dooblem
Functions: dig
26

Instead of opening your browser, googling "whatismyip"...

Also useful for scripts.

dig can be found in the dnsutils package.