How to remove file with special characters? [duplicate]

不羁岁月 提交于 2020-01-23 10:56:52

问题


I have a weird file on a Unix filesystem. It seems to have some special characters in the file name, but I've not been able to remove it. Even if I don't write the name directly in the rm command (and I do ls | rm instead), I get an error that the file doesn't exist. Below some commands that I've tried after a few searches on the internet, in order to debug the issue.

Do you have any suggestions on how to remove it? The system is AIX 7.1. I've tried with rm and a perl script too (simply listing all files and deleting everything from the folder), and none worked. I can't move the folder to /tmp either, I get the same error.

Thanks!

[root@server] ls -1b | od -bc
0000000  342 134 062 060 060 134 062 062 063 012
           ▒   \   2   0   0   \   2   2   3  \n
0000012
[root@server]$ ls -li
ls: 0653-341 The file ./– does not exist.
total 0
[root@server]$ ls
–
[root@server]$ ls | od -bc
0000000  342 200 223 012
           ▒ 200 223  \n
0000004
[root@server]$ rm *
rm: –: A file or directory in the path name does not exist.

Screenshot


回答1:


A relatively safe way would be to list files' inodes with ls -i and then delete the one you need with find . -maxdepth 1 -type f -inum $inum -delete ($inum is the inode to delete).

And be grateful you are on Unix! ❤




回答2:


Your file name contains a en-dash ; that is not the hyphen-minus sign - (unicode U+002D HYPHEN MINUS, also in ASCII), but U+2013 EN DASH in Unicode. Maybe your file name contains other weird characters (then you need to find which ones).

Try first to list that file, without globbing, using the ls(1) command (read about path_resolution(7)...). Consider also using strace(1) on that ls command. Try also echo * | od -cxi to understand what globbing is done. This means some ls ./– (or something similar). I am supposing it works (perhaps some quoting is needed). Find exactly how to have ls ./– | od -cxi work correctly. Then use ls -l instead of just ls to be sure (sometimes, ls might be aliased, so you need to disable that, perhaps by typing /bin/ls instead of ls)

You could remove it by copy/pasting that weird name, or, after changing appropriately your working directory with cd, perhaps with

rm ./–

the last character of that command (copy/pasted) is the Unicode U+2013 EN DASH; How to enter exactly that en-dash Unicode character is a different story (it could depend upon your desktop environment, your terminal emulator, your window manager, your keyboard layout, your localization). On Linux desktops, the charmap GUI utility (from Debian or Ubuntu package gucharmap) could help. You can select that character with your mouse (amongst thousands of other unicode characters) and copy/paste it in UTF-8.

If your directory has a few entries, you might try

  rm -rif .

and the /bin/rm command will prompt you for every entry (or try just rm -if * if you don't want to descend in sub-directories).

If the naughty file is the only one in its directory (excluding of course the . and .. entries; check with ls -al), you could rm -rf that directory and recreate that directory with mkdir

If your shell is nicely configured to glob on Unicode characters (not just on bytes or ASCII) you might try rm -i ./?

If nothing else works, you could even write a small C program calling remove(3) or rename(2) (and checking that it has succeeded) on "\342\200\223" (the UTF-8 encoding of the en-dash). If you need to scan programmatically your weird directory (to find the exact weird name), consider nftw(3), or opendir(3), loop on readdir(3) with stat(2) etc... Or code that in a scripting language such as Python. And you could use some GUI file manager doing the same.

In 2018, we have UTF-8 everywhere. If by mistake your desktop and localization is not using UTF-8, you need to correct that. See locale(7).

You might also use the printf(1) command to get that weird character (that printf command is in POSIX). So the command rm -v $(printf "\342\200\223") should work (on AIX, rm might not be the GNU one, so you cannot use rm -v only rm)



来源:https://stackoverflow.com/questions/52438836/how-to-remove-file-with-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!