Can't delete a file with a '?' in the file name, it fails to delete with the message 'the file does not exist'.
I've tried through the terminal using
rm -f ./pathtofile\?.mkv
But despite no failure message the file still exists. Any suggestions?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
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.
? 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:
lists a single file you can safely run:
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.