When creating filepaths and URLs, I noticed that many times the path starts with ./
or ~/
.
What is the difference between filepaths that start with ./
and ~/
?
What do each of them mean?
When creating filepaths and URLs, I noticed that many times the path starts with ./
or ~/
.
What is the difference between filepaths that start with ./
and ~/
?
What do each of them mean?
./
is the current directory~/
is the home directory of the current user./
means "starting from the current directory"..
refers to the current working directory, so something like./foo.bar
would be looking for a file calledfoo.bar
in the current directory. (As a side note,..
means refers to the parent directory of the current directory. So../foo.bar
would be looking for that file one directory above.)~/
means "starting from the home directory". This could have different meanings in different scenarios. For example, in a Unix environment~/foo.bar
would be looking for a file calledfoo.bar
in your home directory, something like/home/totzam/foo.bar
. In many web applications,~/foo.bar
would be looking for a file calledfoo.bar
in the web application root, something like/var/http/mywebapp/foo.bar
../
means that path is relative to your current position.~/
means that path is relative to your home directory.For completeness sake ...
path
is a file or directory namedpath
in the current directory../path
is a file or directory namedpath
in the current directory, with the directory spelled out..
is the current directory, andpath
is the name of the file or directory within the current directory.~/path
is a shorthand for$HOME/path
where$HOME
is a variable which refers to your home directory. Typically your home directory will be somewhere like/home/you
or/Users/you
whereyou
is your account name. (The commandecho "$HOME"
will display your home directory.) The expanded value is an absolute path (unless you have messed up the value of$HOME
thoroughly), as indicated by the initial slash./path
is an absolute path which refers to a file or directory namedpath
which is in the root directory/
. Every file on Unix is ultimately somewhere in the directory tree which starts with the root directory.Every file name which begins with
/
is an absolute path (aka full path) which explains how to reach a particular node starting from the root directory. For example,/var/tmp/you/reminder.txt
refers to a file or directoryreminder.txt
(probably a file, judging from the name; but Unix doesn't care what you call your files or directories) which is in the directoryyou
which is in the directorytmp
which is in the directoryvar
which is in the root directory.Every file name which doesn't begin with
/
is a relative path which indicates how to reach a particular file or directory starting from the current directory. The special directory..
is the parent directory and the special directory.
is the current directory. Sopath/there
refers to the file or directorythere
inside the directorypath
in the current directory; and (hover the mouse over the yellow area to display the spoiler)In addition to
~/
for the current user's home directory, some shells and applications allow the notation~them/
to refer to the home directory of the user accountthem
. Also, some web server configurations allow each user to have a public web site in their directory~/public_html
and the URL notationhttp://server/~them/
would serve up the site of the user accountthem
for outside visitors.The current directory is a convenience which the shell provides so you don't have to type long paths all the time. You can, if you want to.
is a longwinded but perfectly valid way to say (assuming you are in your home directory),
You could also say
and until you
cd
again, all your commands will run in this directory.(There is a difference in that
ls
will print the path of the files you ask it to list, too. Sols directories.txt
will simply printdirectories.txt
whereasls Documents/unix-101/directories.txt
will print ... that.)You can always put an absolute path instead of a relative one, or vice versa; and you generally don't need to
cd
anywhere in particular (except some basically broken beginner scripts tend to require you to be in a particular directory when you run them).When you first log in, the current working directory is set to your home directory.
A "directory" is sometimes called a "folder" by people who are not yet old enough to prefer the former.
Notice how it looks from this like
ls
has to be a file in the current directory, and yet we also say that it's in/bin
? That's a different question (look up$PATH
).When
.
is not in yourPATH
(as generally it should not be), you have to say./scriptname
instead ofscriptname
to run the commands in an executable file calledscriptname
in the current directory. In other words, this is a corner case were you explicitly have to spell out./
to specify something in the current directory (or equivalently but verbosely spell out the full absolute path, perhaps with a command substitution$(pwd)/scriptname
but this is really pleonastic.)Also, don't confuse the directory name
.
with the Bourne shell command which comprises a single dot (also known by its Bash aliassource
). The commandruns the commands from the file
./scriptname
in the context of the current shell instance, as opposed to in a separate subshell (which is what just./scriptname
does). In other words, this command line invokes the dot command on a filescriptname
in the dot directory.