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.
I believe you are looking for this:
mkdir project1 && cd "$_"
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 function
keyword, and have the first line be mkdcd() {
$1
should be "$1"
. Secondly -- I think that counts as "making a new command". –
Tipsy chdir
syscall (cd
bash builtin) should run in the interactive shell (not in a child process of that shell) –
Dellora 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/obsolete –
Siphon For oh-my-zsh users: take 'directory_name'
Reference: Official oh-my-zsh github wiki
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
"$*"
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 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.
Maybe I'm not fully understanding the question, but
>mkdir temp ; cd temp
makes the temp directory and then changes into that directory.
mkdir temp ; cd temp ; mv ../temp ../myname
You can alias like this:
alias mkcd 'mkdir temp ; cd temp ; mv ../temp ../'
You did not say if you want to name the directory yourself.
cd `mktemp -d`
Will create a temp directory and change into it.
Maybe you can use some shell script.
First line in shell script will create the directory and second line will change to created directory.
© 2022 - 2024 — McMap. All rights reserved.