What are invalid characters for a file name under OS X?

23

3

I am having trouble finding this information, and trial and error is telling me that there may not be any. What are invalid characters for a file name on OS X Lion (10.7)?

Rodney Foley

Posted 2011-08-21T19:32:12.500

Reputation: 332

1Single forward slash ? Single backslash ? Single or double period ? – None – 2011-08-21T19:35:51.517

All the slash combo's and singles seem to work, but a single and double period do fail because they are already in use. I hope there is more than that.... :) – Rodney Foley – 2011-08-21T19:43:15.947

You can't use an ASCII NUL. Think a while and you might spot why yourself… – Donal Fellows – 2011-08-21T19:45:06.287

@Donal ASCII NUL is not something you can easily just "type" or do problematically by mistake. – Rodney Foley – 2011-08-21T19:48:47.523

@rodney what on earth has what you can easily type by accident got to do with anything. Do you want help or not?! Sheesh! – David Heffernan – 2011-08-21T19:54:07.533

1Can you give us some context, Rodney? It would help a lot in answering this if we understood a little bit about what you're trying to do... AFAIK, there are a few potential issues: honest-to-god forbidden characters, characters that are impossible to enter as a path in a terminal shell, and characters that won't display properly in Finder. – Shog9 – 2011-08-21T20:03:19.300

Looking for the Apple doc that give's the specifics of what is legal or not thats really it. I can find MSDN Doc's on Windows, and ones for each Linux platform about this, but nothing from Apple I can find. Its a simple question that context shouldn't really matter if you know the answer, if you do not then do not comment or try to answer its simple. – Rodney Foley – 2011-08-21T20:20:05.070

It's built on BSD, so my educated guess would be "it's the same as UNIX". Check my answer, though. – None – 2011-08-21T20:37:03.440

@Rodney: The key point I was making is that NUL is illegal, precisely because it is the marker for the end of the filename. – Donal Fellows – 2011-08-22T09:31:30.437

Answers

8

HFS Plus allows "Unicode, any character, including NUL. OS APIs may limit some characters for legacy reasons"

tobylane

Posted 2011-08-21T19:32:12.500

Reputation: 1 271

3Techincally the HFS+ file system does allows all Unicode characters (since it represents names internally as Pascal strings with a 2-byte length), but AFAIK none of the APIs provided by OS X allow you create/open files with a slash '/' or NUL '\0' in them. – Adam Rosenfield – 2011-08-21T20:54:58.620

APIs as in Carbon or Cocoa? I assume Terminal is under such restrictions, but what about scripts run by other programs, such as launchd? – tobylane – 2011-08-21T20:58:19.950

8

What are invalid characters for a file name under OS X?

wiki HFS+:

Allowed characters in filenames Unicode, any character, including NUL. OS APIs may limit some characters for legacy reasons


Finder doesn't currently allow entering colons in in file names, but it does allow entering backslashes. However the characters shown as slashes in Finder are shown as colons in shells, and vice versa. Finder doesn't allow any ASCII control characters either.


Bash seemed to allow all control characters except \000 (NUL). I tested it by running the output from:

for i in {1..31} 127 0; do echo touch \\$(bc <<< "obase=8;$i"); done

Lri

Posted 2011-08-21T19:32:12.500

Reputation: 34 501

Could you test / (POSIX path separator) and : (HFS path separator) with bash? – Cœur – 2020-02-05T07:21:31.253

2

EDITED

According to Wikipedia, the only invalid character is the forward-slash /, but at the UNIX layer.

user3463

Posted 2011-08-21T19:32:12.500

Reputation:

2So, no forward slash (/) or colons (:) to be safe? – Nicolas Miari – 2016-02-16T12:07:04.687

2@NicolasMiari Yes. Finder.app won’t allow you to enter a colon, and won’t work properly with filenames that contain it. The UNIX layer won’t allow you to work with files containing a slash (or rather; it translates it to a colon). And then there are other applications (such as Microsoft Word) who choke on either. For instance, if you save a document in TextEdit.app under the filename foo/bar.docx (which works), Microsoft Word will be unable to open it. – Konrad Rudolph – 2016-05-20T14:07:02.310

2Maybe : is prohibited on Mac OS Classic, but certainly not on Mac OS X—touch foo:bar works perfectly fine. – Adam Rosenfield – 2011-08-21T20:56:15.543

1Answer has been updated. You're welcome to remove your downvote. – None – 2011-08-21T21:38:12.257

5@Adam: : is valid in the “unix layer”, but it is translated to/from / in the “Mac layers” (i.e. Finder, most file-related dialogs, etc.): the colon is used as the separator in “HFS paths” and the slash is used as the separator in “POSIX paths” so there is a two-way translation depending on which “layer” you are working with. – Chris Johnsen – 2011-08-22T04:33:01.377