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,491 results
cat /etc/debian_version
2009-08-05 14:47:05
User: caiosba
Functions: cat
-5

Easy way to find out what Debian version your machine is running

find . -name "*.gz" | xargs -n 1 -I {} bash -c "gunzip -c {} | sort | gzip -c --best > {}.new ; rm {} ; mv {}.new {}"
2009-08-05 14:16:15
User: kennethjor
Functions: bash find xargs
-2

I used this because I needed to sort the content of a bunch of gzipped log files. Replace sort with something else, or simply remove sort to just rezip everything

find . -depth -type d -empty -exec rmdir -v {} +
2009-08-05 13:48:13
User: syssyphus
Functions: find rmdir
Tags: find
7

this will show the names of the deleted directories, and will delete directories that only no files, only empty directories.

cat file-that-failed-to-download.zip | curl -C - http://www.somewhere.com/file-I-want-to-download.zip >successfully-downloaded.zip
2009-08-05 13:33:06
Functions: cat
-1

If you are downloading a big file (or even a small one) and the connection breaks or times out, use this command in order to RESUME the download where it failed, instead of having to start downloading from the beginning. This is a real win for downloading debian ISO images over a buggy DSL modem.

Take the partially downloaded file and cat it into the STDIN of curl, as shown. Then use the "-C -" option followed by the URL of the file you were originally downloading.

grep -r . /sys/class/net/eth0/statistics
2009-08-05 08:20:39
User: olorin
Functions: grep
Tags: Linux
4

Within /proc and /sys there are a lot of subdirectories, which carry pseudofiles with only one value as content. Instead of cat-ing all single files (which takes quite a time) or do a "cat *" (which makes it hard to find the filename/content relation), just grep recursively for . or use "grep . /blabla/*" (star instead of -r flag).

For better readability you might also want to pipe the output to "column -t -s : ".

indent -linux helloworld.c
2009-08-05 07:53:08
User: freestyler
Tags: indent
6

put "-linux" option into $HOME/.indent.pro to make it default

curl -u user -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml
wget --spider -v http://www.server.com/path/file.ext
typeset -f <function-name>
2009-08-04 17:07:21
User: log0
1

Display the code of a previously defined shell function.

find . -empty -type d -exec rmdir {} +
2009-08-04 16:55:34
User: jsiei97
Functions: find rmdir
14

A quick way to find and delete empty dirs, it starts in the current working directory.

If you do find . -empty -type d you will see what could be removed, or to a test run.

tar cf - dir_to_cp/ | (cd path_to_put/ && tar xvf -)
2009-08-04 16:51:31
User: jsiei97
Functions: cd tar
1

Just a copy of a big dir when you wan't things like ownership and date etc etc to be untouched.

Note: Updated with the ideas from "mpb".

echo linux|rev
2009-08-04 12:28:44
Functions: echo
Tags: rev
1

NAME

rev - reverse lines of a file or files

SYNOPSIS

rev [file ...]

DESCRIPTION

The rev utility copies the specified files to the standard output, reversing the order of characters in every line. If no files are specified, the standard input is read.

AVAILABILITY

The rev command is part of the util-linux-ng package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.

exiftool '-Directory<DateTimeOriginal' -d %Y/%m/%d dir
2009-08-04 11:47:34
User: karel1980
Tags: exiftool
14

This command would move the file "dir/image.jpg" with a "DateTimeOriginal" of "2005:10:12 16:05:56" to "2005/10/12/image.jpg".

This is a literal example from the exiftool man page, very useful for classifying photo's. The possibilities are endless.

stat -f '%Sp %p %N' * | rev | sed -E 's/^([^[:space:]]+)[[:space:]]([[:digit:]]{4})[^[:space:]]*[[:space:]]([^[:space:]]+)/\1 \2 \3/' | rev
2009-08-04 08:45:20
User: vwal
Functions: rev sed stat
2

Since the original command (#1873) didn't work on FreeBSD whose stat lacks the "-c" switch, I wrote an alternative that does. This command shows also the fourth digit of octal format permissions which yields the sticky bit information.

rsync -a --delete --link-dest=../lastbackup $folder $dname/
2009-08-04 07:08:54
User: pamirian
Functions: rsync
6

dname is a directory named something like 20090803 for Aug 3, 2009. lastbackup is a soft link to the last backup made - say 20090802. $folder is the folder being backed up. Because this uses hard linking, files that already exist and haven't changed take up almost no space yet each date directory has a kind of "snapshot" of that day's files. Naturally, lastbackup needs to be updated after this operation. I must say that I can't take credit for this gem; I picked it up from somewhere on the net so long ago I don't remember where from anymore. Ah, well...

Systems that are only somewhat slicker than this costs hundreds or even thousands of dollars - but we're HACKERS! We don't need no steenkin' commercial software... :)

find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33 | cut -c 35-
2009-08-04 07:05:12
User: infinull
Functions: cut find md5sum sort uniq
17

Calculates md5 sum of files. sort (required for uniq to work). uniq based on only the hash. use cut ro remove the hash from the result.

touch $2;firefox -print $1 -printmode PNG -printfile $2
2009-08-04 06:55:25
User: pamirian
Functions: touch
9

This requires the command-line print extension (see #2861 for more details). I use it to make up complex images with formatted text using CSS and whatnot. It's a lot slicker than imagemagick for certain things. Now imagine using a local webserver with PHP and a database to generate the images. Oh, the possibilities...

touch $2;firefox -print $1 -printmode PDF -printfile $2
2009-08-04 06:40:55
User: pamirian
Functions: touch
5

This uses the "command-line print" plugin for Firefox (http://torisugari.googlepages.com/commandlineprint2). This same plugin can also produce PNGs. On *nix, the file must exist; therefore the touch bit in front. Also, firefox seems to ignore saved user preferences when "printing" this way (margins, header, footer, etc.), so I had to tweak my ~/.mozilla/firefox/xxxxxxxx.default/prefs.js file by hand. Yup, that's *prefs.js* not user.js - apparently, firefox ignores my user.js file too...

cat frame/*.mpeg | ffmpeg -i $ID.mp3 -i - -f dvd -y track/$ID.mpg 2>/dev/null
2009-08-04 06:31:50
User: pamirian
Functions: cat
5

This is an extract from a larger script which formats the video for DVD. The videos I use have no audio track so I need to add one. Tweak as you like...

ffmpeg -loop_input -f image2 -r 30000/1001 -t $seconds -i frame/$num.ppm -y frame/%02d.mpeg 2>/dev/null
2009-08-04 06:25:47
User: pamirian
4

This is an extract from a larger script which makes up slideshow videos from images. $seconds is the number of seconds the video will last, and $num is a loop counter which numbers the videos for concat into a longer video later, so they will be in order. The dev/null bit on the end cuts ffmpeg's verbosity.

ifconfig | grep "0xffffffff" | awk '{ print $2 }' | xargs -n 1 ifconfig em0 delete
2009-08-04 05:18:36
User: vwal
Functions: awk grep ifconfig xargs
3

The example command deletes all aliases for network interface 'em0' assuming that the aliases have netmask of 255.255.255.255 and the master IP has some other netmask (such as 255.255.255.0). See here -> http://my.galagzee.com/2009/07/22/deleting-all-network-interface-aliases/ for more on the rationale of this command.

printf '\!:1\0\!:1\0\!:2' | mmencode | tr -d '\n' | sed 's/^/AUTH PLAIN /'
2009-08-04 05:04:50
User: vwal
Functions: printf sed tr
4

I use this as an alias:

alias authplain "printf '\!:1\0\!:1\0\!:2' | mmencode | tr -d '\n' | sed 's/^/AUTH PLAIN /'"

then..

# authplain someuser@somedomain.com secretpassword

AUTH PLAIN c29tZXVzZXJAc29tZWRvbWFpbi5jb20Ac29tZXVzZXJAc29tZWRvbWFpbi5jb20Ac2VjcmV0cGFzc3dvcmQ=

#

curl -s http://isc.sans.org/sources.html|grep "ipinfo.html"|awk -F"ip=" {'print $2'}|awk -F"\"" {'print $1'}|xargs -n1 sudo iptables -A OUTPUT -j DROP -d > 2&>1
git --no-pager whatchanged -1 --pretty=medium <commit_hash>
alias z='zenity --info --text="You will not believe it, but your command has finished now! :-)" --display :0.0'
2009-08-03 16:38:29
User: bubo
Functions: alias
4

tired of switching to the console to check if some command has finished yet? if notify-send does not work on your box try this one... e.g. rsync -av -e /usr/bin/lsh $HOME slowconnection.bar:/mnt/backup ; z (now fire up X, do something useful, get notified if this stuff has finished).