 Hello, and welcome to video four in this series of 2018 shell script tutorials. I hope you've checked out my previous videos in this series as well as videos I've done over the years because I've done hundreds on shell scripts. Today. We're gonna be looking at the make directory command MK DIR, but we're gonna combine that with the CD command. So for example, I am in an empty directory right now You can see nothing in there I can say make directory my dir and I can move into that by typing CD my dir and you can see it's empty But moving back out of that I can remove that folder make dir and Now I'm in an empty directory again Lots of the time when you make a directory the next thing you probably are gonna do is move into it with CD So if you do do that all the time you might want to combine the commands. Okay, so The first thing you want to do is come up with a name for your command and there's different ways of doing this I'm gonna show you a very simple way today But in the next video, I'll show you a more complicated way that does a little bit of a error checking if you want if you You know if you want to call it that anyway Let's create a command now. I was thinking originally MCD for make change directory problem is That is actually a command on my machine already MCD so man MCD and you can see it may not be installed on your system It's part of the mTools Which you use to work with Microsoft DOS disks, which I have been working doing stuff with fat 32 partitions Which I'm gonna be tutorials on in the near future and that's why I have this So you may not have that command installed But it is name of a command so you may not want to use that because in the future You might install that and you go to use it and you've already overwritten it Although it will still exist. Anyway You come up with whatever name you want and next week we're gonna look at replacing the MD MK dir command, but today we're just gonna create a new command and I'll just call it my CD Okay, so I'm gonna call it my CD and I'm gonna give parentheses So we what does it mean when you have a parentheses like that in a command and pretty much any program language? It's a function. We are creating a function here So we're going to say that and we're gonna give it our little squiggly braces there And what we're gonna do is we're gonna give it two commands make dir MK dir dollar sign one and they're gonna say ampersand ampersand CD dollar sign one and we do a semi-colon which I don't anything needs to be there by like to have it there to let you Know that that's the end. So basically what we're doing here is we're creating a function called my CD and When you run that function, it's gonna run this command and this command So it's gonna run make dir and and CD and then the ampersand ampersand says only move into that directory If this command is successful, which we'll talk about in a moment and then dollar sign one in both of these is the first string the first Variable sent to the command. So we'll look into that more in a second But let's just go ahead hit enter and now I can type in my CD and I can type in test one and when I enter it created That directory and moved me into it in one simple command and I can move back out and I can say MCD test to and I moved into that directory and if I rest run again now I'm in another directory inside that directory. So it's saving me from typing out a separate command now If I was to back out of this and I was to run this command again It's going to give me an error, but still move me into that directory So if I had entered here, it's gonna say that that directory already exists the file already exists because it already did Make CD and it did not move me into it now depending on your scenario. You may or may not want you to move into it So you're gonna create a directory and move into it. It will give you this error unless you pipe it to dev No, uh, if we were to rewrite our function instead of ampersand ampersand We just do semicolon which saying is run this command and then run this command as opposed to run this command And if successful then run this command So now if I was to run this and I was typing my CD to it's gonna tell me that file that folder already exists But it's still gonna move me into it. You may or may not want not want that for your thing now There might be more reasons than one This reason it could not successfully create the directory was because it existed maybe You can't create the directory because you don't have permissions to create the directory inside that directory In which case you'd get two fails. You get a fail that You can't create it and then you get a fail that you couldn't move into it You decide what works best for you I think If the first one fails, it's a good idea to Stop there But again Up to you now if I was to open up a new shell session So we're down here. This is a new shell session down here Like open up a new window or closing that one open up and I was typing my cd You're gonna see it says that it doesn't exist It's gonna my shell because the way I have z shell set up It's going to suggest a change to me because that command does not exist It creates exist in this session up here my cd. Okay, so The way you make that permanent You know through all your sessions is it adds to your c file. So if you're using bash, it's dot bash rc If you're using z shell, it's dot z shell rc in your home directory I'm not going to get into that You probably already know how to do that if not google it And I'm also pretty sure that I've done videos on that in the past Another thing I want to show you is again this dollar sign is saying use the first variable pass to us So if we were to again my cd and type in test for it created that directory move into it, but if I was to type in My cd. This is a folder. It's not going to create a folder called. This is a folder It's going to create a folder called this and ignore the rest of it So you can see I'm in a folder called this if you want it to Create that entire folder. You're going to want to put quotations around that And so there we create a folder called. This is a folder and moved into it You could change your function to basically use all strings passed to it That's one option if you want to do that Well, I think that's necessarily a great idea. Plus you might want to add other options to that So that is a quick and simple way to make a directory and move into it in one command So now I can mcd blah blah blah and I've created it and moved into it I do thank you for watching as always. I hope that you enjoyed this tutorial I hope you enjoyed all my tutorials. Please visit my website filmsbychrist.com That's chris the case. There's a link in the description there You can search through all my videos also support me in the support section at patreon.com for session Alex 1000 or through PayPal So check that out on my website as well. And if you do like my videos and can't support financially I do appreciate a like think about subscribing and sharing definitely helps Again, I thank you for watching and I hope that you have a great day