ChatGPT解决这个技术问题 Extra ChatGPT

Windows equivalent to UNIX pwd

How do I find the local path on windows in a command prompt?

Very useful information, although I believe this now belongs in superuser.com

g
gsamaras

This prints it in the console:

echo %cd%

or paste this command in CMD, then you'll have pwd:

(echo @echo off
echo echo ^%cd^%) > C:\WINDOWS\pwd.bat

i got access denied and searched for solution , it took time so switched to another answer cd only
This does not print the full directory name if you have accessed the directory via the ~ notation, e.g. "C:\PROGRA~3"
Why do you need to echo? Just cd by itself seems to work fine.
cd /? says Type CD without parameters to display the current drive and directory.
@shareef well when you wanna write into C:\windows you might need admin. but you could just write it somewhere you have permissions and add it to your path. the point of this second block is to basically have a pwd alias for convenience
D
Daniel A. White

It is cd for "current directory".


If you need it in a variable or so, using the %CD% pseudo-variable is probably easier.
Cute, but cd is for "change directory"
Related side note: While on Windows cd alone will print the current working directory, on Linux it will change to your user's home directory without printing anything. So beware if you're looking for something cross platform.
@DanielStevens the help line from cd says Displays the name of or changes the current directory.
@phuclv correct, the Windows documentation for cd does say that. The Linux documentation for cd says that when no path is given, it will change to your home directory. My local man pages use the wording: "Change the current directory to dir. if dir is not supplied, the value of the HOME shell variable is the default."
J
Joseph Stine

Open notepad as administrator and write:

@echo %cd%

Save it in c:\windows\system32\ with the name "pwd.cmd" (be careful not to save pwd.cmd.txt)

Then you have the pwd command.


p
phuclv
cd ,

it will give the current directory

D:\Folder\subFolder>cd ,
D:\Folder\subFolder

just cd is enough. The comma is useless
in cmd ,, ;, = are also delimiters just like space and tab, therefore cd , is no different from cd =;= or cd <space>
Doesn't work anymore on Windows 10
R
Roy Tinker

cd without any parameters is equivalent to pwd on Unix/Linux.

From the console output of typing cd /?:

Displays the name of or changes the current directory.

[...]

Type CD without parameters to display the current drive and directory.

p
phuclv

You can just type

cd

it will return you the current path.


p
phuclv

In PowerShell pwd is an alias to Get-Location so you can simply run pwd in it like in bash

It can also be called from cmd like this powershell -Command pwd although cd or echo %cd% in cmd would work just fine


That's the right answer that works for both cmd and PowerShell. How to extract the path though?
s
sean e

hmm - pwd works for me on Vista...

Final EDIT: it works for me on Vista because WinAvr installed pwd.exe and added \Program Files\WinAvr\Utils\bin to my path.


Nor vista. Are you using powershell?
I'm not using powershell. I seem to recall something about command extensions but can't find a checkbox anywhere for that. I've also got completion in my Command Prompt. I could swear there used to be an applet in Control Panel to enable command extensions but I can't find it now.
Command extensions are enabled by default on Windows NT and later. Tab completion is separate from that and was available from Windows 2000 onwards and enabled by default since XP. pwd only works here because I have a pwd.cmd with "echo %cd%" in my path. You can use gcm pwd in Powershell to check where it comes from on your machine (sort of like which(1), only better).
gcm pwd reports that pwd is an Alias with a definition of Get-Location.
Late to the party but usually you can figure out the location where pwd is coming from by the command 'where' on command prompt.
p
phuclv
C:\Documents and Settings\Scripter>echo %cd%
C:\Documents and Settings\Scripter

C:\Documents and Settings\Scripter>

for Unix use pwd command

Current working directory


don't you see for Unix use pwd command useless? Because it's what the OP asked
Doesn't work in Powershell
p
phuclv

Use the below command

dir | find "Directory"

this won't work if there's a file or folder named "Directory" in the current directory, or if Windows language is not English, because output of dir is localized
M
Mastereve

You can simply put "." the dot sign. I've had a cmd application that was requiring the path and I was already in the needed directory and I used the dot symbol.

Hope it helps.