Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use -- to tell a command that a dash-leading filename is an operand, not an option: rm -- -name. For a file in the current directory, a path prefix works too: rm ./-name. These approaches also work with GNU cp and mv when applied to the source filename.

Why a filename beginning with a dash causes trouble

Many command-line programs interpret an argument beginning with - as an option. If a file is literally named -name, a command may interpret it as an option instead of the file you meant to act on. GNU Coreutils documents that -- ends option parsing: “Later arguments, if any, are treated as operands even if they begin with ‘-’.”

Remove, copy, or move one dash-leading file

Remove a file

From the directory containing the file, either of these GNU rm forms identifies the file named -name:

  • rm -- -name
  • rm ./-name

The first uses the end-of-options marker. The second supplies a relative path beginning with ./, so the argument no longer begins with a dash. The Linux man-pages rm(1) manual documents both forms.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Copy or move a file

For GNU Coreutils, put -- before the source operand, or use a path prefix:

  • cp -- -name destination or cp ./-name destination
  • mv -- -name destination or mv ./-name destination

Here, destination is the destination path you intend. For mv, if you need the final operand treated as the exact destination rather than as a directory into which to move the file, GNU mv provides -T (also called --no-target-directory).

What quoting does—and does not do

Quotes protect text from shell operations such as word splitting and expansion; they do not tell the program to stop parsing options. For example, rm -- '-name' both quotes the filename and ends option parsing. If shell characters in a name need protection, quote the name and use --, or pass a path such as ./-name.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle multiple filenames safely

For a one-off operation on visible entries in the current directory, rm -- * passes the shell-expanded names after the option delimiter, so dash-leading names are not treated as options. The glob * does not match hidden names. Before running a destructive command, inspect which names the glob expands to and confirm the target set.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For scripts or batches that may include spaces, newlines, or other unusual characters, do not build a filename list from newline-separated ls output. Newlines can be part of a filename, so that representation cannot safely distinguish every name. GNU documentation recommends NUL-delimited handling, for example:

find . -type f -print0 | xargs -0 command

Replace command with the operation you intend to run, using its appropriate options and operands. The GNU Coreutils documentation on NUL-safe filename handling describes pairing find -print0 with xargs -0. The Linux man-pages find(1) manual also documents find‘s argument and filename handling.

Check portability and the target before acting

  • -- is documented for GNU Coreutils, but do not assume every utility on every Unix-like system accepts it in the same way. Check that command’s manual when portability matters.
  • A path prefix such as ./ is convenient for a name in the current directory; use the correct directory path if the file is elsewhere.
  • Before deletion or batch operations, verify the path and inspect the expansion or file list so the command acts on the intended files.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.