windows 是否有任何无效的 linux 文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1311037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:40:22  来源:igfitidea点击:

Are there any invalid linux filenames?

windowslinuxfilefilesystemsfilenames

提问by izb

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:

如果我想创建一个保证不代表文件名的字符串,我可以在 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?

有没有办法在 Linux 上将字符串标识为“不可能是文件”?

采纳答案by Vinay Sajip

There are almost no restrictions - apart from '/'and '\0', you're allowed to use anything. However, some peoplethink it's not a good idea to allow this much flexibility.

几乎没有任何限制 - 除了'/''\0',您可以使用任何东西。但是,有些人认为允许如此大的灵活性并不是一个好主意。

回答by mark4o

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 canonicalpath name, although it could refer to a file ("/foo"). Another possibility would be something like "/dev/null/foo", since /dev/nullhas 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.

空字符串是 Linux 上唯一真正无效的路径名,如果您只需要一个无效名称,它可能对您有用。您还可以使用像“ ///foo”这样的字符串,虽然它可以引用文件(“ ”),但它不是规范路径名/foo。另一种可能性类似于“ /dev/null/foo”,因为/dev/null具有 POSIX 定义的非目录含义。如果您只需要不能引用常规文件的字符串,您可以使用“ /”或“ .”,因为它们始终是目录。

回答by OOPMan

I personally find that a lot of the time the problem is not Linux but the applications one is using on Linux.

我个人发现很多时候问题不在于 Linux,而是在 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.

以 Amarok 为例。最近我注意到我从我的 Windows 机器上复制的某些艺术家没有出现在图书馆中。我检查并确认文件在那里,然后我注意到文件夹名称中的某些字符(以艺术家命名)用一个看起来很奇怪的方块而不是实际字符来表示。

In a shell terminal the filenames look even stranger: /Music/Albums/Einst$'\374'rzende\ Neubauten is an example of how strange.

在 shell 终端中,文件名看起来更奇怪: /Music/Albums/Einst$'\374'rzende\ Neubauten 是一个多么奇怪的例子。

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.

虽然这些文件肯定在那里,但 Amarok 由于某种原因无法看到它们。我能够使用一些 shell 技巧将它们重命名为合理的版本,然后我可以使用 Musicbrainz Picard 使用纯 ASCII 字符重命名这些版本。不幸的是,Picard 也无法打开文件,直到我重命名它们,因此需要一个 shell 脚本。

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.

总的来说,这是一个棘手的领域,如果您尝试在 Windows 和 Linux 之间同步音乐收藏,其中某些文件夹或文件名包含时髦的字符,它似乎变得非常棘手。

The safest thing to do is stick to ASCII-only filenames.

最安全的做法是坚持只使用 ASCII 文件名。