How do I delete a file named 1 '2 3' 4 5
in Linux? None of the methods I've used have worked.
Asked
Active
Viewed 1,122 times
-1
Toby Speight
- 8,678
Oluchi
- 15
1 Answers
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
- 544,893
-
-
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 -
3If 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 -
1We 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
rm 1and pressing TAB? The tab completion is supposed to do all quoting for you, if the beginning of the file is unique. Usingzsh, you can even tab-sycle through all possibilities. – Philippos Mar 08 '22 at 12:14single quotewhen using single quote to wrap special characters in shell?, What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?, How to escape quotes in shell? – ilkkachu Mar 08 '22 at 15:47