如何删除文件名损坏的文件?

the*_*ker 6 files delete

不知何故,程序创建了一个文件名损坏的文件,无法再删除。任何删除文件的尝试都会导致“没有这样的文件或目录”,就好像该文件不存在一样。

问题似乎是文件名中的控制字符 ASCII 2。

$ ls
??[??????X

$ ls | xxd
00000000: 3f3f 5b3f 3f02 3f3f d8a9 3f58 0a         ??[??.??..?X.

# Typing '?' and letting the bash complete the filename
$ rm \?\?\[\?\?^B\?\??\?X 
rm: das Entfernen von '??[??'$'\002''????X' ist nicht möglich: Datei oder Verzeichnis nicht gefunden

$ rm *
rm: das Entfernen von '??[??'$'\002''????X' ist nicht möglich: Datei oder Verzeichnis nicht gefunden

$ ls -i
2532 ??[???????X
$ find -inum 2532 -delete
find: ‘./??[??\002????X’ kann nicht gelöscht werden.: Datei oder Verzeichnis nicht gefunden
Run Code Online (Sandbox Code Playgroud)

我试图fsck在重新启动后运行,但文件仍然存在。

$ zcat /var/log/upstart/mountall.log.1.gz
...
fsck von util-linux 2.25.1
/dev/sdc3: sauber, 544937/6815744 Dateien, 21618552/27242752 Blöcke
...
Run Code Online (Sandbox Code Playgroud)

没有迹象表明有问题。(“sauber”=干净)

我什至尝试编写自己的删除程序,但失败了,rm命令如下:

$ cat fix.c
#include <stdio.h>
#include <errno.h>

int main() {
    char filename[20];
    sprintf(filename, "%c%c%c%c%c%c%c%c%c%c%c%c", 0x3f,0x3f,0x5b,0x3f,0x3f,0x02,0x3f,0x3f,0xd8,0xa9,0x3f,0x58);
    printf("filename = %s\n", filename);

    int result = remove(filename);
    printf("result = %d\n", result);
    printf("errno = %d\n", errno);
    perror("Error");
    return 0;
}

$ gcc -o fix fix.c && ./fix
filename = ??[??????X
result = -1
errno = 2
Error: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我发现了类似的问题,那里的答案对我不起作用:

其他信息:

$ mount | grep " / "
/dev/sdc3 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)

$ uname -a
Linux hera 4.13.0-16-generic #19-Ubuntu SMP Wed Oct 11 18:35:14 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/issue
Ubuntu 17.10 \n \l
Run Code Online (Sandbox Code Playgroud)

有没有办法摆脱这个文件?

iga*_*gal 5

有很多选项可用于删除非 ASCII 文件名的文件。

我能够使用ANSI C 引用创建和删除具有正在讨论的文件名的文件:

# Create the offending file
touch $'\x3f\x3f\x5b\x3f\x3f\x02\x3f\x3f\xd8\xa9\x3f\x58\x0a'

# Verify that the file was created
ls -lib

# Remove the offending file
rm $'\x3f\x3f\x5b\x3f\x3f\x02\x3f\x3f\xd8\xa9\x3f\x58\x0a'
Run Code Online (Sandbox Code Playgroud)

看看这个帖子:

下面是从该帖子中获取的命令,该命令应删除当前目录中名称包含非 ASCII 字符的所有文件:

LC_ALL=C find . -maxdepth 0 -name '*[! -~]*' -delete
Run Code Online (Sandbox Code Playgroud)

您可以修改全局模式或使用正则表达式来缩小匹配范围。

这是另一篇相关帖子:

有一个建议尝试通过 inode 删除。首先运行ls -lib查找有问题文件的 inode,然后运行以下命令将其删除:

find . -maxdepth 1 -inum ${INODE_NUM} -delete
Run Code Online (Sandbox Code Playgroud)

您可能还会发现以下文章通常很有用:


the*_*ker 2

请务必仔细检查您的文件所在的分区;-)

事实证明,坏文件不在我的根分区上,而是在cifs挂载点上。要删除该文件,解决方案就像那里一样:

删除目标机器上的文件。那里的rm命令正常工作。