51

What does the ~ mean in an absolute file path?

I see this in the output of things like build scripts but the path does not exist.

Jens Erat
  • 17,507
  • 14
  • 61
  • 74
blade34r
  • 519
  • 1
  • 4
  • 3

6 Answers6

38

Normally it means the user's home directory e.g. ~mike/ would be the user mike's home directory, ~/ would be your own home directory. However, it is unclear to me whether ~/ and ~mike/ should be considered absolute or relative; it seems to depend on the definition given (if anyone can come up with an authorative reference, please post a comment).

Note that I'm talking about Unix based systems here.

See http://en.wikipedia.org/wiki/Home_directory#Unix

Adrian Mouat
  • 513
  • 4
  • 8
  • 3
    They are absolute, because they are synonyms for absolute paths: on UNIX, the absolute path can be inferred from the contents of the `/etc/login` file. The expansion is traditionally done by the shell, but any language that has pretensions to be "scripting" will do this as well. – Charles Stewart Nov 16 '10 at 11:20
  • 2
    +1: I didn't know about the `~username/` thing. – Wuffers Nov 16 '10 at 13:21
  • 3
    Interestingly, Windows PowerShell also accepts `~` as a synonym for the user's home directoy. – Joey Nov 16 '10 at 13:51
  • 1
    Jeffery Snover has said that PowerShell was originally based around VIM/EMACs – Anonymous Type Nov 16 '10 at 22:21
  • 1
    @Charles Stewart arguably at least ~/ is relative as it depends on the users context. Also some references define a absolute path as one given from the root of the filesystem, which these obviously aren't. If you have a reference for your statement, please share! – Adrian Mouat Nov 17 '10 at 17:02
  • @Adrian: You can create a file called `~` in your current directory: try `touch "~"` and then `ls`. Tilde expansion happens before anything that manipulates paths gets hold of what you type into the shell: try `echo ~`. The latter gives an absolute path. – Charles Stewart Nov 23 '10 at 13:46
  • 1
    @Charles Stewart, I'm not sure what you're point is. ~/ isn't relative to the current directory, but it is relative to the home directory (normally /home, but I guess it could be /users or similar). It all depends on your definition of absolute and relative. – Adrian Mouat May 13 '13 at 15:29
  • @Adrian: The definition is from POSIX (see [Pathname resolution](http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_12) from the current standard). There's nothing particularly absolute about absolute pathnames: because of chroot, the path /bin for a process might resolve to a different directory than it would for its parent. – Charles Stewart May 14 '13 at 12:09
  • @CharlesStewart, The definition is [from the wild](https://en.wikipedia.org/wiki/Linguistic_description), not POSIX. – Pacerier Oct 15 '15 at 20:29
27

Actually, both of the answers by Adrian Mouat and studiohack are true.
In operating systems with limited naming convention (Older version of Windows/DOS etc') it signifies a long name.

e.g. "c:\program files\" is equivalent to "c:\progra~1\"

In some operating systems (namely Unix) it means home-dir (and might be seen as an absolute but not canonical path).
e.g."/a/vol01/usr/mike/" might be shortened to "~/mike/"
* where 'usr' is the home dir.

Eran
  • 3,421
  • 5
  • 34
  • 33
  • 2
    In the context of build scripts, it is probably the Unix-centric version. – Charles Stewart Nov 16 '10 at 11:22
  • 2
    Unix paths usually use forward-slashes rather than backslashes. – Torben Gundtofte-Bruun Nov 16 '10 at 13:52
  • @torbengb, true...opps – Eran Nov 16 '10 at 14:05
  • 1
    A small correction to Xenorose's otherwise excellent answer. File names like "progra~1" are not for older OSs. My Windows 7 system still uses them. (Do dir /x to see.) This is a legacy feature that supports old software that doesn't know about the long file names in modern systems. Old software thinks all filenames follow the [8.3 convention](http://en.wikipedia.org/wiki/8.3_filename). When a file name doesn't work with this convention, the file system automatically [creates](http://support.microsoft.com/kb/142982) a second, 8.3 compatible name. – Isaac Rabinovitch Sep 06 '12 at 06:14
13

On many file systems, a file name will contain a tilde (~) within each component of the name that is too long to comply with 8.3 naming rules.

Source: Naming Files, Paths, and Namespaces - Short vs. Long Names - MSDN

(Part-way down the page...)

studiohack
  • 13,468
  • 19
  • 88
  • 118
  • Hmm, doesn't quite work for names that contain dots. (e.g. `192.168.1.1` or `somefile.namewithdot`) – Pacerier Oct 15 '15 at 20:38
6

And if you do ASP.NET programming it means the top level of the website; rather than navigating using ../../images/some_image.jpg (and getting your nesting level wrong!) you can simply say ~/images/some_image.jpg

noonand
  • 257
  • 3
  • 14
  • 2
    `/images/some_image.jpg` should take you to the root of any web site. What additional functionality does the tilde provide in ASP.NET? – Sonny Nov 16 '10 at 16:12
  • 8
    ~ takes you to the web application root which is not the same as the web site root if you're using a virtual directory. For example, if your website is installed on myserver in virtual directory myapp, ~/images/myimage.jpg will resolve to http://myserver/myapp/images/myimage.jpg. See http://msdn.microsoft.com/en-us/library/ms178116.aspx. – MCS Nov 16 '10 at 16:18
2

More about Windows:

  1. If hidden file name starts with '~' then Windows Explorer process it as system hidden file. More info in Why are hidden files with a leading tilde treated as super-hidden?

  2. If short file/directory name contains '~' (like "c:\ololoo~1") it is possible for corresponding long name of this file/directory to exceed maximum length (MAX_PATH=260). Developers should workarond this with "\\?\" prefix (even on newer Windows 10 as user can disable ">260"-long paths support with LongPathsEnabled registry parameter or with "Enable NTFS long paths" group policy). Example for this workaround using C# can be found in ZetaLongPaths library sources.

oshatrk
  • 129
  • 2
1

Here is a couple of hints that can help you to figure it out better:

$ readlink -f ~

$ echo $HOME

Note: $ is a convention to specify the user command line prompt, it is not a part of the commands.

vtest
  • 5,130
  • 2
  • 27
  • 27