Hyphen-prefixed file operation on your shell

In Unix/Linux shells, "-" (hyphen) is a special character that indicates the begining of an option. This causes a problem with file operation when the file name starts with hyphen. This page provides the operation tips for these files.

Custom Search

Operating files of which name start with "-" (hyphen)

For some file operation and editing commands, such as touch, rm, and vi, recognize the file names that start with hyphen as command line options. For example, when we want to create file '-foo' by touch command, we would do as follows, but the command fails.

$ touch '-foo'
touch: invalid option -- o
Try `touch --help' for more information.

This is because the argument '-foo' is parsed as command line options. To avoid this problem, we can use a technique to put an option terminator '--' for the command like:

$ touch -- '-foo'

You can use this approach for other commands, such as rm and vi.

Here we note that the option terminator '--' is required even if such hyphen-prefixed file name follows options.

$ rm -f -- '-foo'

succeeds and takes effect, but

$ rm -f '-foo'

fails. This command are recognized as the following command.

$ rm -f -f -o -o