Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What directory is '~' when I type 'cd ~'?

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?

like image 609
damnitshot Avatar asked Feb 14 '09 08:02

damnitshot


People also ask

What directory is cd in?

The cd\ command takes you back to the root directory of the current drive. As shown below, if you were in the same \Windows\System32 directory and typed the cd\ command, it would take you to the C:\ directory.

Does cd Mean Change directory?

The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.

How do I cd to the main directory?

To navigate to your home directory, use "cd" or "cd ~" To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -" To navigate through multiple levels of directory at once, specify the full directory path that you want to go to.

How do I find the last location of a cd?

You can try it out yourself by opening a Terminal window, navigating somewhere within the file system, then immediately switching directories to another location. Now just type cd – to switch back to the prior location, and cd – again to switch back to the original location.


3 Answers

~ 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
like image 185
Alex Wayne Avatar answered Oct 07 '22 17:10

Alex Wayne


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

like image 33
Hannes Ovrén Avatar answered Oct 07 '22 17:10

Hannes Ovrén


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.

like image 41
Jonathan Leffler Avatar answered Oct 07 '22 17:10

Jonathan Leffler