-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,460
  • 3
  • 26
  • 50
Oluchi
  • 15
  • 1
  • 1
    Have you tried writing `rm 1` and pressing TAB? The tab completion is supposed to do all quoting for you, if the beginning of the file is unique. Using `zsh`, you can even tab-sycle through all possibilities. – Philippos Mar 08 '22 at 12:14
  • 5
    If you could demonstrate some of those methods that didn't work, perhaps the answers here could teach you more about the *why*, and prevent future problems! – Jeff Schaller Mar 08 '22 at 14:01
  • See https://unix.stackexchange.com/questions/208140/deleting-files-with-spaces-in-their-names – Andy Lester Mar 08 '22 at 15:04
  • "None of the methods I've used have worked." Show us what those methods were, and we can help solve the problem. – Andy Lester Mar 08 '22 at 15:05
  • [How to echo `single quote` when using single quote to wrap special characters in shell?](https://unix.stackexchange.com/q/187651/170373), [What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?](https://unix.stackexchange.com/q/503013/170373), [How to escape quotes in shell?](https://unix.stackexchange.com/q/30903/170373) – ilkkachu Mar 08 '22 at 15:47

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.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • 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