LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is there a way to make a new directory and then cd into with single command ? (https://www.linuxquestions.org/questions/linux-newbie-8/is-there-a-way-to-make-a-new-directory-and-then-cd-into-with-single-command-752202/)

mynameisthomas 09-02-2009 03:37 PM

Is there a way to make a new directory and then cd into with single command ?
 
Hi,

Is there a way or command that makes a new directory and then also cd into it?

Like what I usually do is
Code:

$pwd
/home/Thomas/
$ls Subjects
English Chemistry
$mkdir Subjects/Maths
$cd Subjects/Maths
$pwd
/home/Thomas/Subjects/Maths

Now what I wish is using a single command that will, on pressing return, not only will create the directory but also cd into it.

Lets say that command is xyz, then

Code:

$pwd
/home/Thomas/
$ls Subjects
English Chemistry
$xyz Subjects/Maths
$pwd
/home/Thomas/Subjects/Maths

So any ideas fellows? I will really appreciate any sort of answer or tip.

Regards

the trooper 09-02-2009 03:48 PM

A simple way is to link the two commands:

Code:

mkdir Subjects/Maths && cd Subjects/Maths

Poetics 09-02-2009 03:50 PM

And/or you can call a very basic script to do the same, so you could type "mcd" or whatever you wanted and have it do 'the work' for you.

sycamorex 09-02-2009 03:56 PM

Also, if you want to create a directory with some subdirectories in one go you could use:

Code:

mkdir -p dir/sub1/sub2
or with the whole tree of directories:

Code:

mkdir -p main_dir/{subA/{subA1,subA2},subB/{subB1,subB2}}

mynameisthomas 09-13-2009 01:29 PM

Well after reading replies to my question, I tried my luck with bash scripting. I got a script that has solved my problem.

Thanks to sycamorex, Poetics and the trooper for helping me.

I am posting my script and a little bit explanation here for some other newbie like me :

I named the following script mkdirNcd.sh

Code:

#!/bin/sh

if [ $# = 0 ]
then
        echo No Folder name given

elif  [ -d $1 ]       
then
                echo Folder already exists
                cd $1
else
                echo Creating a new folder
                mkdir  -p $1
                cd $1       
fi

#!/bin/sh This line is simply a comment

$# This is a variable which has the number of parameters passed to the script.

if [ $# = 0 ]
then
echo No Folder name given

The above portion just checks whether any folder name was given or not. In case it wasn't it prints "No Folder name given"

$1 This variable contains first parameter with which we are concerned.

elif is same as else if

elif [ -d $1 ]
then
echo Folder already exists
cd $1

The above portion checks whether the given folder exists or not. If it exists it cd into it.

else
echo Creating a new folder
mkdir -p $1
cd $1

The above portion creates a new folder and cd into it.

fi This tells the end of if structure

A problem

There is a problem with using cd in a script. Actually all scripts on execution make their own sub-shell. Once the script ends, control exits out of that sub-shell and we are back at the original shell.

Now when we use cd in a script. That script creates its own sub-shell. In that sub-shell control cd into the new folder. But once script ends, we are back to our original shell. Hence there is no cd into new folder.

To solve it we can use
Code:

$ source mkdirNcd.sh
or
Code:

$ . mkdirNcd.sh
Making it accessible globally

For that, I just had to put it into a folder defined in $PATH

Code:

$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/Thomas/bin

I selected the last folder /home/Thomas/bin; created bin in my home folder and copied mkdirNcd.sh into it.

Now I just had to write
Code:

$ . mkdirNcd.sh newFolder
to get the desired effect. But it feels like a burden to write the whole thing with a dot preceding it, so I made an alias of it.

Making an Alias

I added following line to my .bashrc file
Code:

alias thomas='. mkdirNcd.sh'
Now I just have to write
Code:

$thomas Chemistry
Creating a new folder
$pwd
/home/Thomas/Chemistry



All times are GMT -5. The time now is 02:22 AM.