Is there anything that corresponds to the ~ character found in Linux. E.g. CD ~\Downloads to go to the user's download folder from anywhere?
- 27,773
- 24,631
- 40
- 135
- 202
2 Answers
The ~ (Tilde) is a built in shortcut that autocompletes to the users 'Home' folder. Normally the home folder will be found under /home/ however it can infact be in almost any location on the system (including on network drives). This is defined in /etc/passwd.
So cd ~/Downloads is actually running the command cd /home/<username>/Downloads
- 6,713
-
1Thanks for this, I did learn something, but this is tagged windows – Louis Waweru Jun 25 '17 at 13:56
There's nothing like ~ in Windows cmd, but you can use %USERPROFILE% or %HOMEDRIVE%%HOMEPATH% like $HOME in *nix. See
However in PowerShell you can use ~ like on Linux. cd ~ will work as expected or you can shorten it to cd in PowerShell Core like how you can on Linux
The tilde character (~) is shorthand notation for the current user's home folder. This example shows
Resolve-Pathreturning the fully qualified path value.Resolve-Path ~Path
C:\Users\User01
PS C:\> Push-Location ~ -StackName Stack2 PS C:\Users\User01> Pop-Location -StackName Stack2 PS C:\>The first command pushes the current location onto a new stack named Stack2, and then changes the current location to the home directory, represented in the command by the tilde symbol (
~), which when used on a FileSystem provider drives is equivalent to$HOMEand$env:USERPROFILE.
In PowerShell Core cd alone without a location behaves exactly like on Linux where cd and cd ~ both changes to the home directory
-PathSpecify the path of a new working location. If no path is provided,
Set-Locationdefaults to the current user's home directory. When wildcards are used, the cmdlet chooses the first path that matches the wildcard pattern.
- 27,773