sodcalculator

10 Tech How-To

Filenames Starting With a Dash in Linux: How to Handle

Guide 10-31407 · Updated · Step by step

Files whose names begin with a dash (e.g. -file.txt) are common after a botched download or a typo. The dash makes the shell interpret the name as an option, so commands like rm -file.txt fail or behave unexpectedly. This guide shows you the safe, standard ways to list, view, edit, copy, move, and delete such files, including the -- end-of-options marker and the ./ prefix trick.

What You’ll Need

  • A Linux terminal (any shell, but Bash is assumed)
  • Basic familiarity with the command line (cd, ls, rm)
  • Permission to modify the files in question (you own them or have sudo)

Step-by-Step

  1. Identify the exact filename. Run ls -la in the directory. The -a flag shows hidden files (those starting with a dot), and -l gives details. Look for names that start with a dash. If the filename contains spaces or special characters, note them exactly.
  2. Use the ./ prefix to reference the file. The simplest way to tell the shell “this is a filename, not an option” is to prepend ./ (current directory). For example, to view a file named -notes.txt, run cat ./-notes.txt. This works with most commands: less ./-notes.txt, nano ./-notes.txt, cp ./-notes.txt backup.txt.
  3. Use the -- end-of-options marker. Many GNU utilities accept -- to signal that everything after it is a filename, not an option. For example, rm -- -file.txt deletes the file. This is especially useful for commands that don’t accept ./ (like find with certain predicates).
  4. For rm specifically, be extra careful. The command rm ./-file.txt is safe. Never run rm -file.txt because rm might interpret -file.txt as a set of options (e.g., -f for force, -i for interactive, -l for list, etc.) and could delete other files or fail. If you’re unsure, use rm -- ./-file.txt to be doubly safe.
  5. Use find with ./ or --. To find files starting with a dash, use find . -maxdepth 1 -name '*-*' but that matches any dash. To match exactly a leading dash, use find . -maxdepth 1 -name '-*'. To delete them, combine: find . -maxdepth 1 -name '-*' -exec rm -- {} \;. The -- inside exec is not needed because {} is a path, but it doesn’t hurt.
  6. Rename the file to something safer. Often the best fix is to rename it to a normal name. Use mv ./-file.txt normal.txt. After renaming, you can use the file normally.
  7. If the file is in a different directory, use the full path. For example, rm /home/user/-file.txt works because the path doesn’t start with a dash. The ./ trick only works for files in the current directory.
  8. For commands that don’t support -- (like echo or printf), use ./ or redirect. For example, to see the contents of a file named -data, use cat ./-data. To write to it, use echo "hello" > ./-data.
  9. When using wildcards, be aware of the dash. A pattern like rm * will not match files starting with a dash because the shell expands * to all non-hidden files, and -file is a valid expansion. But rm * will try to remove all files, which is dangerous. Use rm ./* to include the ./ prefix, but be careful as it will delete everything in the directory. Better to list first: ls ./*.
  10. For batch operations, use a loop with ./. For example, to rename all files starting with a dash to have a prefix “safe_”: for f in ./-*; do mv "$f" "safe_${f#./-}"; done. This loops over each file, strips the ./- prefix, and adds “safe_”.

Common Mistakes

  • Running rm -file without -- or ./. This is the most dangerous. The shell passes -file as an option to rm, which may interpret -f (force) and -i (interactive) and -l (list) etc. It could delete other files or nothing at all.
  • Using -- with commands that don’t support it. Some commands, like echo, don’t have a -- option. Using echo -- -file will just print “– -file”. Use ./ instead.
  • Forgetting that ./ only works for the current directory. If the file is in a subdirectory, you need the full path or subdir/-file (but that path starts with a letter, so it’s fine).
  • Using find without -maxdepth and accidentally matching files in subdirectories. If you want to delete all files starting with a dash in the current directory only, use -maxdepth 1 to avoid recursion.
  • Quoting incorrectly. If the filename contains spaces, you must quote it: rm ./-my file.txt is wrong; use rm ./-my\ file.txt or rm ./' -my file.txt' (with proper quoting).

Tips

  • Always test with ls first. Run ls ./-* to see exactly what files match before deleting or moving.
  • Use tab completion. Type cat ./- and press Tab. The shell will complete the filename, adding proper escaping automatically.
  • Consider renaming the file immediately. Once you’ve accessed it, rename it to a normal name to avoid future headaches.
  • For files that start with a dash and contain wildcard characters, quote them. For example, rm ./-file?.txt might expand the ?. Use single quotes: rm ./-file?.txt (the single quotes prevent expansion).
  • Use -- as a habit. Many GNU tools support it, and it’s a good practice to use it when you have filenames that start with a dash.

Troubleshooting

Command says “invalid option” even with ./. This can happen if the command itself doesn’t accept ./ as a prefix (rare). For example, cd ./-dir works, but some commands might not. Try using the full path: cd /home/user/-dir.

rm -- -file says “No such file or directory”. Check if the file actually exists. Use ls -la to see the exact name. Sometimes the file is named -file but with a leading space? Use ls -lb to show non-printing characters.

Wildcard .* doesn’t match. The pattern .* matches hidden files (starting with dot), not files starting with dash. To match files starting with dash, use -* but be careful: ls -* might be interpreted as options. Use ls ./-*.

When using find, the -- doesn’t work. find doesn’t use -- to separate options. Instead, use find . -name '-*' and the ./ prefix is implied. To delete, use -exec rm {} \; – the {} is a path, so it’s safe.

File is in a directory that also starts with a dash. For example, ./-dir/-file. The ./ prefix works for the first component, but the second component also starts with a dash. Use the full path: /home/user/-dir/-file.

Frequently Asked Questions

Q: Why does rm -file.txt not work?
A: The shell passes -file.txt as an option to rm. rm interprets the leading dash as option flags (like -f, -i, etc.), so it doesn’t see a filename. Use rm -- -file.txt or rm ./-file.txt.

Q: Can I use -- with any command?
A: No, only commands that explicitly support it. Most GNU coreutils (like rm, cp, mv, ls) do, but some commands like echo or printf don’t. For those, use ./.

Q: How do I delete a file named -rf?
A: This is dangerous because rm -rf is a recursive force delete. To delete a file literally named -rf, use rm -- -rf or rm ./-rf. Be absolutely sure you’re in the right directory.

Q: What if the filename starts with a dash and has spaces?
A: Quote the filename after the ./: rm ./-my file.txt (with quotes) or escape the space: rm ./-my\ file.txt. Tab completion will handle this for you.

Q: Is there a way to avoid creating such files in the first place?
A: Yes, be careful with commands like wget or curl that might save files with a leading dash if you mistype a URL. Also, when using touch or nano, avoid starting filenames with a dash. If you must, use touch ./-file.

Q: Can I use find to rename all such files at once?
A: Yes. For example, to prefix all files starting with a dash in the current directory with “safe_”: find . -maxdepth 1 -name '-*' -exec bash -c 'mv "$1" "safe_${1#./-}"' _ {} \;. This is advanced, but works.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →