Pretty stupid when it comes to linux and I'm trying to figure out navigation with the command line. I can move back and forth from my home folder and the folders inside pretty easily with this:
cd ~/Documents/
but when I go to open a folder in documents i'm getting an error like this:
chance@asus:~/Documents$ cd ~/xmr-stak-cpu-1.3.0-1.5.0
bash: cd: /home/chance/xmr-stak-cpu-1.3.0-1.5.0: No such file or directory
I'm confused because I need to use "~" to get into documents, but I cannot navigate into a subsequent folder while in documents.
Best Answer
TL;DR
~/
in path means your home folder./
means in this folder or stuff in current working directory~
is relevant in command-line only, not in GUI (as mentioned in comments).The answer is edited to include more formal explanation of tilde expansion and to suit the new edited title of the question, so please read the more formal explanation below. First two sections are more or less gentle explanations of what OP did and didn't do.
The problem
The leading
~
(tilde) followed by slash in path is understood as reference to your user's home directory, i.e.~/Documents
always means/home/chance/Documents
. (There are exceptions and special cases but for the purpose of explaining the basics, let's keep it simple so far; see below for the deeper explanation).So when you do
cd ~/xmr-stak-cpu-1.3.0-1.5.0
, you're saying take me toxmr-stak-cpu-1.3.0-1.5.0
directory that is located in my home folder. What also is important to note, is that a path is always read left to right, separated by slashes, with the leftmost being the top of the directory tree that you reference. So if you want to descend from home, to Documents, to your folder, all pieces must be present in the path, separated by slashes, and the folder you want to end up in, of course, has to be the right most.Look at the error message closely:
The shell properly expanded
~
to be/home/chance
and joined that with what you gave it. Of course, because the directory you specified doesn't exist, you got the appropriate error message.What you should have done
You can do it in one of two ways - give full path or jump into
~/Documents
first, then jump to directory you want. When youcd ~/Documents
and want to navigate to folder that's located inside the current directory that you're at, you can use./
notation. So you would do:Of course you don't have to add that
./
, when you simply docd xmr-stak-cpu-1.3.0-1.5.0
will automatically look inside your current working directory. However, it's better to be explicit, and besides./
helps avoiding issues with filenames that might start with-
(which is a whole different bag of problems and is a topic for another day).Alternatively, you can give full path:
Note that you can also use the
$HOME
variable instead of~
, which is pretty much the same in effect, however they differ in nature and in the extra stuff that you can do with~
. See my answer on Difference cd and cd ~.More formal and in-depth explanation
Alright, so far we've kept things fairly simple for the purpose of explaining basics to new users. But there's much more to tilde, dot-slash, and navigating the directory tree than that. Matters get more complicated when we consider that tilde and
cd
behave differently in other shells, and if we also consider the POSIX standard. Some of the things here I've already touched on in my related answer on Unix & Linux, so be sure to check that out as well.In Bourne-like shells (that is
bash
,ksh
,ksh
-related implementations likemksh
, and Ubuntu's default/bin/sh
, which is actuallydash
) the unquoted leading tilde signifies Tilde Expansion, which is in fact specified as standard behavior by POSIX. Depending on what follows an unquoted leading tilde, it has different effects:with an unquoted leading
~
, when there are no characters following it (or if there's nothing between~
and next unquoted/
, i.e. a null string) it expands to the same value as$HOME
environment variable, i.e. your home folder. Socd ~
andcd ~/
mean change directory to your home folder. Note that~
and/
should be unquoted:with an unquoted leading
~
, the collection of characters that follow it are considered a possible login name. Socd ~testuser
orcd ~testuser/
would mean you want to jump intotestuser
's current working directory. Again, quoting matters:This is all standard behavior and works across the board in
dash
,ksh
, and of coursebash
. However, there are a few things that shells do differently. In particular,bash
has extra expansions that you can perform with tilde, and handles an unsetHOME
differently than other shells. Here are the extra expansions you can do inbash
:~+
references your current working directory, same asPWD
variable. This also works inksh
but notdash
.~-
references your previous working directory, same asOLDPWD
variable. Also works inksh
but notdash
.In
bash
you can navigate AND record where you've been by pushing specific directories on the directory stack (or array), and you can reference them by~[+-]<NUMBER>
, where+
would mean looking from the left of the array, and-
from the right. This isbash
specific and doesn't work work inksh
nordash
.See diagram in muru's answer to a related question, which shows how to use indexes for
~+
and~-
.Another thing that I've already hinted at is the unset
HOME
environment variable. The fact that it is a variable and can be unset poses a problem - as specified by POSIX standard, tilde expansion depends on the environment variables, but according to the standard "[i] HOME is unset, the results are unspecified." In this case,ksh
anddash
simply break:In fact, this is in accordance with the standard for
cd
as well:By contrast
bash
, handles things differently. According to the bash manual:In other words, when home is unset only, bash won't break, but if
HOME
is set to the null string, it will break:Fun fact
~
was chosen to represent user's home directory because one upon a time~
andHOME
keys used to reside on the same keyboard key on Lier-Siegler ADM-3A terminal. (source)See also:
~
considered to be a relative path?