Why does backslash work for file or folder names, but question mark less so?
I created a HTML file as this name on Sierra 10.12.6: test\55.html
and it showed up in the address bar of Firefox as:
Users/MYNAME/Desktop/test\55.html
I then made a duplicate as test?\55.html
and it rendered in the browser as:
Users/MYNAME/Desktop/test%3F\55.html
If naming files, why does backslash seem to work? I know forward slash (/
) doesn't and renders as ::
in Finder, but for backslash and question mark, why do these work in Finder and should they be avoided in filenames?
I'm on HFS, not APFS, so does this relate to it, and in Mojave, would the problems around these not exist?
If anyone can answer I'd be grateful for this; done some basic research but am not fully sure.
Characters to avoid in filenames
You shouldn't be using slashes or question marks in your filenames. This can cause issues depending on the operating system/file system being used.
There's already an excellent answer in SuperUser What characters are safe in cross-platform file names for Linux, Windows and OS-X addressing this, but to summarize:
- forward slashes are used to separate values in the path in *nix operating systems
- back slashes are used to escape spaces in file names in *nix operating systems and as path separators in Windows.
- Windows does not like
\/:*?"<>|
in paths or filenames.
As for the "double colon" (::
) this is a convention used by Finder to represent the home directory. The single colon is a path separator.
Question Marks (and other characters) in URLs
This is not a good idea. First off, Windows users (the browsers actually) are going to have a hard time with the the file with the question mark. Secondly, The question mark indicates a query string in PHP meaning, you're sending the PHP language a query or a command to execute.
For instance, take a look at this URL:
https://www.bing.com/search?form=MOZLBR&pc=MOZI&q=php+url+question+mark
Putting an arbitrary question mark in the file name of the URL will definitely throw a monkey in the wrench.
Additionally, you're not "escaping" characters in the URL with the percent sign - it's called "Percent Encoding or URL Encoding." This allows you to use special or reserved characters (like a space) in the URI and have it be functional.
This has nothing to do with HFS, APFS or macOS Mojave really. It is simply the way URLs in any browser work.
URLs can have an optional query component that is marked by a single question mark (?) character. What comes after the question mark is the query part of the URL.
As your file name is not the query, but rather the actual name of the file - the question mark in the file name needs to be "escaped" (i.e. translated) so as to convey the full name without the browser thinking this is the query component of the URL.
Escapes in URLs start with a percent sign (%) and then the hex-codes character code for the character to escape - in this case 3F which is the question mark character. This is also known as "percent encoding".
The reason this is called "escaping" is that you're "breaking out" of the normal meaning that the characters following the escape character (%) would otherwise have had. In this case your file name does not contain the letters 3 or F, they have a different meaning because of the escape character %. RFCs 1738 and 2396 (URL "standards") explicitly call this escaping, whereas the newer RFC 3986 uses the term "percent encoding" in order to distinguish this particular form of escaping from others.
As for the backslash character, it has no special meaning in an URL today - so it is just treated like any other character. On macOS it also doesn't have a special meaning (whereas the forward slash is used as a directory seperator in some cases).
Note that pre-2005, it was recommended that the backslash character is escaped in URLs, but that is not necessary anymore. You might see some (older) software do just that with URLs. The escaped and non-escaped version has the same meaning, so it doesn't make any practical difference.