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 Lets say that command is xyz, then Code:
$pwd Regards |
A simple way is to link the two commands:
Code:
mkdir Subjects/Maths && cd Subjects/Maths |
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.
|
Also, if you want to create a directory with some subdirectories in one go you could use:
Code:
mkdir -p dir/sub1/sub2 Code:
mkdir -p main_dir/{subA/{subA1,subA2},subB/{subB1,subB2}} |
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 $# 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 Code:
$ . mkdirNcd.sh For that, I just had to put it into a folder defined in $PATH Code:
$ echo $PATH Now I just had to write Code:
$ . mkdirNcd.sh newFolder Making an Alias I added following line to my .bashrc file Code:
alias thomas='. mkdirNcd.sh' Code:
$thomas Chemistry |
All times are GMT -5. The time now is 02:22 AM. |