Score:1

How to delete directory/file named "-f", "-r" or other in terminal

pe flag

Just wondering how to delete directory/file named "-f", "-r" or other using terminal.

terdon avatar
cn flag
@user535733 it's not hard. You can trivially do it from a graphical file manager or, from the command line with `mkdir somedir/-f` or `touch somedir/-r` or `touch -- -r` etc.
Score:5
cn flag

This is literally in the manual

To remove a file whose name starts with a '-', for example '-foo', use one of these commands:

         rm -- -foo
         rm ./-foo
Score:4
in flag

The rm command supports a -- option, which tells it that any further arguments that begin with - should be taken literally and are not to be treated as option flags. eg:

rm -- -r

Many other Unix commands support something similar, and it's an out-of-the-box feature of the commonly-used getopt utility which is often used for command option parsing.

Note that quoting the file name, eg rm "-r" does not work (whereas quoting does help when you're trying to delete a file called, say, * (asterisk), eg rm "*"). That's because the wildcard expansion is being handled by the your Unix shell before your command is invoked, and quoting disables such expansion. But options are effectively normal arguments that the command program (eg rm) itself gets to decide what to do with - they aren't "special" to the shell, so quoting has no effect.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.