This is Hacker Public Radio Episode 3,882 for Tuesday, the 20th of June 2023. Today's show is entitled, Alternatives to the CD Command. It is part of the series Bash Scripting. It is hosted by Clotoo and is about 11 minutes long. It carries a clean flag. The summary is Navigate your file system without CD. Hey everybody, this is Clotoo and this episode I want to talk about changing directories. I know that's not seemingly a very exciting topic, but what I want to do in this episode is come up with as many ways I can think of to navigate a system in Linux without CD or not in the way that you would expect with CD. So the first one that I could think of was Push-D and Pop-D. In the Bash Shell, the Push-D, Pop-D and Ders, D-I-R-S commands manage a map of everywhere you go on your system. I mean they only do that when you use them so you have to mentally substitute Push-D instead of CD or you can alias it, something like that. But here's how it works. When you want to change directorie to a new location, use Push-D to push your current location onto the stack. The stack in this context is an imaginary play, well it's not imaginary, it's a place in memory that remembers where you've been. So for example, PWD, Home-Platoo, okay? Well, Push-D documents, PWD, Home-Clatoo documents. To see what's on the stack, use the Ders command, D-I-R-S. It says, tilde slash documents, space, tilde. It's listing my history on one line, starting with the most recent that is your current location of in this example, tilde documents, followed by another tilde for the Home-Directory because that's where I started from. You can add more to your stack by moving around your file system some more, so for instance, Push-D, tilde slash pictures, Ders, tilde pictures, tilde documents, tilde. To pop a directory off the stack and make it your current location, use the pop D command. The pop D command removes the most recent directory from the stack and makes it your current location and displays your new stack with the left directory representing your new location. So D-I-R-S, tilde pictures, tilde documents, tilde, pop D, all right, I don't even have to type in, Ders, it tells me, tilde documents, tilde. So to condense that down into one sentence explanation, Push-D, to change a directory to a new place, pop D, to take a directory off of your stack and make it your new current location. Ders to a view your stack. Next, CD space dash, I know, I said without CD, but this is with CD, but there's a slight difference here. This is maybe some people don't know about this little trick. When you find yourself switching back and forth between two directories, you do not have to type the directories each time. So don't even have to use control P or the up arrow, you can use CD-Dash. The CD-Dash command takes you to your previous directory. It doesn't use the same stack as Push D does. It just remembers what your previous one directory was and then takes you to it and displays your current location. So if I'm in slash Etsy, I could do a CD slash var and then CD space dash on back in Etsy. CD space dash, I'm back in var, CD space dash, I'm back in Etsy, back and forth. Really, really convenient. Three variables. Sometimes you might find yourself returning or otherwise interacting with the same directory with a long and inconvenient name over and over again. For instance, suppose you're auditing some files in slash, or home, clout to slash dot local slash Etsy slash myder slash config slash exam. And then there are a bunch of files in there. Instead of typing some variation of e-max, home, clout to dot local slash Etsy slash mighty, myder config examples file one dot txt for every file you need to open, create a short variable and use it as your path instead. So export myder equals slash home slash clout to slash dot local slash Etsy slash myder slash config slash example. And then from then on, you can just use myder. So e-max dollar sign, myder slash file one dot txt done. The variable disappears when you reboot or you can destroy it manually when you're done with the unset command, unset myder. Now you don't have that variable or you could reset it to something else. Myder equals some other long directory and now you've got that one for to use for history and history verify. You might already know that the history command lets you see your past, I don't know, thousand or so commands as a numbered list. And you can use that list to instantly execute a previous command by number. The syntax is simply an exclamation mark followed by the number of the command you want to execute. So for example, if you do history, pipe head dash in three, you see maybe a CD tilde slash documents as command 758. Return and you've just changed directories to tilde, tilde document. But the hist verify option in the bash shell lets you verify a history line before you execute, activate it with shopped shell options, s-h-o-p-t, shopped space dash s to set hist verify that's h-i-s-t verify all one string. Now when you type in exclamation mark 758, instead of executing that command from your history, your next prompt contains the command and waits for you to either edit it or to accept it with a return or enter on your keyboard. So for me, that's not faster than just exclamation, you know, 801 or whatever, 500, whatever. But what it does is give, it gives me the confidence to actually use something from history. Because sometimes I'll be a little bit nervous that I'm forgetting the number of the history or something like that. And so I just, I won't, I'll end up not using it. But with that confirmation step before I actually do the thing, then I feel more comfortable about looking at history, remembering it, being confident that I remember it, and being confident that if I don't, I'll have a chance to not do the thing that I, that I thought I'm about to do. It also helps me edit commands. So if there is a long directory name and then I start using a similar name, you know, in that, a similar path, but just, just the, a different sub directory, then I can just edit that command with his to verify, edit it before I accept it. And now I've got a new command that I can start using out of my history. Five auto CD, the auto CD show option in bash lets you skip the CD command altogether. To change directory, you just type the path of a valid directory on your system, no CD required. It has to be enabled. So you, you can enable it with the same way as with his to verify, you do SHOPT, that's like shell option, SHOPT, space dash S to set, like Sierra, set, dash S, space auto CD. Now just type the path of a valid directory, so you can, like, slash TMP. Now I'm in the TMP folder, like it's that easy. There's no CD to, to type, it's, it's kind of nice. I mean, it's three fewer keyboard presses. And then finally, the last one, I could think of, and I, I hope someone can think of more. The last one, I could think of was not to change directory at all. It's a little bit of a cheat, but realistically, for a lot of the things on a Linux, in the terminal that you're doing in a, with a Linux command, you don't really, very often, you don't have to be in the place you're working. You can just open the file, or, or pipe something to the, or, like, redirect something to that file, or, or convert that file from a path to a path. You don't have to go there to do it. The command will do all of that legwork for you. I think I, I tend to go to, to, to, to directories, maybe more often than I probably should. I mean, admittedly, sometimes that's not the case. Maybe, sometimes it is better to be in the directory, because that way you only have to type the file name, or, you know, it, it's just, it's less to type, although, again, I could use a variable. So sometimes I just feel like I'm moving around too much, and, and lately, I have been trying to challenge myself a little bit, not, not, not too much, but a little bit, just when I think I want to do something to a file, or look at a file, or whatever, I don't see it either. I just do it from wherever I am, just, just work out of my home directory. It's kind of a fun challenge, sometimes, and that's, that's kind of what made me think of this was how, how could I get around CDing so much, because I've noticed that I do spend too much time, I think, CDing to places. It's kind of like, I've cut certainly, you know, a graphical file manager out of my life, not entirely, but, you know, if I don't, if I don't need one, if I don't want to use a graphical file manager, I don't have to. And so, I feel kind of like I replaced it with a bunch of CDing and L.S.ing a lot, and that's another, that's a new inefficiency, that, that just half of the time doesn't really need to be done. Sometimes it does, like I say, file paths can get long and cumbersome, and for one off, I mean, you may as well, CD there in L.S, because why not? Although maybe not, again, you could just L.S. So really, I think we may be possibly some people, myself, at least, we CD too much. So if you want to cut that down, think of a couple of ways or borrow my ways of not CDing, of not moving around your file system as much, you could save seconds of work for yourself. Thanks for listening to this episode. Go record your own on some cool tip that you have, talk to you next time. You have been listening to Hecker Public Radio, as Hecker Public Radio does work. Today's show was contributed by a HBO artist like yourself, if you ever thought of a recording podcast, click on our contributally to find out how easy it means. Hosting Prage VR has been kindly provided by an onsthost.com, the Internet Archive, and our Sync.net. On this otherwise stages, today's show is released on our Creative Commons, Attribution, 4.0 International License.