Is it possible to use "/" in a filename?
Solution 1
The answer is that you can't, unless your filesystem has a bug. Here's why:
There is a system call for renaming your file defined in fs/namei.c
called renameat
:
SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
int, newdfd, const char __user *, newname)
When the system call gets invoked, it does a path lookup (do_path_lookup
) on the name. Keep tracing this, and we get to link_path_walk
which has this:
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 code applies to any file system. What's this mean? It means that if you try to pass a parameter with an actual '/'
character as the name of the file using traditional means, it will not do what you want. There is no way to escape the character. If a filesystem "supports" this, it's because they either:
- Use a unicode character or something that looks like a slash but isn't.
- They have a bug.
Furthermore, if you did go in and edit the bytes to add a slash character into a file name, bad things would happen. That's because you could never refer to this file by name :( since anytime you did, Linux would assume you were referring to a nonexistent directory. Using the 'rm *' technique would not work either, since bash simply expands that to the filename. Even rm -rf
wouldn't work, since a simple strace reveals how things go on under the hood (shortened):
$ ls testdir
myfile2 out
$ strace -vf rm -rf testdir
...
unlinkat(3, "myfile2", 0) = 0
unlinkat(3, "out", 0) = 0
fcntl(3, F_GETFD) = 0x1 (flags FD_CLOEXEC)
close(3) = 0
unlinkat(AT_FDCWD, "testdir", AT_REMOVEDIR) = 0
...
Notice that these calls to unlinkat
would fail because they need to refer to the files by name.
Solution 2
You could use a Unicode character that displays as /
(for example the fraction slash), assuming your filesystem supports it.
Solution 3
It depends on what filesystem you are using. Of some of the more popular ones:
Solution 4
Only with an agreed-upon encoding. For example, you could agree that %
will be encoded as %%
and that %2F
will mean a /
. All the software that accessed this file would have to understand the encoding.
Solution 5
The short answer is: No, you can't. It's a necessary prohibition because of how the directory structure is defined.
And, as mentioned, you can display a unicode character that "looks like" a slash, but that's as far as you get.
![subcan](https://i.stack.imgur.com/OVm9w.png?s=256&g=1)
Comments
-
subcan over 2 years
I know that this is not something that should ever be done, but is there a way to use the slash character that normally separates directories within a filename in Linux?
-
Blackle Mori over 12 yearsit doesn't depend only on the file system, system calls in all *nix systems will parse the / as a component of the directory tree.
-
tchrist over 12 yearsYes, precisely: only /, which is U+002F
SOLIDUS
, is forbidden. There are plenty of other suitable candidates: ⁄ is U+2044FRACTION SLASH
; ∕ is U+2215DIVISION SLASH
; ⧸ is U+29F8BIG SOLIDUS
; / is U+FF0FFULLWIDTH SOLIDUS
, and ╱ is U+2571 isBOX DRAWINGS LIGHT DIAGONAL UPPER RIGHT TO LOWER LEFT
. All would work admirably! -
Robert Martin over 12 years"that which we call a slash by any other name would smell as foul" -- Shakespeare
-
Robert Martin over 12 yearsThe forward slash character is hard-coded into the kernel, independent of the file system (try doing
grep -r "'/'" *
in your kernel source) -
Robert Martin over 12 years@tchrist Excuse me. "Forward slash" is a completely acceptable way of referring to the slash character to make thoroughly clear which slash one refers to. Sometimes people get confused :P
-
ehabkost almost 12 yearsAlso, note that at least
e2fsck
considers any filename as an illegal filename that has to be fixed—see the source. So if you somehow end up with a filename that has slashes in it, you can usefsck
to fix the problem. -
Evi1M4chine about 8 yearsBut then what if the user uses those actual characters in his file/dir names? We need a generic escaping solution. Too bad Linux’s normal code doesn’t support any, as it literally matches on ASCII 0x2F. ASCII is a big no-no since at least 20 years. (Unicode 1.0 is from 1991!)
-
Jesse W. Collins over 6 yearsHah, but @tchrist also has a point, I think. Why does forward' imply '/' and 'back' imply '\'? The best explanation I have so far is that if written with a pen starting on a line, from the bottom up, '/' moves right or 'forward' and '\' moves 'left' or 'back', when reading/writing from left to right. I don't really like that explanation though, in part because I don't always write my characters from the bottom and move up. I think starting from the top and moving down while writing a character often flows better.
-
Stuart R. Jefferys about 6 years@jwso This is totally a side point, but this is standard, canonical language. Slash is not what unicode calls symbols that looks like these, it calls them solidus, but "\" is a reverse solidus, which is synonymous with backwards, hence backslash. But if one needs a justification, back and forward are the direction the line leans or should fall, with direction based on the direction of writing (left to right). It leans or should fall <== or backwards if it looks like "\", and ==> or forwards if it looks like "/".
-
Trevor Boyd Smith almost 6 years@tchrist i prefer not to depend on unicode. so i would probably prefer a multi-character delimiter like
---
. your delimeter choice can use a different character and vary the number of repetitions. -
flarn2006 over 5 years@ehabkost Any filename? Sounds like a bug in
e2fsck
:p -
Cadoiz about 4 yearsFor a list of possible replacements on numerous characters, which are forbidden under different filesystems, have a look at my answer: stackoverflow.com/a/61448658/4575793
-
LeopardShark over 3 yearsIt’s not a redundant glyph, it’s a different character for a different purpose: compare 1 ∕ 3 with 1 / 3. It’s still fine as a replacement in this case, though.
-
kravemir over 2 years
/
is definitely a path, but is it a file? -
George over 2 yearsIt's a directory. And a directory (like many other things in Unix) is a file.
-
user598527 about 2 yearsMy terminal (the Gnome default) displays the character like this i.stack.imgur.com/JSBzu.png and the Dolphin file manager in this fashion i.stack.imgur.com/5Qhfy.png