Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I move between folders in a command-line session without losing my place?

Quick answer

Before moving, save or note your starting location. In Bash, use pwd to check it, cd path/to/folder to move, and cd "$start_dir" to return if you first saved it with start_dir=$PWD. In PowerShell, use Get-Location, Set-Location path\to\folder, and return with Set-Location $startLocation after saving $startLocation = Get-Location. Paths depend on your prepared practice folder. [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I want to practice moving between folders, but I don’t want to lose track of where I started. What should I do first?

Geez

Use a prepared practice folder—not a location containing work you do not want to disturb—and record your starting location before moving. The commands differ slightly between Bash and PowerShell, so first identify which shell you are using. In either one, the basic routine is: save or note the starting place, move to a practice folder, check the current location, then return.

In Bash, run pwd to print the current directory’s absolute path. The Bash manual describes cd as changing the shell’s current working directory, and pwd as printing that absolute path. [S1] You can also save the starting path as a variable before the exercise.

Follow-up question

Can you show me the Bash version using a small folder exercise?

Geez

Assume your prepared practice folder contains practice/notes and practice/drafts, and that you start in the folder containing practice. First save your starting location, then move into notes and inspect where you landed:

start_dir=$PWD
pwd
cd practice/notes
pwd

The first pwd should show the starting directory; the second should show a path ending in practice/notes. Those are expected kinds of output, not exact paths: yours will depend on where the exercise folder is located. To try another folder, run cd ../drafts from practice/notes, then pwd again. To return to the saved starting location, run cd "$start_dir" and verify with pwd.

Bash also documents cd - as a way to return to the previous directory. [S1] That is handy for a quick back-and-forth, but saving start_dir gives you an explicit return point for the whole exercise.

Follow-up question

What does the same exercise look like in PowerShell, and how do I check whether I actually moved?

Geez

Assume the same prepared folder layout and that PowerShell starts in the folder containing practice. Save the current location, move into notes, and inspect the result:

$startLocation = Get-Location
Get-Location
Set-Location practice/notes
Get-Location

Set-Location changes PowerShell’s current location, and Get-Location reports it. [S2] Look for a displayed path ending in practice/notes; your full path will vary. For the second folder, use Set-Location ../drafts, then run Get-Location to check. PowerShell documents moving to a parent using a relative path, which is why .. in this example refers to the parent folder. [S2]

When finished, return to the saved place with Set-Location $startLocation, then run Get-Location again. The location display is your check; don’t assume a command worked just because you entered it.

Follow-up question

What if the command reports an error or the displayed folder is not what I expected?

Geez

Pause and inspect the location before trying another path. Run pwd in Bash or Get-Location in PowerShell. Compare the displayed path with the folder names and starting point you wrote down. Check that you used the command for your current shell and that the path matches the prepared folder layout; do not substitute a guessed path from another computer.

If you are unsure where you started, do not keep navigating at random. Use the saved variable if you created it (cd "$start_dir" in Bash or Set-Location $startLocation in PowerShell), then check the location again. If the saved variable was never set, stop and identify the correct starting path before continuing. A particular folder’s existence and exact output path depend on your own setup. [S1][S2]

A practical checklist

  • Use a prepared practice folder and note its expected subfolder names.

  • Identify whether the session is Bash or PowerShell.

  • Save the starting location before navigating.

  • After each move, run pwd in Bash or Get-Location in PowerShell.

  • Return to the saved location and check it once more.

A few more useful answers

Can I use the same command in Bash and PowerShell?

Not always. Use cd and pwd for the Bash steps shown here; use Set-Location and Get-Location for the PowerShell steps.

Will the example show the same full path on my computer?

No. The displayed path depends on where your practice folder is located. Check that the ending folder name matches the step you intended.

What if I did not save my starting location?

Stop rather than guessing. Inspect the current location and identify the correct starting path before continuing; the return-variable examples only work if you saved that location first.

Sources

References used for this guide.

  1. [S1] Bash Reference Manual: Bourne Shell Builtins
    gnu.org · Checked
  2. [S2] Managing current location in PowerShell
    learn.microsoft.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work