Can't delete a file with a '?' in the file name
Solution 1:
Does the file really have a ?
in the filename, or is it a non-printing character that ls
shows as a ?
?
$ touch $'a\ab' 'a?b'
$ ls
a?b a?b
One file has an ASCII BELL character in the name, and the other has a plain old question mark.
Newer versions of ls
can show it special characters in a clearer form by default:
$ touch $'b\aa'
$ ls
'b'$'\a''a'
ls -q
is how older versions of ls
show non-printing characters by default. So, if you just do ls
in any current version of Ubuntu, you're likely to see just question marks.
Try, instead, one of:
$ ls -b
a?b a\ab
$ printf "%q\n" *
a\?b
$'a\ab'
If the output from either of these don't have question marks, then the filename doesn't have question marks.
You can use the output of printf
for deleting:
rm a\?b
rm $'a\ab'
Or rely on tab completion:
$ rm a<tab>
a?b a^Gb
If it shows ^G
, then press CtrlV then CtrlG to enter it. Or tell bash to cycle through tab completions:
$ bind tab:menu-complete
$ rm a<tab>
$ rm a\?b<tab>
$ rm a^Gb
In either case, using rm a?b
could work, but is dangerous. It would match all filenames starting with a
, ending in b
and having one character in between:
$ touch acb; printf "%q\n" a?b
a\?b
$'a\ab'
acb
So, if you do rm a?b
(or worse, rm a*b
), you could end up deleting files you didn't intend to.
Solution 2:
The ?
is most likely another non-ASCII symbol that your terminal program is unable to display so it displays ?
. This is easily proven - you can execute touch ?.mkv
and rm ?.mkv
- both command execute just fine.
Files like that are easily deleted using a GUI file manager.
Alternatively you could try using wildcards. If command:
ls pathtofile\FewLetters*.mkv
lists a single file you can safely run:
rm pathtofile\FewLetters*.mkv`.
Finally you could try the harder but surer way as described in Can not delete files containing special characters in the file name as pointed by Android Dev above.
Solution 3:
rm -f 'path?.mkv'
works for me. Correct me if I'm wrong, but the ' '
does disable the functionality of some special characters such as ?
. Sorry for the bad formatting, rplying in speed gotta hurry.
Hope it helps, have a nice day =)