0

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?

phuclv
  • 27,773
Louis Waweru
  • 24,631
  • 40
  • 135
  • 202

2 Answers2

1

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

1

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-Path returning the fully qualified path value.

Resolve-Path ~

Path

C:\Users\User01

Resolve-Path

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 $HOME and $env:USERPROFILE.

Push-Location

In PowerShell Core cd alone without a location behaves exactly like on Linux where cd and cd ~ both changes to the home directory

  • -Path

    Specify the path of a new working location. If no path is provided, Set-Location defaults to the current user's home directory. When wildcards are used, the cmdlet chooses the first path that matches the wildcard pattern.

Set-Location

phuclv
  • 27,773