One command to create and change directory
Asked Answered
N

9

42

I'm searching for just one command — nothing with && or | — that creates a directory and then immediately changes your current directory to the newly-created directory. (This is a question someone got for his exams of "linux-usage", he made a new command that did that, but that didn't give him the points.) This is on a debian server if that matters.

Nightingale answered 3/1, 2013 at 10:5 Comment(4)
I'm pretty sure the exam question was for you to understand why bash functions are necessary.... Making a script won't work (and is a bad answer).Dellora
This isn't a good question for stackoverflow, either like @BasileStarynkevitch said he didn't get the marks because he used a script and not function or he made some other syntactical mistake or it was a mistake with the marking/question itself. The only way you'll find out is to speak to the exams markers/setters.Onomasiology
Build a script for these two command.Skiff
@user1929959: It cannot be a script. See my answer (it should be a function)Dellora
N
64

I believe you are looking for this:

mkdir project1 && cd "$_"
Nf answered 3/11, 2014 at 19:36 Comment(2)
Great answer, IMHO. I looked it up and $_ is a "Special variable set to final argument of previous command executed", source: tldp.org/LDP/abs/html/internalvariables.htmlHalflight
@BartvanKuik, ...that said, the ABS isn't a particularly good reference to point people to -- it's the W3Schools of bash, frequently using bad practices in its examples and rarely refreshed for outdated content. Consider instead wiki.bash-hackers.org/syntax/shellvars, or the official manual at gnu.org/software/bash/manual/html_node/Special-Parameters.html, or the BashGuide at mywiki.wooledge.org/BashGuide/…Siphon
D
33

define a bash function for that purpose in your $HOME/.bashrc e.g.

 function mkdcd () {
     mkdir "$1" && cd "$1"
 }

then type mkdcd foodir in your interactive shell

So stricto sensu, what you want to achieve is impossible without a shell function containing some && (or at least a ; ) ... In other words, the purpose of the exercise was to make you understand why functions (or aliases) are useful in a shell....

PS it should be a function, not a script (if it was a script, the cd would affect only the [sub-] shell running the script, not the interactive parent shell); it is impossible to make a single command or executable (not a shell function) which would change the directory of the invoking interactive parent shell (because each process has its own current directory, and you can only change the current directory of your own process, not of the invoking shell process).

PPS. In Posix shells you should remove the functionkeyword, and have the first line be mkdcd() {

Dellora answered 3/1, 2013 at 10:8 Comment(5)
Firstly -- $1 should be "$1". Secondly -- I think that counts as "making a new command".Tipsy
no, this is not what i'm looking for (as ruakh said) that's making a new command/function/script or something like thatNightingale
It cannot be done like you dream. The chdir syscall (cd bash builtin) should run in the interactive shell (not in a child process of that shell)Dellora
@BasileStarynkevitch: The answer that you posted does not meet the OP's requirements. If your real answer is "it is impossible to meet these requirements", then you need to include that in the answer you post.Tipsy
BTW, if you are going to keep the function keyword, you should remove the () -- that way your code is compatible with ksh (compatibility with code written with ksh extensions is why bash has a function keyword at all). Right now, it's compatible with neither. See also wiki.bash-hackers.org/scripting/obsoleteSiphon
H
19

For oh-my-zsh users: take 'directory_name'
Reference: Official oh-my-zsh github wiki

Hoskinson answered 12/7, 2015 at 15:38 Comment(1)
Wow, thanks for this super trick! I use zsh for years but never heard of thatMetapsychology
A
10

Putting the following into your .bash_profile (or equivalent) will give you a mkcd command that'll do what you need:

# mkdir, cd into it
mkcd () {
    mkdir -p "$*"
    cd "$*"
}

This article explains it in more detail

Acord answered 24/10, 2014 at 10:13 Comment(2)
"$*" is generally the Wrong Thing -- it takes all your arguments together and throws them together with the first character of $IFS as a separator. That means that while mkdir "Directory One" "Directory Two" will create two directories, mkcd "Directory One" "Directory Two" will try to create a single directory named Directory One Directory Two.Siphon
Try "$@" instead of "$*", I've heard it works wonders to quote each argument separately, and it will silently omit "" if you have no arguments...Ney
A
6

I don't think this is possible but to all people wondering what is the easiest way to do that (that I know of) which doesn't require you to create your own script is:

mkdir /myNewDir/
cd !$

This way you don't need to write the name of the new directory twice.

!$ retrieves the last ($) argument of the last command (!).

(There are more useful shortcuts like that, like !!, !* or !startOfACommandInHistory. Search on the net for more information)

Sadly mkdir /myNewDir/ && cd !$ doesn't work: it retrieves the last of argument of the previous command, not the last one of the mkdir command.

Ayo answered 9/9, 2013 at 10:59 Comment(1)
"mkdir coffeescript && cd !$" worked for me, thanks!Nf
S
2

Maybe I'm not fully understanding the question, but

>mkdir temp ; cd temp

makes the temp directory and then changes into that directory.

Skipper answered 8/12, 2014 at 20:38 Comment(0)
D
2
mkdir temp ; cd temp ; mv ../temp ../myname

You can alias like this:

alias mkcd 'mkdir temp ; cd temp ; mv ../temp ../'
Doubles answered 26/11, 2018 at 8:22 Comment(0)
C
2

You did not say if you want to name the directory yourself.

cd `mktemp -d`

Will create a temp directory and change into it.

Chihli answered 27/8, 2019 at 15:7 Comment(1)
Is there a way to rename the current directory? Prolly.. Cause if so, we could maybe chain it. :DAyo
O
-3

Maybe you can use some shell script.

First line in shell script will create the directory and second line will change to created directory.

Opheliaophelie answered 3/1, 2013 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.