17

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?

albert
  • 5,966
  • 3
  • 13
  • 29
Tot Zam
  • 7,081
  • 9
  • 45
  • 66

5 Answers5

21

./ means "starting from the current directory". . refers to the current working directory, so something like ./foo.bar would be looking for a file called foo.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 called foo.bar in your home directory, something like /home/totzam/foo.bar. In many web applications, ~/foo.bar would be looking for a file called foo.bar in the web application root, something like /var/http/mywebapp/foo.bar.

David
  • 176,566
  • 33
  • 178
  • 245
21

For completeness sake ...

  • path is a file or directory named path in the current directory.
  • ./path is a file or directory named path in the current directory, with the directory spelled out. . is the current directory, and path 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 where you is your account name. (The command echo "$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 named path 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 directory reminder.txt (probably a file, judging from the name; but Unix doesn't care what you call your files or directories) which is in the directory you which is in the directory tmp which is in the directory var 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. So path/there refers to the file or directory there inside the directory path in the current directory; and (hover the mouse over the gray area to display the spoiler)

there/.././and/back/.. is a (wicked complicated) way to refer to the directory and in the current directory, where we traverse the there directory and then move back to the current directory; then stay in the current directory; then refer to the directoryback inside the directory and, but then move back to the parent directory of that, ending up with ./and.

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 account them. Also, some web server configurations allow each user to have a public web site in their directory ~/public_html and the URL notation http://server/~them/ would serve up the site of the user account them 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.

/bin/ls /home/you/Documents/unix-101/directories.txt

is a longwinded but perfectly valid way to say (assuming you are in your home directory),

ls Documents/unix-101/directories.txt

You could also say

cd Documents/unix-101
ls directories.txt

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. So ls directories.txt will simply print directories.txt whereas ls 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).

A common beginner mistake is assuming that the location of a script or an executable dictates where it looks for files; but then if that were true, ls . would display files in the bin directory instead of your current directory, wouldn't it?

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 your PATH (as generally it should not be), you have to say ./scriptname instead of scriptname to run the commands in an executable file called scriptname 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 alias source). The command

. ./scriptname

runs 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 file scriptname in the dot directory.

tripleee
  • 139,311
  • 24
  • 207
  • 268
  • 3
    I like your explanation of how `~/path` is shorthand for `$HOME/path`. That definitely would have made things clearer when I asked this question back when I was first starting to program, and I'm sure others will find the explanation helpful in the future. Thanks for this detailed response even 3+ years later! – Tot Zam Mar 25 '19 at 20:11
  • Very good explanation. As far as the tilde (~) character goes, it actually means to take everything between the ~ and the first / and treat that as a username. If that space is empty, like ~/ or if the user does not exist, then it defaults to the current user. This is something that could lead to troubles if a script is run as another user or root using sudo and a malicious string. – Michael Treanor Mar 26 '19 at 05:20
  • 1
    @Skeptycal Thanks, I was just thinking about adding a passage about `~user`. Done now. – tripleee Mar 26 '19 at 05:40
  • This broadly applies to Windows and DOS and some other similar atrocities as well, with the difference that DOS etc has a number of file systems, each with a separate drive letter (and the separator was backslash `\ ` instead of forward slash `/`). Typically `C:\ ` was the root of the first hard drive (and `A:\ ` was the root of the first floppy drive) back in the 286 days, though there are variations. – tripleee Oct 22 '19 at 07:56
  • The companion answer to [What exactly is a current directory](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904#66860904) has an overview of similar things with a focus on explaining the concept of current working directory. – tripleee May 05 '21 at 06:38
5

./ is the current directory

~/ is the home directory of the current user

vadian
  • 232,468
  • 27
  • 273
  • 287
3

./ means that path is relative to your current position.

~/ means that path is relative to your home directory.

Tot Zam
  • 7,081
  • 9
  • 45
  • 66
fillo
  • 31
  • 2
0

I will explain a simple example of it. As developers mentioned:

  • ./ is current directory.
  • ~/ is the home directory of the current user.

How both of the file path expressions can help us? Suppose you want to execute a script (.sh) and you're in the same directory where file exists then you can simply do it ./filename.sh

I mostly use ~/ to access my home directory files like .bashrc when I want to add any config in it. It's easier since the file path expression (for home directory) feels much easier and makes accessibility to the file from anywhere, without worrying about the path or changing the path.

Harshit
  • 1,228
  • 11
  • 35