如何在 Mac OS X 终端中删除带有不可打印字符的文件

Per*_*nce 7 terminal rm macos

可能的重复:
在 OS X 中删除文件名中包含奇怪字符的文件

我有一个文件夹,其中包含一个名为 Icons 的文件,并以附加的不可打印字符结尾。如果我使用 -B 选项,我可以看到无法打印的内容:

$ ls -B
$ Icon\015
Run Code Online (Sandbox Code Playgroud)

如果我使用 ls -b 我得到:

$ ls -b
$ Icon\r
Run Code Online (Sandbox Code Playgroud)

我想删除文件,但找不到输入不可打印字符的方法。如果我做:

$ rm Icon\015
Run Code Online (Sandbox Code Playgroud)

我得到:

$ Icon\015: No such file or directory
Run Code Online (Sandbox Code Playgroud)

如果我做:

$ rm Icon\r
Run Code Online (Sandbox Code Playgroud)

我得到:

$ Icon\r: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我可以删除整个文件夹,但我需要一种更通用的方法来执行此操作,因为此类文件会在许多用户文件夹中弹出。

Den*_*nis 3

shell 会自动将每个 CR ( \r) 转换为 LF ( \n) 并执行前面的命令。但是,您可以使用echo来生成 LF 字符。所有这些都应该有效:

rm $(echo -e "Icon\r")

rm $(echo -e "Icon\015")

echo -e "Icon\r" | xargs rm

echo -en "Icon\r" | xargs -0 rm
Run Code Online (Sandbox Code Playgroud)

最后一个选项应该相当强大,可以处理所有可能的奇怪字符。