views:

586

answers:

11

I'm only new to using SSH, but when I log in I end up in the directory ~, which in general is the same directory when FTPing in. I can still go to /, but I don't know what the ~ means. Home? Where is it in relation to /, or how could I find out?

+8  A: 

~ is your home directory, yes. Which is very nice since your home directory is not always where you think it should be (/home/).

Also, fun fact: You can use "cd ~myuser" to get to the home directory of the user "myuser".

kigurai
One time it won't be where you "think" it should be is on OS X - "~" expands to "/Users/myuser" rather than "/home/myuser". It's nice to use "~" so us OS X users can still get the same functionality!
Chris Lutz
Older Unix systems often didn't have a /home directory, so the home directories were under something like /usr/users. Using "~" works for them, too.
David Thornley
+2  A: 

Yes, it is the home directory of the user you logged in as. You can use the command pwd (print working directory) to see where it is located on the file system.

cowgod
+13  A: 

~ is an alias to the currently logged in users home directory. To find out where that really is, type pwd (stands for: Print Working Directory) right after logging in, which should give you the location relative to /. It's probably something like:

/home/myusername
Squeegy
Your profile chain can quite easily change your current working directory before the prompt appears. A more reliable way is "( cd ; pwd )" to, in a subshell, change to your home directory then print out the working directory.
paxdiablo
Actually, that's not true either, since the .bashrc runs for each subshell as well (hence can change directory). Even $HOME can be modified :-). Safest is to check your entry in /etc/passwd.
paxdiablo
~ is always whatever $HOME is set to, so ctrl's suggestion of "echo ~" will work, as will "echo $HOME" or "cd;pwd" (since cd defaults to changing to $HOME). You shouldn't second-guess yourself, pax. :)
dannysauer
A: 

And home, in relation to /, isn't necessarily always in the same place. That's why the ~ shortcut is so useful. The path to home should be in $HOME. Try typing

echo $HOME

gbarry
A: 

/ is the root of the file system ~/ or ~ is the root of your profile, ~/ is always /home/username

tHeSiD
It's not always /home/username since you're not forced to have your home directory under /home. I'd suggest clarifying that before you get -1'ed :-)
paxdiablo
And it isn't forced to be the directory listed in the password file. It just usually is.
Jonathan Leffler
A: 

Home directory need not necessarily be under /home as kigurai has pointed out.

Alan Haggai Alavi
+6  A: 

As others have commented, the tilde indicates your current $HOME directory. This may or may not be the same as the value of ~username for your user name. On my machine, $HOME and ~ both refer to /work1/jleffler. However, ~jleffler is a reference to an NFS mounted directory, /u/jleffler, as specified in the /etc/passwd file (or any equivalent database - the POSIX standard defines the behaviour in terms of the getpwnam() function; see below). My profile carefully sets $HOME. It is interesting (aka exasperating) to work out which software packages use the wrong definition of the home directory.

For most people, ~ and ~username are the same for their user name, but that is not required. Given that you are asking the question, it is almost certainly the case that ~ and ~username are the same.

Quote from section 2.6.1 'Tilde Expansion' of POSIX.1-2008:

A "tilde-prefix" consists of an unquoted <tilde> character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash> in the word, or all the characters in the word if there is no <slash>. [...] If the login name is null (that is, the tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variable HOME. If HOME is unset, the results are unspecified. Otherwise, the tilde-prefix shall be replaced by a pathname of the initial working directory associated with the login name obtained using the getpwnam() function [...]. If the system does not recognize the login name, the results are undefined.

Jonathan Leffler
How strange this is the post that talks about /etc/passwd and ~, but it is not the accepted answer.
Johan
Got there an hour too late :D
Jonathan Leffler
+2  A: 

~ expands to your home directory, as has been pointed out, but I think it's worth noting that isn't a feature of ssh itself.

ssh (among many other wonderful features!) lets you establish a remote shell, and this shell can provided by many different pieces of software.

On a *nix system, your account will be associated with a particular shell, GNU bash is a popular choice. And it so happens than in bash, and most other POSIX compliant shells, the tilde character expands as a shortcut to your home directory.

Paul Dixon
+3  A: 

You can try realpath

realpath ~
kalyanji
A: 

Different shells may or may not handle this differently, but Johnathan got the closest without coming out and saying it. The shell expands "~" to whatever's stored in the $HOME environment variable. The shell expands ~username to whatever's listed in the shell field of /etc/passwd for the given username. If you don't override it, the shell (or ssh, depending on the implementation) will set $HOME to be the home field from /etc/passwd, so they're both the same (assuming you're "username") until you change one.

As to why you see a ~ in ssh...

The prompt says "~" is your current directory most likely because you're using Bash as your shell, and the value of $PS1 (the prompt string you see - it's set in /etc/profile or /etc/profile.d/*, more than likely) contains a \w or a \W somewhere. The \w string in the prompt shows the current directory, and collapses to a "~" when you're in the directory specified by $HOME. Here's a little demo starting in my homedir - note how the "\w" gets replaced with either the current directory or with a ~, based on what the value of HOME is set to. Also note that the trailing slash doesn't work - HOME can't end with a slash for this to work. :)

danny@machine ~ > export PS1='\w > '     # change my prompt (effective on next line)
~ > cd /tmp                              # move to /tmp to demonstrate
/tmp > export HOME=/tmp/                 # set HOME to include trailing /
/tmp > export HOME=/tmp                  # try again without trailing /
~ >                                      # notice that this works
~ > cd /home/danny                       # back to homedir
/home/danny > export HOME=/home/danny    # see how it's /home/danny, not ~
~ > export PS1='danny@machine \w > '     # after resetting $HOME, it should work ok
danny@machine ~ >                        # hooray!
dannysauer
+1  A: 

~ is your home directory. To see the path type:

echo ~

in the terminal

John T