 Hello and welcome, this video is part of a series. Be sure to check out the full playlist in the link in the description of this video, as well as the end of this video. Also check out all my previous shell videos. I've done hundreds over the years on the Linux shell, Bash shell, and other shells. And in the previous video, we looked at creating a command, a function such as this for our shell. Basically, we're creating our new function that makes a directory and then if successful in creating that directory, we move into it. So I can hit enter here and now I can say mider123, any created directory called 123 and move me into it. If I move back out and I run that same command again, I'll be getting the file already exists and or the directory already exists and it does not move us into it. Today, we're gonna take that a step further. And what I'm gonna do is I'm going to, in the previous video, I told you how you can put it inside your Bash RC file to make it so that you can have this throughout multiple sessions. First off, I wanna say that you can also name this function mkdir and that way you don't have to learn a new command. But if you're gonna do that, I would recommend doing it like so just to clarify in your code if you would do it like this. So basically, this is telling it specifically to make sure you're using the command makedir and not the function that we're currently in. I think that if you leave that out, in most cases it will probably work fine, but it's good practice to put that in there. But getting on to our Bash RC file, I've created an if then statement inside my Bash RC file, so .bashrc right here, I put it at the top here just to get to it quickly and I call it mkdir. So basically, we're gonna be doing the same thing, but instead of just trying to create it and if it's successful move into it, we're gonna check first, does that directory exist? So we're saying if this directory does not exist is actually what we're checking here. So we're saying the exclamation mark says, if this is not true, check if this directory exists. So if it does not exist, we're going to make directory, make that directory and if that's successful move into it. Now it can fail for multiple reasons. As I said in the previous video, it could fail because that file, that folder file already exists or because you don't have permissions, but we're checking here, does it exist? So we're taking this up farther and we're actually checking, does it exist? If it does not create it and try to move into it, that still may fail if you don't have permissions. But here, if the file already does exist, we're gonna output a message saying that the folder already exists, which you already kind of get from the mkdir, but let's go ahead and what I'm gonna do is I'm gonna save that, I'm gonna go into my bash shell and I'm gonna say make one, two, three, which we just created, so it should tell me they cannot create the directory, the file already exists. Oh, that's the mkdir. My command was mkd, one, two, three. So you can see there's a difference. Mine gives you this output, which is from our if then statement. Now, why would you wanna do this? You're already getting that output from the mkdir file because in certain scenarios, if you can come up with something, it might be beneficial to have different checks for different problems, whether it's permissions or if the folder already exists. You may want to, which we're gonna do here in a moment, just a theoretical example, if that file exists, create a new folder with that name and some sort of integer at the end or some other character. So a very basic example here, so what I'm gonna do is I'm going to go back into my bash rc file and in here, I'm gonna say, yeah, you know, the folder already exists and then I'm gonna say creating that file underscore one instead and then what I'm gonna do is I'm going to call this function itself. So I'm gonna say mkdir $1 underscore and if all goes well, I can exit out of this. I can go into bash again because it's in my bash rc file, it doesn't automatically go into the session I'm in because the bash rc file runs when you start a new session. So I type bash here and if I mkdir 123 this time, if I typed everything properly, it says this file already exists and we are creating this instead, but didn't create that. So I typed something wrong. It did create a folder with the underscore. Let's see, then bash rc. Oh, because I didn't put the one there. There we go. So if we go back out and now if I mkdir 123, it says 123 already exists, creating 123 underscore instead. Oh, I forgot to start a new bash session. Whoops. So let me do that real quick. So that might mistake. Hopefully that didn't confuse you too much. The changes again didn't take effect because I didn't start a new session. It's my bash rc file. So mkdir 123 now I'm getting the results I wanted. Okay, so it's saying that 123 already exists and creating 123 underscore instead and in which it did and it moved me into there. Now if I was to move back out, I can run that command again, mkdir 123. And this time it's gonna tell me, oh, well, 123 already exists. We're gonna try creating 123 underscore one instead and then it says, oh, 123 underscore one already exists and we're gonna create 123 underscore one underscore instead. You could probably come up with a better script where it actually numbers it rather than putting an underscore one each time. But every time I run that command mkdir 123, it will go through the cycle of checking each one because it's calling upon itself. Again, you can make that a little bit better. I'm just doing this like a quick little basic example here for you. But I hope that I showed you a little bit something today. So let's review the three things we went over today. So we added our command to our bash rc file but not only did we add it to our bash rc file, we added to it by having an if then statement that checks where the directory exists. You can also have it check other things. Again, permissions and have it do something else with permissions or give different messages, whatever it gives you a little more flexibility than our code from last time, which was very basic and worked, but this gives you more options. Again, if you were to want to create a function called mkdir, which is the original command, that way you don't have to relearn the command. It's a good practice to put command dir in there. So if I enter now and you do want a space after the curly braces here, there we go. So now I can say make dir123 and it creates, so I don't have to learn a new command. So it's up to you whether you want to have the mkdir command work like this or if you want to create your own command that way they works independently of each other. And again, the command here is just specifying that we're using the mkdir command and not this function so it's not calling itself. So again, if you left that out, I think it will still work, but it's good practice to have that there. So that's it. We learned three things, putting into the bashrc file or if then statement and also the command command to make sure that we have the original command rather than the function we're calling. I know I messed up once or twice in the middle there. Let's just go back here and just as an example, create another file, I'll call it folder. I'll call it folder. And if I move back out and try to run that again. Oh, right, mkdir folder. There we go. So instead of creating folder which already existed, it created folder underscore one. Again, up to you in your scenario, I think in most cases I would prefer if it already exists to move into that folder, but that could cause problems if you do that because maybe you thought you just moved into a new directory but you moved into a directory that already exists and all of a sudden you start messing with files that are in there without realizing you start overriding stuff. You might like try to cat or echo something into a file that you don't think exists and then overriding it. Again, all your preferences is all about flexibility and doing what you need to accomplish. Anyway, I do thank you for watching. I hope you enjoyed these tutorials. Be sure to check out the link in the description to my website, filmsbychrist.com. That's Chris the K there. You can search through all my videos and I've got plenty of them. You can also go to the support section on my page and support me through either PayPal or patreon.com forward slash milix 1000. I do thank you for watching. If you do like these, be sure to like, share, subscribe and comment. I hope that you have a great day.