Removing a file whose names starts with -
is a FAQ, but it looks like you've got a different problem.
Of the solutions you mentioned in the question:
rm -- -
That should work.
rm "-"
That's no different from rm -
. The quotes are stripped by the shell, and the rm
command just sees the single hyphen as an argument. Quotation marks are useful for escaping shell metacharacters; the hyphen is special to rm
, not to the shell.
rm "\-"
The quotation marks escape the backslash, so this attempts to remove a file whose name consists of two characters, a backslash and a hyphen.
rm \-
This is just like rm "-"
, equivalent to rm -
.
As it happens, at least with the GNU coreutils version of rm
, rm -
actually removes (or attempts to remove) a file whose name consists of just a hyphen. If your file had that name, it would be gone by now. I conclude that your file has a name that only looks like a hyphen.
ls | cat -A
should give an idea of what it's really called.
Here's a Perl script I just threw together that will prompt you for each file in the current directory and ask whether to remove it. Offered without warranty, but I've tested it briefly.
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
opendir my $DIR, '.' or die ".: $!\n";
my @files = sort grep { -f $_ } readdir $DIR;
closedir $DIR;
foreach my $file (@files) {
print "Remove $file? ";
my $answer = scalar <>;
if ($answer =~ /^[Yy]/) {
unlink $file or warn "$file: $!\n";
}
elsif ($answer =~ /^[Qq]/) {
exit 0;
}
}
EDIT: A less elaborate solution: if the file's name is just one character, then rm ?
should remove it (along with any other such files).
rm
version are you using (rm --version
)?rm -
works fine with GNU coreutils 8.5.rm ./-