 Okay, this shell script tutorial is going to be showing you how to check if a directory exists And we're going to be going over what we did in the last few tutorials checking error codes or I'm sorry exit codes and Continuing whether something happens or not. So basic way to check if a directory exists is to Give our brackets here, which is kind of like doing a basic if then statement. I can say Dash D. This is checking does this directory exist and then in quotations I'll say temp which I do have a temp directory. I'll hit enter we get no output But there was an exit code once again every program every command that runs has an exit code whether it exited successfully or Unsuccessfully so in this particular case if the temp folder exists It would have exited successfully which should which should give us a zero if it does not exist We should get a one so if I echo Dollar sign question mark it should give us the exit code of the last command ran So I'll hit enter and you can see that we got the output of zero if I was to Hit up arrow twice and then put in something like this which I know that directory does not exist and we say echo Dollar dollar sign question mark we should get a one this time because it does not exist now You can always store that value and check it later on in your code if you're writing out an actual script But just as we learned last time we can continue a certain way Whether it exists or not. So let's go up hitting up arrow a few times going back in our history here So we're going to check once again to see if the temp directory exists once again We have our our brackets here. Remember there are spaces here and here without those don't that this won't work properly Dash D and another space. So that's checking does this directory exist and then whatever directory you put inside these quotations and we're going to say is ampersand ampersand the n symbol and n symbol and What that's going to do is well this next command I type will only run if that last command was true So I'm going to say echo the folder Exists Now if I hit enter you can see we get the folder exists Because the folder does exist if I hit up arrow to go back to it again And I do forward slash and a bunch of gibberish again. I know that folder does not exist We get no output. So meaning that the folder does not exist. So we can Run the same command again and do pipe pipe, which is basically like saying well then do this If that's not true, then do this. We're going to say echo folder does not exist and If I hit enter now we get the output of folder does not exist now Just look at this again. We're checking. Does this folder exist does this folder exist forward slash temp forward slash that blah blah blah and If that is true, so this is checking the error code of this if it's zero basically then we're going to do this Pipe pipe means otherwise do this. So it's going to either do this or it's going to do this whether this is true or not and That's the basics of it checking out whether the their This works or not now in a lot of cases. Maybe you have a lot more You want to do rather than just one command probably the easiest way to do that is do a traditional if then statement Which we can do So let's actually just do that. I'm going to go Vim I'll just call this My dur Dot sh so Vim is my text editor. You can use whatever text are you like as long as it's a text editor and not a word processor That's a personal choice And then I'm going to start with my shebang line here Let's make that a little bit bigger so you can see so pound exclamation mark forward slash bin forward slash bash in this case Because I'm running a bash script. Although what we're going over should be pretty similar in most shells if not exactly the same so what I'm going to do here is I'm going to say if and And basically the same thing as before our brackets dash D. I'll say temp forward slash temp Well, if that then this Else this and close our if then statements. So If that is true, so then we're going to do this echo The directory Exists echo No comma it does not exist So this is basically doing the same thing we did before it's checking if this directory exists Then we're going to do this If that's not true. Well, the only other option is to do this and Then we close our if then statement So it's doing the same thing, but let's say you want to do a bunch of different things In your script This is probably a better route to go if you're not just doing a simple check this check that because a lot of cases You're probably do if this exists then continue if not either create it or exit out of your script. So Let's save that Make it executable with change mod plus x in the name of the script Dot slash the name of the script enter the directory Exists because my temp directory does exist. So I hope you found the tutorial useful Once again, I went over stuff. I've gone over in previous tutorials So be sure to go back and watch the previous tutorials They should be in a playlist right before this video if you're watching in the playlist There might be an annotation for the playlist on the screen if I did not forget Otherwise and go to films by Chris calm. That's Chris of the K There should be a link in the description Go to the video section and you can search for what we're looking for and we're working with exit codes So if you type exit codes two different words, you'll probably get all the videos I have on exit codes. So go ahead check that out. Got lots of other videos there more than you'll probably ever watch So check them out if you watch them all that's great. I appreciate it I hope you enjoy them and I hope that you have a great day