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 using cd from sorted by
Terminal - Commands using cd - 179 results
cd () { command cd $1 && ls ; }
function cdf () { [ -f $1 ] && { cd $(dirname $1); } || { cd $1 ; }; pwd; };
2012-09-08 10:50:58
User: Josso
Functions: cd dirname
Tags: bash ksh
0

Had trouble with the other function, because of missing semicolons. (According to my bash on OS X)

alias md='mkdir -p'; alias rd='rmdir'; mcd () { mkdir "$@" && cd "$_"; }
2012-08-12 12:54:51
User: expelledboy
Functions: alias cd mcd mkdir
0

I realise that this is just a reiteration of another entry (regardless of whether I came up with all this all by myself), but I would like present my additional alias' in context as a method of managing your directories. Rather convenient.

function _xterm_cwds() { for pid in $(pidof -- -zsh) $(pidof zsh); do reply+=$(readlink /proc/$pid/cwd) done }; function xcd() { cd $1 }; compctl -K _xterm_cwds xcd
2012-07-12 19:59:46
User: aartur
Functions: cd pidof readlink
0

It gives a 'xcd' command for changing directory to one of CWDs of other ZSH processes (typically running in a terminal emulator). Useful for single-windowed terminal emulators like XTerm or Rxvt which don't have ability to pass CWD of one shell to another.

cd -
ccd () { cd *$1*; }
2012-07-01 10:46:06
User: saturation
Functions: cd
0

If you have long and complicated folder names this might ease your work.

add this into .bashrc

cd -
2012-06-26 17:20:54
User: ankush108
Functions: cd
Tags: cd
1

switch to previous directory or toggle

function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
2012-06-26 17:19:16
User: ankush108
Functions: cd eval mkdir
Tags: cd mkdir
0

Creates a directory and then cds into it directly

up() { [ $(( $1 + 0 )) -gt 0 ] && cd $(eval "printf '../'%.0s {1..$1}"); }
2012-06-15 17:10:45
User: Mozai
Functions: cd eval
Tags: bash cd
6

`up 3` will climb the directory tree by three steps. `up asdf` will do nothing, and returns exit code 1 as an error should.

set str=user_pref("browser.startup.homepage", "http://www.fcisolutions.com/"); cd = "%APPDATA%\Mozilla\Firefox\Profiles\*.default\" echo %str%>>prefs.js
2012-05-30 18:50:15
User: Micool777
Functions: cd echo set
1

Pros: Works in all Windows computers, most updated and compatible command.

Cons: 3 liner

Replace fcisolutions.com with your site name.

cd /System/Library/LaunchDaemons; sudo launchctl load -w com.apple.metadata.mds.plist
up() { local x='';for i in $(seq ${1:-1});do x="$x../"; done;cd $x; }
2012-05-16 04:21:41
User: evil
Functions: cd seq
Tags: cd
8

I wrote this a long time ago, wondering why this wasn't floating around somewhere out there (at least not where I could find).. this seems much more simple than multiple aliases and can cd out of directories easier.

cd "~/Music/iTunes/iTunes Media/Mobile Applications";ls > filepath
up () { if [ "${1/[^0-9]/}" == "$1" ]; then p=./; for i in $(seq 1 $1); do p=${p}../; done; cd $p; else echo 'usage: up N'; fi }
2012-04-19 08:16:34
Functions: cd echo seq
Tags: alias cd
2

Change n directories up, without parameters change one up

cd $OLDPWD
2012-04-16 21:18:27
User: khopesh
Functions: cd
Tags: cd
-2

This is like `cd -` but doesn't echo the new directory name, which is preferable (to me) for an alias, e.g.

alias cdo="cd $OLDPWD"
cd / && touch ./\-i
2012-04-05 20:55:37
User: joedhon
Functions: cd touch
-10

Somehow, i prefer forcing to rm interactively to accidently rm'ing everything...

diff --suppress-common-lines -y <(cd path_to_dir1; find .|sort) <(cd path_to_dir2; find .|sort)
2012-02-13 12:49:33
User: knoppix5
Functions: cd diff find
2

Output of this command is the difference of recursive file lists in two directories (very quick!).

To view differences in content of files too, use the command submitted by mariusbutuc (very slow!):

diff -rq path_to_dir1 path_to_dir2
cd /path/to/dir && command_or_script; cd -;
tmpfs(){ cd /;for i in $@;do tar czvf /tmp/$i $i;mount -t tmpfs tmpfs /$i;tar xvzf /tmp/$i;cd ~ ;}# usage: tmpfs etc var
_ff(){ cd /mnt;echo /mnt/*/* |sed 's/ \/mnt\//\&/g' |sed '/'"$1"'/!d'; cd -;}
2011-12-30 23:25:31
User: argv
Functions: cd echo sed
-5

_ff(){

cd /mnt;

echo /mnt/*/* |sed '

s/ \/mnt\//\&/g;

'|sed '/'"$1"'/!d';

cd -;

}

ff(){

case $# in

0)

echo "usage: ff glob [sed-cmds] [--|var-name]"

;;

1)

_ff $1 |sed =

;;

[2-9])

case $2 in

--) _ff $1 |less -SN

;;

*) _ff $1 |sed -n ''"$2"''|tr '\n' '\040' |sed 's/.*/export '"$3"'=\"&/;s/=\" /=\"/;s/ $/\"/' > $HOME/.ff;

case $# in

3)

. $HOME/.ff

;;

esac;

sed '

s/export .*=\"/\$'"$3"' = \"/;' $HOME/.ff;\

;;

esac

;;

esac;

}

v(){

local a=$HOME;

sed '

s/export /less -n \$/;

s/=.*//;

' $a/.ff > $a/.v ;

. $a/.v ;

}

Another approach using ls(1)

lsl(){

_lsl ()

{

ls -l $3 /mnt/*/$1* 2>/dev/null;

};

case $# in

0)

echo "usage: lsl pat [ls-options|result-no]";

echo "usage: lsle pat [sed-cmds]"

;;

1)

_lsl $1 |sed =

;;

2)

case $2 in

-*) _lsl $1 $@;;

*)

_lsl $1 |sed 's/.* //;

'"$2"'!d;

'"$2"'q' > $HOME/.lsl ;

export v=$(sed 1q $HOME/.lsl);

echo \$v = $v

;;

esac

;;

esac;

}

exp(){

echo "%s/\$/ /";

echo "%j";

echo "s/^/export v=\"";

echo "s/\$/\"";

echo "s/ \"\$/\"";

echo ".";

echo "wq";

}

lsle(){

lsl $1 -1 |sed $2 > .lsl&&

exp |ed -s .lsl >&-&&

. .lsl&&

echo \$v = $v;

}

STARTING_DIR=$(cd $(dirname $0) && pwd)
2011-11-30 17:35:15
User: bbbco
Functions: cd dirname
0

Sometimes you need the full path to your script, regardless of how it was executed (which starting directory) in order to maintain other relative paths in the script.

If you attempt to just use something simple like:

STARTING_DIR="${0%/*}"

you will only get the relative path depending on where you first executed the script from.

You can get the relative path to the script (from your starting point) by using dirname, but you actually have to change directories and print the working directory to get the absolute full path.

cd ~/ruby/project_a ; find . -name "*profile*" -exec pax -rw {} ~/ruby/project_b/ \;
2011-11-10 21:18:15
User: cbpowell
Functions: cd find pax
0

Let's say you have a set of files in tree A that you want duplicated to tree B while preserving their directory structure / hierarchy. (For example, you might want to copy your 'profile' model/views/controller from one Rails application to another.) The "pax" command will copy all matching files to the destination while creating any necessary directories.

#!/bin/bash cd /usr/share/consolefonts/; for i in * ; do setfont; echo "testing >> $i << font" ; setfont $i ; showconsolefont ; sleep 5 ; clear ; done
export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'
2011-10-18 19:58:39
User: mpb
Functions: cd export
1

I was surprised to find that with RedHat bash, I could not find any comment lines (begining with #) in my bash shell history. Surprised because in Mageia Linux this works. It turns out that RedHat's bash will keep comment lines if in my .bashrc, I define:

export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'

Why have comment lines in shell history? It's a handy and convenient way to make proto-commands (to be completed later) and for storing brief text data that is searchable in shell history.

upto() { cd "${PWD/\/$@\/*//$@}" }