170 lines
9.5 KiB
Plaintext
170 lines
9.5 KiB
Plaintext
|
|
Episode: 3882
|
||
|
|
Title: HPR3882: Alternatives to the cd command
|
||
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr3882/hpr3882.mp3
|
||
|
|
Transcribed: 2025-10-25 07:14:42
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
This is Hacker Public Radio, Episode 3882 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 Klaatu, and is about 11 minutes long.
|
||
|
|
It carries a clean flag.
|
||
|
|
The summary is, Navigate your file system without CD.
|
||
|
|
Hey everybody, this is Klaatu, 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 PushD and PopD.
|
||
|
|
In the bash shell, the PushD, PopD, and DERS, DIRS, commands manage a map of everywhere
|
||
|
|
you go on your system.
|
||
|
|
They only do that when you use them, so you have to mentally substitute PushD instead
|
||
|
|
of CD, or you could alias it, something like that.
|
||
|
|
But here's how it works.
|
||
|
|
And you want to change directory to a new location, use PushD to push your current
|
||
|
|
location onto the stack.
|
||
|
|
The stack in this context is an imaginary plate, well it's not imaginary, it's a place
|
||
|
|
in memory that remembers where you've been.
|
||
|
|
So for example, PWD, HomeKlaatu, okay, well PushD documents, PWD, HomeKlaatu documents.
|
||
|
|
To see what's on the stack, use the DERS command, DIRS.
|
||
|
|
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, PushD, 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 DIRS, tilde pictures, tilde documents, tilde, pop D, or I don't even have to type in
|
||
|
|
theirs, it tells me, tilde documents, tilde.
|
||
|
|
So to condense that down into one sentence explanation, PushD 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, DIRS to 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.
|
||
|
|
If you 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 PushD 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, I'm 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 home, clatoon slash dot local
|
||
|
|
slash Etsy slash myder slash config slash example and then there are a bunch of files
|
||
|
|
in there.
|
||
|
|
Instead of typing some variation of emax, home clatoon dot local slash Etsy slash 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 clatoon slash dot local slash Etsy slash myder slash
|
||
|
|
config slash example.
|
||
|
|
And then from then on, you can just use myder.
|
||
|
|
So emax, 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.
|
||
|
|
Four, 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 till the
|
||
|
|
slash documents as command 758, exclamation 758, return and you've just changed directories
|
||
|
|
to till the till the document.
|
||
|
|
But the hissed verify option in the bash shell lets you verify a history line before you
|
||
|
|
execute.
|
||
|
|
Activate it with shopped shell options SH OPT, shoppedspace dash S to set hissed verify.
|
||
|
|
That's HIST 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 801 or whatever, 500, whatever.
|
||
|
|
But what it does is gives me the confidence to actually use something from history.
|
||
|
|
Sometimes I'll be a little bit nervous that I'm forgetting the number of the history
|
||
|
|
or something like that.
|
||
|
|
And so I won't 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'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 in that similar
|
||
|
|
path, but just a different sub directory, then I can just edit that command with hissed
|
||
|
|
verify, edit it before I accept it.
|
||
|
|
And now I've got a new command that I can start using out of my history.
|
||
|
|
5.
|
||
|
|
Auto-CD.
|
||
|
|
Auto-CD shall option in bash.
|
||
|
|
Let's you skip the CD command all together.
|
||
|
|
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 can enable it with the same way as with hissed verify.
|
||
|
|
You do SHOPT, that's like shell option, SHOPT, space-s to set, like Sierra, set-s, space
|
||
|
|
auto-CD.
|
||
|
|
Now just type the path of a valid directory.
|
||
|
|
So you can slash TMP.
|
||
|
|
Now I'm in the TMP folder.
|
||
|
|
It's that easy.
|
||
|
|
There's no CD to type.
|
||
|
|
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 hope someone can think of more,
|
||
|
|
but 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 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 pipe something to the, or like, redirect something to that
|
||
|
|
file 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 tend to go to two directories, maybe more often than I probably should.
|
||
|
|
I mean, admittedly, sometimes that's not the case.
|
||
|
|
Sometimes it is better to be in the directory because that way you only have to type the
|
||
|
|
file name.
|
||
|
|
You know, 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 lately, I have been trying to challenge myself a little bit, 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
|
||
|
|
CD there.
|
||
|
|
I just do it from wherever I am, just work out of my home directory.
|
||
|
|
It's kind of a fun challenge sometimes.
|
||
|
|
And that's kind of what made me think of this was 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 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 just half of the time doesn't really need
|
||
|
|
to be done.
|
||
|
|
As it does, like I say, file paths can get long and cumbersome.
|
||
|
|
And for one-offs, 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 maybe, 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 Hacker Public Radio, as Hacker Public Radio does work.
|
||
|
|
Today's show was contributed by a HBR listener, like yourself, if you ever thought of recording
|
||
|
|
a podcast, and click on our contribute link to find out how easy it really is.
|
||
|
|
Hosting for HBR has been kindly provided by an honesthost.com, the Internet Archive
|
||
|
|
and our sync.net.
|
||
|
|
On the Sadois status, today's show is released under Creative Commons, Attribution 4.0 International
|
||
|
|
License.
|