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 splante from sorted by
Terminal - Commands by splante - 8 results
get_duration() { durline=$(sox "$1" -n stat 2>&1|grep "Length (seconds):");echo ${durline#*\: }; }
2011-09-02 15:22:43
User: splante
Functions: echo grep stat
2

This is an alternative to #9131. ffmpeg didn't work on my .au files, though it did on the .wav ones. Also useful if you don't have ffmpeg but do have sox. Handily, sox already reports in seconds (decimal).

date --set="$(ssh user@server date)"
2011-08-30 20:03:06
User: splante
Functions: date
Tags: ssh ,NTP ,Date
32

Shorter, easier to remember version of cmd#7636

NTP is better, but there are situations where it can't be used. In those cases, you can do this to sync the local time to a server.

eval ls -l /proc/{$(pgrep -d, COMMAND)}/cwd
2011-04-14 13:41:58
User: splante
Functions: eval ls
3

This is an alternative to another command using two xargs. If it's a command you know there's only one of, you can just use:

ls -l /proc/$(pgrep COMMAND)/cwd
psql
2011-04-08 18:35:20
User: splante
2

This is regarding the command 8263 using an alias to fill in command line options for psql.

You can actually just type 'psql'. In order for that to work, you want to set environment variables PGDATABASE, PGHOST, PGUSER, and (except you're using the default) PGPORT. Also, you can add a line "host:port:dbname:user:password" (asterisk ok in some columns) to your ~/.pgpass file. Finally, if you don't like the aligned columns, you can add the line "\pset format unaligned" to your ~/.psqlrc file.

ssh user@remote-host "DISPLAY=:0.0 import -window root -format png -"|display -format png -
2011-04-08 01:07:41
User: splante
Functions: ssh
11

Admittedly, I'd never have thought of this without the earlier examples, but here's one that you can execute from your workstation to just display the image from another, without separately doing a file transfer, etc. By the way, I hear a loud beep coming from the other room, so I guess it's not too stealthy :-D

type () { if [ "$1" = "-c" ]; then shift; for f in "$@"; do ff=$(builtin type -p "$f"); readlink -f "$ff"; done; else builtin type $typeopts "$@"; fi; }
2011-04-07 18:57:51
User: splante
Functions: readlink type
0

The "type" builtin command is handy to find out what executable will be used if you issue a command. But on some distros, particularly when using /etc/alternatives, certain executables get buried under layers and layers of symbolic links and it becomes hard to find which one.

If you put the above command in your .bashrc, it adds a "-c" option to the type command that will weed through the symbolic links and prints the actual file that will be executed.

du -h --max-depth=1 | sort -hr
2011-04-07 18:01:18
User: splante
Functions: du sort
Tags: sort du
0

Credit goes to brun65i but he posted it as a comment instead as an alternative. I hadn't noticed the -h option on sort before and this seems like the cleanest alternative. Thanks Brun65i!

cd () { cdop=""; while [ "$1" != "${1#-}" ]; do cdop="${cdop} ${1}"; shift; done; if [ $# -eq 2 ]; then newdir="${PWD/$1/$2}"; [ -d "${newdir}" ] || { echo "no ${newdir}"; return 1; }; builtin cd $cdop "${newdir}"; else builtin cd $cdop "$@"; fi }
2011-04-07 14:36:26
User: splante
Functions: cd echo return
Tags: bash cd
0

ksh's version of cd has an optional syntax where you can type "cd old new" and it will replace "old" with "new" in your current directory and take you there. This is very handy when you have a parallel directory structure, like source and object directories. As suggested, you can just type cd ${PWD/old/new} to get this in bash, but this function in your .bashrc will let you type the ksh cd syntax and avoid typing the special characters while preserving other cd functionality.