2

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?

  • 1
    Just now got 50 points so converting to comment: Possible duplicate of askubuntu.com/questions/233158/… – Android Dev Mar 14 '16 at 23:54
  • @AndroidDev To flag a post as a duplicate, you can use the "flag" link under the question: you'll be prompted for the link of the duplicate question and a system will post a comment in your behalf automatically. – kos Mar 14 '16 at 23:57
  • @AndroidDev No problem. You may still flag the question if you think it's a duplicate. I suggest you have a read here so that you can get an idea of how flagging works exactly: askubuntu.com/help/privileges/flag-posts. – kos Mar 15 '16 at 0:05
  • 2
    Just so you know, the -f flag tells it to suppress error messages in case of failure, and fail silently. So it's a good idea not to use that flag if you want more information about why something failed. – thomasrutter Mar 15 '16 at 1:13
  • This does appear to be a duplicate @android-dev, I was being too narrow with my search when I tried to find a solution for this. Thanks for the note about -f @thomasrutter – Noki Mar 15 '16 at 22:06
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.

  • Duh, how did I not think of that, Obvious really! Thanks, this worked for my problem. – Noki Mar 15 '16 at 22:01
3

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.

  • I did not try this as I just wanted the file gone and the other answers worked for me. But you covered some stuff I didn't know, thanks a lot! – Noki Mar 15 '16 at 22:03
2

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 =)

  • This worked, after I posted I found another file and this did not work for that. But good to know thanks! – Noki Mar 15 '16 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.