The GNU coreutils version of ls
(which is what you're likely to see on Linux) has the --quoting-style
option, which controls how it shows names with unprintable or otherwise funny characters. If set to shell-escape
, it uses the various shell quotes to make the output more readable and valid as input to a shell. That output can be copypasted to the shell command line as an argument to rm
.
Though note that it uses $'...'
quotes to make special characters more readable, and while most shells support it by now, not all might. In particular, Dash, the shell used as /bin/sh
on Debian and Ubuntu, doesn't seem to support it. Bash, ksh and zsh definitely do, though.
Here, the second file is your '--'
, quoted with double quotes, and the other one is an abomination with characters like Ctrl-A, tab, newline, a single quote and a trailing space. There it uses $'..'
and backslash-escapes.
$ ls --quoting-style=shell-escape -1
''$'\001\t'\''foo'$'\n''bar '
"'--'"
(ls
doesn't seem to try to minimize the output much, the first could be printed as just $'\001\t\'foo\nbar '
)
Either or both can be removed by using that output:
$ rm -- ''$'\001\t'\''foo'$'\n''bar '
$ rm -- "'--'"
Note that you still need to use --
if the filename starts with a dash, even if ls
shows that dash quoted.
I think shell-escape
is also the default quoting style used by recent-ish versions of ls
(at least the output looks like it), so you don't even actually need to spell out the long option.