The ~ character is a universal alias in the Unix word for referencing the home directory of the user. It is a special character that will be parsed and replaced by the full path of the current user's home directory. Is there an equivalent of this in cmd.exe ? (not Powershell)
Asked
Active
Viewed 2,447 times
8
adamency
- 376
1 Answers
18
No.
You can use commands like cd /D %homedrive%\%homepath% or cd /D %userprofile% but typing them is just not the same even if end result is. The closest to the simplicity of cd ~ I've ever seen is Señor CMasMas's elegant solution below.
Create a new bat file with one single line:
@cd /d %UserProfile% –
Save it with name cd~.bat into any folder in your %PATH%. After that you can get from anywhere in the system back to home directory by typing command
cd~
phuclv
- 27,773
Peregrino69
- 4,664
-
13It is funny that you mention this. I have a
cd~.batfile in my path that has one line in it..@cd /d %UserProfile%– Señor CMasMas Sep 23 '21 at 02:18 -
1
-
6Be aware that
%homepath%does not include the drive, just the path relative to that drive.%homedrive%contains the drive letter. – Ross Presser Sep 23 '21 at 12:14 -
-
-
1In Unix you wouldn't even type
cd ~, justcdbecause$HOMEis already the default destination.~is useful when you're cd'ed somewhere else and want to reference a file in your home dir, e.g.vim ~/notes.txtorcp -a foo ~/bin/. (Or in a script where you might be.) Anyway, isn't%homedrive%\%homepath%basically equivalent to$HOME, expanding an environment variable? When you say it's "not the same", do you mean it's too long to type? – Peter Cordes Sep 23 '21 at 22:30 -
@PeterCordes Yap, I generally drop the tilde except in scripts,
cd ~just has a bit of charming nostalgia :-) And of course written downcdandcd ~don't mean the same thing.%homedrive%\%homepath%as concept is more/users/$USER,%UserProfile%is more$HOME, methinks. I updated the answer again. – Peregrino69 Sep 23 '21 at 23:07 -
In Unix,
cdandcd ~do mean the same thing. Tilde expansion happens after word splitting, so even if your$HOMEcontains spaces, it still works likecd "$HOME"instead of breaking. Maybe you meant in Windows? I don't know Windowscmd.exevery well. – Peter Cordes Sep 23 '21 at 23:15 -
They do, typed in shell, but "typed in shell" != "written down". Writing on a paper, forum etc.
cd= "change directory", butcd ~= "change directory to $HOME". That's what I meant :-) – Peregrino69 Sep 23 '21 at 23:25
C:\Users\<current user>\Documents– phuclv Sep 23 '21 at 04:15