Delete File – How to Delete a File with Corrupt Filename

deletefiles

Somehow a program created a file with a broken filename which cannot be deleted anymore. Any attempt to delete the file results in "No such file or directory" as if the file isn't there.

The problem seems to be a control character ASCII 2 in the filename.

$ 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

I tried to run fsck after reboot but the file is still there.

$ 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
...

No indication there was a problem. ("sauber" = clean)

I even tried to wrote my own deletion program which failed as well as the rm command:

$ 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

I found similar questions the answers there don't work for me:

Other information:

$ 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

Is there a way to get rid of this file?

Best Answer

There are a bunch of options for deleting files with non-ascii filenames.

I was able to create and delete a file with the filename under discussion by using ANSI C quoting:

# 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'

Take a look at this post:

Here's a command taken from that post that should delete all files in the current directory whose names contain non-ascii characters:

LC_ALL=C find . -maxdepth 0 -name '*[! -~]*' -delete

You can modify the glob pattern or use a regular expression in order to narrow down the matches.

Here's another relevant post:

There's a suggestion there to try deleting by inode. First run ls -lib to find the inode of the offending file, and then run the following command to delete it:

find . -maxdepth 1 -inum ${INODE_NUM} -delete

You might also find the following article to be generally useful:

Related Question