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
- Identify the exact filename. Run
ls -lain the directory. The-aflag shows hidden files (those starting with a dot), and-lgives details. Look for names that start with a dash. If the filename contains spaces or special characters, note them exactly. - 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, runcat ./-notes.txt. This works with most commands:less ./-notes.txt,nano ./-notes.txt,cp ./-notes.txt backup.txt. - 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.txtdeletes the file. This is especially useful for commands that don’t accept./(likefindwith certain predicates). - For
rmspecifically, be extra careful. The commandrm ./-file.txtis safe. Never runrm -file.txtbecausermmight interpret-file.txtas a set of options (e.g.,-ffor force,-ifor interactive,-lfor list, etc.) and could delete other files or fail. If you’re unsure, userm -- ./-file.txtto be doubly safe. - Use
findwith./or--. To find files starting with a dash, usefind . -maxdepth 1 -name '*-*'but that matches any dash. To match exactly a leading dash, usefind . -maxdepth 1 -name '-*'. To delete them, combine:find . -maxdepth 1 -name '-*' -exec rm -- {} \;. The--insideexecis not needed because{}is a path, but it doesn’t hurt. - 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. - If the file is in a different directory, use the full path. For example,
rm /home/user/-file.txtworks because the path doesn’t start with a dash. The./trick only works for files in the current directory. - For commands that don’t support
--(likeechoorprintf), use./or redirect. For example, to see the contents of a file named-data, usecat ./-data. To write to it, useecho "hello" > ./-data. - 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-fileis a valid expansion. Butrm *will try to remove all files, which is dangerous. Userm ./*to include the./prefix, but be careful as it will delete everything in the directory. Better to list first:ls ./*. - 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 -filewithout--or./. This is the most dangerous. The shell passes-fileas an option torm, 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, likeecho, don’t have a--option. Usingecho -- -filewill 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 orsubdir/-file(but that path starts with a letter, so it’s fine). - Using
findwithout-maxdepthand accidentally matching files in subdirectories. If you want to delete all files starting with a dash in the current directory only, use-maxdepth 1to avoid recursion. - Quoting incorrectly. If the filename contains spaces, you must quote it:
rm ./-my file.txtis wrong; userm ./-my\ file.txtorrm ./' -my file.txt'(with proper quoting).
Tips
- Always test with
lsfirst. Runls ./-*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?.txtmight 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
- How to Cool Down an Overheating Laptop
- Good Password: How to Create One
- Remove Write Protection on External Hard Drive
- How to Disable Chkdsk at Windows Startup
- Restore WiFi and Secure Your Netgear Router After Reset
- Fix Error 0x80004005: Top Easy Ways That Work