是否存在无效的 Linux 文件名?
如果我想创建一个保证不代表文件名的字符串,我可以在 Windows 上将以下字符之一放入其中:
\ / : * ? | < >
例如,
this-is-a-filename.png
?this-is-not.png
有没有办法在 Linux 上将字符串标识为“不可能是文件”?
If I wanted to create a string which is guaranteed not to represent a filename, I could put one of the following characters in it on Windows:
\ / : * ? | < >
e.g.
this-is-a-filename.png
?this-is-not.png
Is there any way to identify a string as 'not possibly a file' on Linux?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
几乎没有任何限制 - 除了
'/'
和'\0'
之外,您可以使用任何内容。然而,有些人认为允许这么大的灵活性并不是一个好主意。There are almost no restrictions - apart from
'/'
and'\0'
, you're allowed to use anything. However, some people think it's not a good idea to allow this much flexibility.空字符串是 Linux 上唯一真正无效的路径名,如果您只需要一个无效名称,那么它可能适合您。您还可以使用类似“
///foo
”的字符串,尽管它可以引用文件(“/ foo
”)。另一种可能性是类似“/dev/null/foo
”,因为/dev/null
具有 POSIX 定义的非目录含义。如果您只需要无法引用常规文件的字符串,则可以使用“/
”或“.
”,因为它们始终是目录。An empty string is the only truly invalid path name on Linux, which may work for you if you need only one invalid name. You could also use a string like "
///foo
", which would not be a canonical path name, although it could refer to a file ("/foo
"). Another possibility would be something like "/dev/null/foo
", since/dev/null
has a POSIX-defined non-directory meaning. If you only need strings that could not refer to a regular file you could use "/
" or ".
", since those are always directories.从技术上讲,它不是无效的,但名称开头带有破折号(-)的文件会给您带来很多麻烦。这是因为它与命令参数冲突。
Technically it's not invalid but files with dash(-) at the beginning of their name will put you in a lot of troubles. It's because it has conflicts with command arguments.
我个人发现很多时候问题不在于Linux,而在于Linux 上使用的应用程序。
以 Amarok 为例。最近,我注意到我从 Windows 计算机复制的某些艺术家没有出现在库中。我检查并确认文件在那里,然后我注意到文件夹名称中的某些字符(以艺术家命名)用一个看起来很奇怪的正方形而不是实际的字符表示。
在 shell 终端中,文件名看起来更奇怪: /Music/Albums/Einst$'\374'rzende\ Neubauten 就是一个例子,说明了如何奇怪。
虽然这些文件确实存在,但 Amarok 由于某种原因看不到它们。我能够使用一些 shell 技巧将它们重命名为正常版本,然后我可以使用 Musicbrainz Picard 用纯 ASCII 字符重新命名。不幸的是,Picard 也无法打开这些文件,直到我重命名它们,因此需要 shell 脚本。
总的来说,这是一个棘手的领域,如果您尝试在 Windows 和 Linux 之间同步音乐收藏,其中某些文件夹或文件名包含时髦字符,这似乎会变得非常棘手。
最安全的做法是坚持仅使用 ASCII 文件名。
I personally find that a lot of the time the problem is not Linux but the applications one is using on Linux.
Take for example Amarok. Recently I noticed that certain artists I had copied from my Windows machine where not appearing in the library. I check and confirmed that the files were there and then I noticed that certain characters in the folder names (Named for the artist) were represented with a weird-looking square rather than an actual character.
In a shell terminal the filenames look even stranger: /Music/Albums/Einst$'\374'rzende\ Neubauten is an example of how strange.
While these files were definitely there, Amarok could not see them for some reason. I was able to use some shell trickery to rename them to sane versions which I could then re-name with ASCII-only characters using Musicbrainz Picard. Unfortunately, Picard was also unable to open the files until I renamed them, hence the need for a shell script.
Overall this a a tricky area and it seems to get very thorny if you are trying to synchronise a music collection between Windows and Linux wherein certain folder or file names contain funky characters.
The safest thing to do is stick to ASCII-only filenames.