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 rkulla from sorted by
Terminal - Commands by rkulla - 21 results
curl http://github.com/api/v1/yaml/search/vim
2010-05-30 00:29:03
User: rkulla
Tags: git curl github
2

In this example we search for 'vim' but vim doesn't have a project on github right now. That's ok, this command still searches for every project that has 'vim' in their description (forks, plugins, etc). To get XML or JSON output just replace 'yaml' in the url with 'xml' or 'json'.

curl http://github.com/api/v1/yaml/git
2010-05-30 00:18:00
User: rkulla
Tags: git curl github
2

In this example 'git' is the user name and the output format is YAML but you can change this to XML or JSON, eg: curl http://github.com/api/v1/json/usernamehere

ack -a -G '^(?!.*bar/data.*).*$' pattern
2010-05-10 00:13:11
User: rkulla
0

Say you have a directory structure like "foo/, foo/data/, bar/, bar/data/". If you just want to ignore 'bar/data' and you use "ack --ignore-dir=data pattern" it will ignore both foo/data and bar/data and 'ignore-data=bar/data' etc won't work.

ubuntu-bug
2010-05-02 20:24:32
User: rkulla
2

As of 10.04 LTS, you need to use this command-line to reports bugs to the launchpad.net tracking system (you need a launchpad acct for this to work). This command is preferred over using the website because it collects/sends info about your system to help developers.

ubuntu-bug is a symlink to apport-bug which sees if KDE/Gnome is running and calls apport-gtk/apport-kde dialogs, otherwise apport-cli, so you can fill out a bug report.

First run 'ubuntu-bug' without args to see a list of known symptoms. If there's no matching symptom, or you know which package is to blame, then run 'ubuntu-bug <package>'. If the process is still running, use 'ubuntu-bug <PID>'

url="[Youtube URL]"; echo $(curl ${url%&*} 2>&1 | grep -iA2 '<title>' | grep '-') | sed 's/^- //'
2010-04-29 02:03:36
User: rkulla
Functions: echo grep sed
2

There's another version on here that uses GET but some people don't have lwp-request, so here's an alternative. It's also a little shorter and should work with most youtube URLs since it truncates at the first &

find . \( -iname '*.[ch]' -o -iname '*.php' -o -iname '*.pl' \) -exec wc -l {} \; | sort
2010-04-28 07:18:21
User: rkulla
Functions: find wc
Tags: find count code
2

Gives you a nice quick summary of how many lines each of your files is comprised of. (In this example, we just check .c, .h, .php and .pl). Since we just use wc -l to count, you'll just get a very rough estimate of how many lines of actual code there are. Use a more sophisticated algorithm instead if you need to.

strace php tias.php -e open,access 2>&1 | grep foo.txt
2010-04-20 19:42:42
User: rkulla
Functions: grep strace
7

Sometimes a program refuses to read a file and you're not sure why. You may have display_errors turned off for PHP or something. In this example, fopen('/var/www/test/foo.txt') was called but doesn't have read access to foo.txt.

Strace can tell you what went wrong. E.g., if php doesn't have read access to the file, strace will say "EACCESS (Permission denied)". Or, if the file path you gave doesn't exist, strace will say "ENOENT (No such file or directory)", etc.

This works for any program you can run from the command-line, e.g., strace python myapp.py -e open,access...

Note: the above command uses php-cli, not mod_php, which is a different SAPI with diff configs, etc.

zsync -i existing-file-on-disk.iso http://example.com/new-release.iso.zsync
2010-04-20 07:02:37
User: rkulla
2

Zsync is an implementation of rsync over HTTP that allows updating of files from a remote Web server without requiring a full download. For example, if you already have a Debian alpha, beta or RC copy downloaded, zsync can just download the updated bits of the new release of the file from the server.

This requires the distributor of the file to have created a zsync build control file (using zsyncmake).

sudo powertop
2010-04-19 21:59:29
User: rkulla
Functions: sudo
3

Run this command as root to get enough stats. It works on AMD and Intel machines, including desktops. If ran on a laptop it'll give you suggestions on extending your battery life.

You'll need to install PowerTOP if you don't have, via 'apt-get install powertop', etc.

To grep the output use: sudo powertop -d | grep ...

The many command suggestions PowerTOP gives you alone will increase your command-line fu!

APP=wine; if [ $(sudo apt-get --print-uris -y install $APP | sed -ne 's/^After this operation, \([0-9]\{1,\}\).*MB.*/\1/p') -gt 50 ]; then gnometris 2>/dev/null & sudo apt-get install $APP; else sudo apt-get install $APP; fi
2010-04-18 19:32:26
User: rkulla
Functions: install sed sudo
2

Change the APP variable's value to whatever you want to install. Depending on how fast your machine is, you'll want to adjust the value 50 to something else. You might also want to play a different game than Gnometris - just make sure it's a GUI game.

vim -r 2>&1 | grep '\.sw.' -A 5 | grep 'still running' -B 5
2010-04-17 19:43:35
User: rkulla
Functions: grep vim
3

Catches .swp, .swo, .swn, etc.

If you have access to lsof, it'll give you more compressed output and show you the associated terminals (e.g., pts/5, which you could then use 'w' to figure out where it's originating from): lsof | grep '\.sw.$'

If you have swp files turned off, you can do something like: ps x | grep '[g,v]im', but it won't tell you about files open in buffers, via :e [file].

mplayer -playlist <(find $PWD -type f)
2010-04-17 00:20:08
User: rkulla
Functions: find
0

Press > or < to go to the next or previous track. Space to toggle play/pause, etc.

It creates a temp file descriptor. To see where the file descriptor gets created type: echo <(echo foo)

This works better than running find first, then piping to mplayer with xargs or something, because that won't let you use keyboard shortcuts.

printf "\n%25s%10sTOTAL\n" 'FILE TYPE' ' '; for ext in $(find . -iname \*.* | egrep -o '\.[^[:space:].]+$' | egrep -v '\.svn*' | sort -f | uniq -i); do count=$(find . -iname \*$ext | wc -l); printf "%25s%10s%d\n" $ext ' ' $count; done
2010-04-16 21:12:11
User: rkulla
Functions: egrep find printf sort uniq wc
0

I created this command to give me a quick overview of how many file types a directory, and all its subdirectories, contains. It works based off file extension, rather than file(1)'s magic output, because it ended up being more accurate and less confusing.

Files that don't have an ext (README) are generally not important for me to want to count, but you're free to customize this fit your needs.

pv -cN orig < foo.tar.bz2 | bzcat | pv -cN bzcat | gzip -9 | pv -cN gzip > foo.tar.gz
2010-04-16 05:21:10
User: rkulla
Functions: gzip
0

In this example we convert a .tar.bz2 file to a .tar.gz file.

If you don't have Pipe Viewer, you'll have to download it via apt-get install pv, etc.

zenity --title passwd --width 800 --height 600 --text-info --filename /etc/passwd
2010-04-16 04:20:52
User: rkulla
Functions: passwd
2

I use zenity because it's a rewrite of gdialog and also replaces gmessage and has more useful options.

Using --text-info allows you to select and copy the text to your clipboard.

To see a file in a list dialog: cat /etc/passwd | zenity --width 800 --height 600 --list --column Entries

If you don't have zenity, you'll have to download it via apt-get install zenity, etc.

echo 'obase=16; C+F' | bc
2010-04-14 20:35:31
User: rkulla
Functions: echo
Tags: hex bc math asm
4

To do hex to binary: echo 'ibase=16; obase=2; 16*16' | bc # prints: 111100100

To do 16*16 from decimal to hex: echo 'ibase=10; obase=16; 16*16' | bc # prints: 100

You get the idea... Alternatively, run bc in interactive mode (see man page)

truecrypt volume.tc
2010-04-14 18:34:09
User: rkulla
2

This should automatically mount it to /media/truecrypt1. Further mounts will go to /media/truecrypt2, and so on. You shouldn't need sudo/su if your permissions are right.

I alias tru='truecrypt' since tr and true are commands.

To explicitly create a mount point do: tru volume.tc /media/foo

To make sure an GUI explorer window (nautilus, et al) opens on the mounted volume, add: --explorer

To see what you currently have mounted do: tru -l

To dismount a volume do: tru -d volume.tc. To dismount all mounted volumes at once do: tru -d

Tested with Truecrypt v6.3a / Ubuntu 9.10

tree -C | less -R
vim -o file1 file2...
2010-04-13 22:09:47
User: rkulla
Functions: vim
Tags: vim split
7

-o acts like :spit. Use -O (capital o) for side-by-side like :vsplit. Use vim -d or vimdiff if you need a diff(1) comparison.

To split gnu Screen instead of vim, use ^A S for horizontal, ^A | for vertical.

screen -raAd
2010-04-12 22:54:58
User: rkulla
Functions: screen
11

By default, screen tries to restore its old window sizes when attaching to resizable terminals. This command is the command-line equivalent to typing ^A F to fit an open screen session to the window.

vim $(find . ! -path \*.svn\* -type f -iname \*foo\*)
2010-04-11 23:32:41
User: rkulla
Functions: find vim
Tags: vim find
2

This command searches the current directory, and all of its subdirs, for files that have the string "foo" in their filename (foo.c, two-foo.txt, index-FOO-bar.php, etc), and opens them in Vim. It ignores any hidden .svn directories. Change -iname to -name if you want to do case-sensitive matches.

Files open in buffers by default, so to verify that the correct files were opened, type ":list". You can load all the files in tabs by doing ":tab ball" or use 'vim -p' on the command-line to load files straight to tabs.

If you get permission denied errors, do: vim $(find . ! -path \*.svn\* -type -f iname \*foo\* 2>/dev/null)

To narrow it down to a single file extension, such as .php files, use \*foo\*.php (or '*foo*.php'. Which ever you prefer)