-1

How do I delete a file named 1 '2 3' 4 5 in Linux? None of the methods I've used have worked.

Toby Speight
  • 8,678
Oluchi
  • 15

1 Answers1

9

You should "escape" both the spaces and the single quotes using \, so the command should be:

rm 1\ \'2\ 3\'\ 4\ 5

Or use double quotes:

rm "1 '2 3' 4 5"

In several shells, you can also use TAB completion (type 1 and then TAB to let the shell complete the rest of the file), and the shell will take care of using appropriate quoting / escaping.

  • Or just single quotes: '1 '\''2 3'\'' 4 5' – ceving Mar 08 '22 at 07:49
  • You can also try to use globbing patterns for problematic characters, if the result is still unique: '1 '?'2 3'?' 4 5' – ceving Mar 08 '22 at 07:54
  • 3
    If it is hard to predict uniqueness, a safe method is interactive mode: rm -i *1*2*3*4*5* should not produce too many matches. – Paul_Pedant Mar 08 '22 at 09:47
  • 1
    We don't need an exhaustive list of every conceivable way to generate the file name. The double quoted string is perfectly readable. – chepner Mar 08 '22 at 14:54