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 tagged cd from sorted by
Terminal - Commands tagged cd - 49 results
upto() { cd "${PWD/\/$@\/*//$@}" }
jd() { cd **/"$@"; }
2011-10-05 11:47:57
User: sharfah
Functions: cd
-3

Usage: jd dir

Requires globstar. To set globstar use:

shopt -s globstar
mkdir /home/foo/doc/bar && cd $_
2011-08-12 11:29:19
User: kzh
Functions: cd mkdir
41

The biggest advantage of this over the functions is that it is portable.

map() { if [ "$1" != "" ]; then alias $1="cd `pwd`"; fi }
2011-07-11 15:46:19
User: javidjamae
Functions: alias
1

Put the function in your .bashrc and use "map [alias]" to create the alias you want. Just be careful to not override an existing alias.

cd() { if [ -n "$1" ]; then [ -f "$1" ] && set -- "${1%/*}"; else [ -n "$CDDIR" ] && set -- "$CDDIR"; fi; command cd "$@"; }
2011-06-24 08:48:13
User: flatcap
Functions: cd command set
Tags: cd test set
0

Move efficiently between directories.

.

This command adds a couple of extra features to cd, without affecting normal use.

CDPATH use is also unaffected. It introduces and environment variable CDDIR which is used as an alternate home directory.

.

Note: I don't want to alter $HOME because then all my dot files will move.

.

Examples:

.

cd dir

Change directory to "dir" (using CDPATH if necessary)

.

cd dir/file.txt

Change directory to "dir" (containing folder of "file.txt")

This allows you to cut'n'paste, or use

.

CDDIR is unset

cd

Change directory to $HOME

.

CDDIR=/home/flatcap/work

cd

Change directory to /home/flatcap/work

.

For convenience, put the command, and the following, in your .bashrc or .bash_profile

export CDDIR="/home/flatcap/work"

alias cdd="CDDIR=$(pwd)"

mydir=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)
2011-04-27 16:33:38
User: xeor
Functions: cd dirname
Tags: cd script pwd
0

I submitted a command like this without $0 if $BASH_SOURCE is unset. Therefor, it did only work when using ./script, not using 'sh script'. This version handles both, and will set $mydir in a script to the current working directory. It also works on linux, osx and probably bsd.

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.

take() { mkdir -p $1 && cd $1; }
2011-04-06 15:22:13
Functions: cd mkdir
Tags: cd mkdir
1

This creates a bash function `take` that you can call with the name of the directory as the first parameter. Add the function to ~/.bashrc to have it available anytime.

mydir(){mkdir -p $1 && cd $1}
2011-04-06 15:03:33
User: m0jumb0
Functions: cd
Tags: cd mkdir
0

Make it a reusable function and add the -p flag to mkdir to create directories recursively

usage: mydir some/dir/to/create

Dir=dirname; mkdir $Dir && cd $Dir
2011-04-06 14:53:57
User: saibbot
Functions: cd mkdir
Tags: cd mkdir
-7

Create a directory called "dirname" and navigate into it.

( cd $DIR; command; )
2011-03-29 13:16:00
User: sanmiguel
Functions: cd
Tags: bash cd
3

Obviously the example given is necessarily simple, but this command not only saves time on the command line (saves you using "cd -" or, worse, having to type a fully qualified path if your command cd's more than once), but is vital in scripts, where I've found the behaviour of "cd -" to be a little broken at times.

cd ${PWD/a/b}
2011-03-03 06:27:12
User: greggster
Functions: cd
Tags: bash cd
7

This is useful for quickly jumping around branches in a file system, or operating on a parellel file.

This is tested in bash. cd to (substitute in PWD, a for b) where PWD is the bash environmental variable for the "working directory"

shopt -s autocd
2010-12-12 20:53:33
User: xeor
Tags: bash cd bashrc
1

Makes bash-4.x like zsh. Automatic cd into a directory if a command with that name doesnt exists. Ready for your ~/.bashrc file

cd() { if [ -z "$1" ]; then command cd; else if [ -f "$1" ]; then command cd $(dirname "$1"); else command cd "$1"; fi; fi; }
2010-04-23 19:17:43
User: xeor
Functions: cd command dirname
Tags: cd command
9

This little function will smarten 'cd'. If you try to cd into a file (which I guess we all have done), it cd's into the directory of that file instead.

I had to use nesten if's, to get cd to still work with 'cd' (to get to $HOME), 'cd -' (to get to last directory), and 'cd foo\ bar'.

echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
2010-01-13 09:37:56
User: phaidros
Functions: cd echo mkdir
-5

combines mkdir and cd

added quotes around $_, thanx to flatcap!

function ..(){ for ((j=${1:-1},i=0;i<j;i++));do builtin cd ..;done;}
2010-01-02 08:36:12
User: bhepple
Functions: cd
Tags: cd
3

Instead of typing "cd ../../.." you can type ".. 3". For extremely lazy typists, you can add this alias:

alias ...=".. 2" ....=".. 3"

- so now you can write just .... !!!

NB the .. function needs to be "source"d or included in your startup scripts, perhaps .bashrc.

alias burnaudiocd='mkdir ./temp && for i in *.[Mm][Pp]3;do mpg123 -w "./temp/${i%%.*}.wav" "$i";done;cdrecord -pad ./temp/* && rm -r ./temp'
2009-11-21 19:57:18
User: eightmillion
Functions: alias mpg123 rm
3

This uses mpg123 to convert the files to wav before burning, but you can use mplayer or mencoder or ffmpeg or lame with the --decode option, or whatever you like.

alias ..="cd .." ...="cd ../.." ....="cd ../../.."
alias ..="cd .."; alias ...="cd ../.."; alias ....="cd ../../.."
2009-10-30 01:04:33
User: Tzunamii
Functions: alias
Tags: cd
1

Change to your taste. Much quicker than having to add 'cd' every time. Add it to your .bashrc or .bash_profile.

cd() { if [[ "$1" =~ ^\.\.+$ ]];then local a dir;a=${#1};while [ $a -ne 1 ];do dir=${dir}"../";((a--));done;builtin cd $dir;else builtin cd "$@";fi ;}
2009-10-29 21:43:51
User: eightmillion
Functions: cd
Tags: cd
3

This is a kind of wrapper around the shell builtin cd that allows a person to quickly go up several directories.

Instead of typing:

cd ../..

A user can type:

cd ...

Instead of:

cd ../../..

Type:

cd ....

Add another period and it goes up four levels. Adding more periods will take you up more levels.

script_path=$(cd $(dirname $0);pwd)
2009-10-14 16:04:03
User: jgc
Functions: cd dirname
Tags: cd pwd PATH
7

Another way of doing it that's a bit clearer. I'm a fan of readable code.

md () { mkdir -p "$@" && cd "$@"; }
2009-09-24 16:09:19
User: drewk
Functions: cd mkdir
27

How often do you make a directory (or series of directories) and then change into it to do whatever? 99% of the time that is what I do.

This BASH function 'md' will make the directory path then immediately change to the new directory. By using the 'mkdir -p' switch, the intermediate directories are created as well if they do not exist.

cd
alias ..='cd ..'
2009-03-20 09:57:28
User: eimantas
Functions: alias
Tags: bash unix shell cd
8

Alias two dots to move to parent directory. Put it into your .bashrc or .profile file.