LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-02-2009, 03:37 PM   #1
mynameisthomas
LQ Newbie
 
Registered: Feb 2008
Distribution: Fedroa 10
Posts: 19

Rep: Reputation: 0
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
 
Old 09-02-2009, 03:48 PM   #2
the trooper
Senior Member
 
Registered: Jun 2006
Location: England
Distribution: Debian Bullseye
Posts: 1,508

Rep: Reputation: Disabled
A simple way is to link the two commands:

Code:
mkdir Subjects/Maths && cd Subjects/Maths
 
Old 09-02-2009, 03:50 PM   #3
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
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.
 
Old 09-02-2009, 03:56 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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}}
 
Old 09-13-2009, 01:29 PM   #5
mynameisthomas
LQ Newbie
 
Registered: Feb 2008
Distribution: Fedroa 10
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
  


Reply

Tags
cd, mkdir



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to make a directory with mkdir using the date command. Altheastronut Linux - General 5 06-09-2009 12:14 PM
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 04:16 AM
Copying a single file into each directory of a directory tree mlapl1 Linux - Newbie 2 06-27-2007 10:18 PM
copy all of one filetype found by locate command to a single directory socratesone Linux - Software 3 06-22-2005 01:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration