8

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)

5
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~

9
  • 13
    It is funny that you mention this. I have a cd~.bat file in my path that has one line in it.. @cd /d %UserProfile% Sep 23 at 2:18
  • 1
    That's the most elegant solution I've ever seen :D Sep 23 at 9:35
  • 6
    Be aware that %homepath% does not include the drive, just the path relative to that drive. %homedrive% contains the drive letter. Sep 23 at 12:14
  • @RossPresser Good point, let's update the answer :-) Sep 23 at 12:16
  • 1
    In Unix you wouldn't even type cd ~, just cd because $HOME is 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.txt or cp -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? Sep 23 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.