1. Overview

In this article, we’ll look at the possibility of adding the “/” character to a filename in Linux. We’ll cover the Linux kernel restriction on using the character in filenames and then delve a little deeper into the code of the rename system call.

Finally, we’ll discuss a workaround that we can use to include a Unicode character similar to “/” in our filenames.

2. Is It Possible to Use “/” in Filenames?

The use of the “/” (Solidus) character in filenames is restricted by Linux because it’s reserved for system use. Technically, Linux uses the “/” character as a delimiter to organize the directory tree. For instance, we can access the video0 device in Linux using the /dev/video0 path. Here, we have two path components, namely the dev directory and the video0 symlink. Using a “/” in any of these two names will affect the ability to access the location of the target file.

For that reason, when a filename contains a “/” character, the Linux kernel will not be able to correctly parse the path. Therefore, it’s not possible to use a regular “/” character in a filename. There is one more reserved character called the NUL character (\0) that is also restricted by Linux from being used in a filename.

2.1. The Rename System Call

The renameat filesystem call is responsible for locating a file and renaming it. This call is defined in the fs/namei.c file. Whenever this system call is invoked, it carries out a path lookup. Behind the scenes, this path lookup will invoke a function named link_path_walk, where we’ll find a check that basically uses the “/” character as a delimiter.

Let’s have a brief look at this function:

static int link_path_walk(const char *name, struct nameidata *nd)
{
    ...
    struct path next;
    int err;
    unsigned int lookup_flags = nd->flags
    while (*name=='/')
        name++
    if (!*name)
        return 0;
    ...
}

This function walks through the specified file path in the while statement. As we can see, the “/” delimiter is hardcoded in the function and cannot be escaped. Therefore, it doesn’t matter which file system we’re using because this system call applies to all file systems. However, if we do encounter a file system that allows the “/” character in a filename, there are two possible reasons for it:

  • The “/” character is not a regular forward-slash but rather a Unicode character that looks like a forward-slash
  • The file system contains a bug

If, for some reason, we still need to have the “/” character in a filename or at least a character that looks like “/”, we can use a Unicode character instead, which we’ll cover next.

3. Workaround: Using a Unicode Character

For each character in the ASCII character set, it’s possible that the character has a look-alike in the Unicode set. Although not every system or software supports Unicode, it’s still the most widely used standard supported by major Linux distributions.

3.1. Alternatives to “/” in Unicode

We can use the following characters in place of the regular forward-slash (U+002F):

  • U+2044 = Fraction Slash:  ⁄
  • U+2215 = Division Slash:  ∕
  • U+29F8 = Big Solidus:  ⧸
  • U+FF0F = Fullwidth Solidus:  ╱
  • U+2571 = Box Drawings Light Diagonal Upper Right to Lower Left:  ╱

3.2. Putting the Alternative Slashes in Filenames in Linux

On Linux, we can enter a Unicode character by pressing the ++u keys. As we press the keys, we can notice a lower-case underlined “u” that indicates the prompt for entering a Unicode identifier.

Let’s open any file manager and try it out:

Renaming with Unicode

Now, let’s enter the “2215” code and press enter:

Inserting Unicode in a Filename

Once we rename a file with the Unicode character in it, we can observe that our file now contains the “Division Slash” in the filename:

Renamed File

Sometimes, depending on the font and Unicode character, the character will overlap with other characters. In that case, we can experiment with the other slash characters to choose one that looks better.

In the same way, we can insert the Unicode characters on most command-line terminals as well. However, some terminals might have their own way of adding Unicode characters. In that case, we can refer to their official documentation for help.

4. Fixing Filenames That Contain “/”

We can’t do normal operations on a file that contains a regular “/” in its filename. For instance, we can’t remove the file as it will treat the “/” character as a delimiter. Not only that, but we also can’t do rm * on it because the shell will expand the wildcard to the appropriate filename(s).

Fortunately, we can fix those filenames by running the fsck utility on the filesystem. Most major Linux distributions come with fsck pre-installed, so we can use this utility to remove the slashes from the filenames. Before running it, make sure the filesystem is unmounted:

# fsck -AC

The -A option runs fsck on all partitions, while the -C option will display a progress bar.

5. Conclusion

In this article, we covered why using “/” in filenames is not possible in Linux. Then, we briefly took a look at the code that hardcodes the prevention of using this character in filenames. Afterward, we discussed using other unrestricted alternative characters that look like “/”.

Finally, we briefly saw how we could fix filenames that contain the “/” character using fsck.