What directory is '~' when I type 'cd

2019-02-18 07:26发布

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?

11条回答
走好不送
2楼-- · 2019-02-18 07:41

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

查看更多
一纸荒年 Trace。
3楼-- · 2019-02-18 07:41

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

查看更多
姐就是有狂的资本
4楼-- · 2019-02-18 07:43

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.

查看更多
淡お忘
5楼-- · 2019-02-18 07:44

~ 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".

查看更多
再贱就再见
6楼-- · 2019-02-18 07:46

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.

查看更多
登录 后发表回答