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?
-
1I guess you can modify the name of a file using direct access to your hardisk partition and patch in a '/' character somewhere. What happens is an interesting question ... most probably not what you want. – hochl Mar 23 '12 at 22:32
-
1But the short answer should be: no, this is not something that should ever be done :-) – Simeon Visser Mar 23 '12 at 22:32
-
1Does hacking a slash into the file name in the directory entry in the FS count? It wouldn't be recommended; you'd not be able to access the file, ever. – Jonathan Leffler Mar 23 '12 at 22:47
-
57This reminds me of the time my friend made a file named `*` and then asked, "How do I remove a file?" I answered, `rm` followed by the filename. Well, you know the rest. – David Heffernan Mar 23 '12 at 22:57
-
3For new Linux users, when you're not confident about an expression or file name, I think that it's good practice to use `ls` to list the files that you want to remove and then to change the `ls` command to `rm` afterward. – Dave F May 06 '19 at 03:58
8 Answers
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.

- 3,782
- 4
- 16
- 33

- 16,759
- 15
- 61
- 87
-
10Also, note that at least `e2fsck` considers any filename as an illegal filename that has to be fixed—[see the source](http://git.kernel.org/?p=fs/ext2/e2fsprogs.git;a=blob;f=e2fsck/pass2.c#l455). So if you somehow end up with a filename that has slashes in it, you can use `fsck` to fix the problem. – ehabkost Sep 04 '12 at 14:58
-
10
You could use a Unicode character that displays as /
(for example the fraction slash), assuming your filesystem supports it.

- 175
- 13

- 1,131
- 1
- 8
- 14
-
51Yes, precisely: only /, which is U+002F `SOLIDUS`, is forbidden. There are plenty of other suitable candidates: ⁄ is U+2044 `FRACTION SLASH`; ∕ is U+2215 `DIVISION SLASH`; ⧸ is U+29F8 `BIG SOLIDUS`; / is U+FF0F `FULLWIDTH SOLIDUS`, and ╱ is U+2571 is `BOX DRAWINGS LIGHT DIAGONAL UPPER RIGHT TO LOWER LEFT`. All would work admirably! – tchrist Mar 23 '12 at 22:38
-
2But 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!) – Evi1M4chine Jun 05 '16 at 09:30
-
@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. – Trevor Boyd Smith Sep 14 '18 at 12:01
-
1For a list of possible replacements on numerous characters, which are forbidden under different filesystems, have a look at my answer: https://stackoverflow.com/a/61448658/4575793 – Cadoiz May 09 '20 at 16:13
-
It’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. – LeopardShark Dec 15 '20 at 20:18
-
My terminal (the Gnome default) displays the character like this https://i.stack.imgur.com/JSBzu.png and the Dolphin file manager in this fashion https://i.stack.imgur.com/5Qhfy.png – user598527 Apr 11 '22 at 20:25
-
Depends on the intent of the user. If reliably encoding/decoding the / character into a filename is desired, I suggest URL encoding only `/` and `%` - that is, replace `/` with `%2F` and `%` with `%25`. – Benjamin Atkin Sep 26 '22 at 00:39
It depends on what filesystem you are using. Of some of the more popular ones:

- 1,106
- 11
- 25
-
2it doesn't depend only on the file system, system calls in all *nix systems will parse the / as a component of the directory tree. – Blackle Mori Mar 23 '12 at 22:35
-
3The forward slash character is hard-coded into the kernel, independent of the file system (try doing `grep -r "'/'" *` in your kernel source) – Robert Martin Mar 23 '12 at 23:17
-
21@tchrist Excuse me. "Forward slash" is a completely [acceptable](http://en.wikipedia.org/wiki/Slash_(punctuation)) way of referring to the slash character to make thoroughly clear which [slash](http://en.wikipedia.org/wiki/Slash_(musician)) one refers to. Sometimes people get confused :P – Robert Martin Mar 26 '12 at 23:01
-
2Hah, 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. – Jesse W. Collins Mar 27 '18 at 17:24
-
7@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 "/". – Stuart R. Jefferys Jun 04 '18 at 04:17
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.

- 179,497
- 17
- 214
- 278
-
20"that which we call a slash by any other name would smell as foul" -- Shakespeare – Robert Martin Mar 23 '12 at 23:14
In general it's a bad idea to try to use "bad" characters in a file name at all; even if you somehow manage it, it tends to make it hard to use the file later. The filesystem separator is flat-out not going to work at all, so you're going to need to pick an alternative method.
Have you considered URL-encoding the URL then using that as the filename? The result should be fine as a filename, and it's easy to reconstruct the name from the encoded version.
Another option is to create an index - create the output filename using whatever method you like - sequentially-numbered names, SHA1 hashes, whatever - then write a file with the generated filename/URL pair. You can save that into a hash and use it to do a URL-to-filename lookup or vice-versa with the reversed version of the hash, and you can write it out and reload it later if needed.

- 3,266
- 21
- 33
The short answer is: you must not. The long answer is, you probably can or it depends on where you are viewing it from and in which layer you are working with.
Since the question has Unix
tag in it, I am going to answer for Unix
.
As mentioned in other answers that, you must not use forward slashes in a filename.
However, in MacOS
you can create a file with forward slashes /
by:
# avoid doing it at all cost
touch 'foo:bar'
Now, when you see this filename from terminal you will see it as foo:bar
But, if you see it from finder
: you will see finder converted it as foo/bar
Same thing can be done the other way round, if you create a file from finder with forward slashes in it like /foobar
, there will be a conversion done in the background. As a result, you will see :foobar
in terminal but the other way round when viewed from finder.
So, :
is valid in the unix layer
, but it is translated to or from /
in the Mac layers like Finder window, GUI. :
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 happening, depending on which “layer” you are working with.
See more details here: https://apple.stackexchange.com/a/283095/323181

- 473
- 8
- 13
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.

- 18,829
- 16
- 59
- 101
You can have a filename with a / in Linux and Unix. This is a very old question, but surprisingly nobody has said it in almost 10 years since the question was asked.
Every Unix and Linux system has the root directory named /. A directory is just a special kind of file. Symbolic links, character devices, etc are also special kinds of files. See here for an in depth discussion.
You can't create any other files with a /, but you certainly have one -- and a very important one at that.

- 2,436
- 4
- 15
- 30
-
-
It's a directory. And a directory (like many other things in Unix) is a file. – George Feb 03 '22 at 02:32