0

I create a file name :~ on bash shell by accident

scp foo :~

How to delete it?

Tony
  • 117

2 Answers2

2

try rm ':?' or rm ":?" or rm :\?

mainly you need to quote the file name either with single quotes or double quotes. Or escape any special characters contains in files name.

some special characters I can count are:

*
|
$
&
#
<>
;
space/tab/newline
\
brackets/parenthesis/braces
'"`
?
etc

I suggest to use single quotes to prevent deleting a wrong file named my_file instead of $file with rm $file or rm "$file" when file='my_file'. The single quote character is the only character that you can't quote with single quotes in Bourne-like shells, but you can always quote the rest of the file name with '...' and ' itself as \':

rm -- '-$#~<>'\''"*?[]{}'`

Note that - is not special to the shell, but it's special to rm in that if found at the start of an argument, it's treated as an option. Here the -- tells rm that the arguments that follow are not to be treated as options even if they start with a -.

αғsнιη
  • 41,407
-1

In general, whenever you want to remove a weirdly-named file, you can try to use the marker -- which denotes the end of options. Anything after that flag is handled as a positional parameter. (See also: What does "--" (double-dash) mean? )

So, that would be

rm -i -- :?

(As suggested by @Doug O'Neal, the -i option prompts you before deletion to make sure you're removing the correct file.)

However, in this case ? will be expanded by the shell as a glob character. So the above won't work.

In this case, you can use the autocomplete feature of the shell. Go into the directory where the weirdly-named file is, then type rm : (without pressing Enter) and then press Tab to cycle through all files in the current directory. The weirdly-named file will appear properly quoted.

dr_
  • 29,602
  • 2
    -- is a marker for rm, not Bash. And it doesn't do anything to avoid the ? being treated as a glob character. – ilkkachu May 29 '18 at 13:27