 Hello and welcome. This is a tutorial on the Linux shell and looking at a command when it comes to change a directory and getting back to the previous directory you're in. This little trick of this system-wide variable was brought to me by my attention by a viewer, Jan and Harold, probably about six months ago. But I'm finally getting around to doing this tutorial. So real simple, let's go over some basic CD stuff. So what I'm going to do here is PWD is a present working directory, I believe. And anyway, it's going to show you the directory you're currently in. So I do that and it says I'm in my home directory, which is home Metal X 1000. Now, let's say I was to move into another folder, E4 slash USR. And again, I can PWD. I know my prompt here is set up to show me what directory I'm in, but it may not be, or it may only show so much of the folder you're in. PWD will give you your folder. Also, you should be able to echo $PWD does the same thing, which is just another system-wide variable. So obviously, I can CD our little squiggly mark here to get back to, that's a till day, by the way, not a squiggly mark. That will move us back to our home directory. Also, you should also be able to just type CD and hit enter on most systems to get back to your home directory, which is actually even shorter. So again, I'm in my present working directory, which is my home directory here. And I really should have looked up if PWD is present working directory, because I feel like someone has corrected me before. That's not what that stands for, but that's how I remember it. Anyway, I'll go into a folder. And now let's say I want to go back to the last folder. And again, that's our home folder. Let's just move into another folder here, www.html. And then let's say I move into my home directory. Now, how do I move back to that last directory? Obviously, I can hit up a couple of times moving that, but let's say I just ran a bunch of commands. And now I want to get back to that directory. Let's say it's a big long name. And I actually had this incident the other day I was working with a friend on his web server. And we were down like five or six directories down in his web directory, and they all had long names. It didn't take long to type out, but it would have been nice for me to just flip back to that directory after switching over to the home directory real quick. And the way you do that, again, this is brought to me by attention by that viewer, is CD, dollar sign, old, whoops, old PWD. And when you do that, it brings you to the last directory you were in. So the last one you CDed from. So, again, I'm going to, if I run that again, it's going to be back to my home directory. If I run it again, it's going to be back to my HTML directory. Because it's just bringing me back to the last one I was in. So PWD. And now we can go to another directory. Let's say we go to our var directory. And now if I was to CD old PWD, it's going to bring me back to my home directory. I can move into USR. And again, do that again. So now, actually, let me keep go this var. So now, again, if I run this command over and over again, you're going to switch back and forth between USR and USR, which stands for Unix system resources, not user, and the var directory. So there's that. Now, there's actually a shorter way to do it. It actually makes a little bit of a difference, which you can do CD dash. So CD space dash. And that moves you to your previous directory, but also prints out the name of the previous directory before it moves, which is interesting. So that is actually an even easier way to remember how to do it. So CD dash. And you can see I'm going back and forth between my USR folder and my var folder. And of course, anytime you see dollar sign in a shell script, that that is a variable. And usually system variables are full capital, just like PWD, but old PWD. So I can just echo out that. And it's going to show me my previous folder. So if in a script, I change directory and I want to go back to a weird directory. I can't think of a reason you would do this, but it could come in handy if you thought of a situation. I'll just call this last dir equals and I can give it this variable here. And now I have put that into a variable. So now I can change directories multiple times and how to record that course. Then again, if you're running this in a script, you're going to want to, you know, probably save the directory before you change out of it if you want to go back to a directory. But for some reason you didn't, you already changed directories and you want to go back. That's how you do it. But the main thing I want to take you to take away from this tutorial is CD dollar sign old PWD to go to your previous directory or even shorter CD dash. And you know what? I would bet that there is another way to do it even, you know, like this, but without it printing out. Hold on one second. Yeah. So I just quickly Googled it. And when I Googled move to previous directory, this is the command that came up. So, I mean, you could always make something shorter with an alias as we talked about. Aliases in our last video alias, I can go old equals and then I can do this command. And now anytime I type old, it's going to move to my previous. Oh, no, it did. Oh, because you know what? Because this is a variable and it's saving the variable in there. So now, no matter where I go, Var is going to be old. So there should be a way that we could alias that. But if I, I mean, this is still doing. So now if I type old, old, CD, old, old, that's working but it's printing it. So, I mean, it's easier to type CD dash than that. But if you know of another command similar to CD dash that doesn't print out the variable, that'd be great. Of course, you could probably do CD dash null and alias that to CD dash, something like that. Anyway, now I'm just going off. I'm just trying to think, you know, is there another way to do it? If you know of one, comment below in the comments. So yeah, CD dash will bring you to your previous directory and print out what that previous directory is. Or you can do the variable way of capital, old, PWD. I do thank you for watching. Please visit filmsbychrist.com. That's Chris Decay. There's a link in the description. As always, I thank you for watching. Think about supporting over patreon.com. There's a link to that and my website in the description of the video. I hope you subscribe. As always, I hope that you have a great day.