Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr3882 :: Alternatives to the cd command

Navigate your filesystem without cd

<< First, < Previous, , Latest >>

Hosted by Klaatu on 2023-06-20 is flagged as Clean and is released under a CC-BY-SA license.
bash, linux. (Be the first).
The show is available on the Internet Archive at: https://archive.org/details/hpr3882

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:10:32

Bash Scripting.

This is an open series in which Hacker Public Radio Listeners can share their Bash scripting knowledge and experience with the community. General programming topics and Bash commands are explored along with some tutorials for the complete novice.

Five or six ways I could think of to roam the files of your Linux computer without cd.

pushd and popd dirs

https://en.wikipedia.org/wiki/Pushd_and_popd

The pushd command, when called with a filepath as an argument, saves the current working directory in memory (via a directory stack) so it can be returned to at any time, places the new filepath at the top of the stack, and changes to the new filepath. The popd command returns to the path at the top of the directory stack.

cd -

From man bash An argument of - is converted to $OLDPWD before the directory change is attempted.

Variables

EXPORT mydir="/path/to/file/"
cd ${mydir}

History and histverify

Using !number from the history command will execute the command

[user@pc ~]$ history
1  cd tmp
2  cd ~
3  history

[user@pc ~]$ !3
cd tmp
[user@pc tmp]$

from the man command

       shopt [-pqsu] [-o] [optname ...]
              Toggle the values of settings controlling optional shell behavior.
              ...
              -s     Enable (set) each optname.
              -u     Disable (unset) each optname.

Now using !number from the history command will put the command on the prompt but you need to execute it yourself

[user@pc ~]$ shopt -s histverify
[user@pc ~]$ !39673
[user@pc ~]$ cd tmp

autocd command

[ken@kalani ~]$ shopt -s autocd
[ken@kalani ~]$ tmp
cd -- tmp
[ken@kalani tmp]$ ~
cd -- /home/ken

working without changing to directory

[ken@kalani ~]$ ls tmp

Comments

Subscribe to the comments RSS feed.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
What is the HOST_ID for the host of this show?
What does HPR mean to you?