I have moved my blog to Wordpress at theunixgeek.wordpress.com. I will still be checking back periodically on this one as well, though. 19 April 2009

featured

Merging Mkdir and Cd | 280 Slides Interview | I Switched to KDE 4

clickable portals

Saturday, February 21, 2009

Merging mkdir and cd

I oftentimes use mkdir and cd together, as in mkdir project && cd project or mkdir project; cd project and I believe many people probably do the same thing. I always found it tedious and repetitive to have to type in the directory's name twice, so I thought, "why not merge these two into one command?" Something like mkcd project could do both jobs at once and reduce typing; it's like hitting two birds with one stone, so to speak.

The biggest problem I've encountered in the implementation of a seemingly simple idea is that when programs or shell scripts are run they are child processes. That said, you can't have a simple BASH script that reads "mkdir $1 && cd $1" because it would only switch to the directory while running as a child process instead of actually switching to the directory. The same applies to the system( ); function in stdlib.h (in C, C++ - cstdlib -, and related languages).

I am considering submitting the idea to the GNU project. The idea has been sent and I'm awaiting their reply.

6 comments:

Tomy said...

function mkcd()
{
mkdir $1 && eval cd $1
}

Simon said...

mkdir $A&& wait && cd $A

The command in the middle "wait" is a shell built-in (i think) that waits for all file io to finish (again i think)

Should be easy enough to alias this to something, or put a cmd in /usr/local/bin

Craig said...

Make a shell script that does the mkdir $1 && cd $1, then make a bash alias like this:

alias mkcd='. /path/to/mkcd'

The . makes it source the file in the current shell rather than forking, and thus works. You can then expand your mkcd script to include options such as -p to make it more useful, which you couldn't do with just a plain alias.

sagotsky said...

I've been using this for a couple months:

function mkcd() {
mkdir -p "$@"
cd "$@"
}

smallawei said...

震動環,潤滑液,情趣禮物,情趣玩具,威而柔,精油,逼真按摩棒,數位按摩棒,
情人節禮物,情人趣味用品珠,情人趣味用品
情人趣味用品愛蜜莉,情人趣味用品液
情人趣味用品轉,情人趣味用品娃,

G點,按摩棒,轉珠按摩棒,變頻跳蛋,跳蛋,無線跳蛋,

飛機杯,男用強精長軟質套,男用強精短軟質套,充氣娃娃,自慰套,自慰套,情趣娃娃,自慰器,電動自慰器,充氣娃娃,
情趣內褲,丁字褲,SM,情趣,情趣用品,情趣味用品,,
情趣用品,情趣,情趣用品爽翻天,情趣爽翻天情人趣味艾蜜莉,情人趣味艾蜜莉,情色貼圖,情趣商品,情趣用品店,

Avantime said...

Try to put this in your ~/.bash_profile: function mcd() { [ -n "$1" ] && mkdir -p "$1" && cd "$1"; }