Unix tools: what if a file is named minus something?
-
I'm always wondering: most GNU/Unix tools take options in the form "minus something", sometimes followed by an argument. What if you got a file named minus something? $ ls -f $ rm -f $ ls -f $ mv -f abc mv: missing destination file operand after `abc' Try `mv --help' for more information. $ cat -f cat: invalid option -- 'f' Try `cat --help' for more information. or $ ls -ohello.c $ gcc -ohello -ohello.c gcc: fatal error: no input files compilation terminated. This is just out of curiosity; I don't have a use case for this.
-
Answer:
To remove a file named -x, use rm -- -x (-- means end of options) or rm ./-x.
eepp at Super User Visit the source
Other answers
It is fairly common to ask this type of question in interview settings. A common way to handle files with dashes is either: $ rm -- -f $ rm ./-f
William Pursell
you have to use rm -- <filename> Ex: rm -- -f
sat
A common question in Unix. The main way is to give the full path name to the file, so it doesn't have a dash in front of it: $ rm -file.txt unknown option -l $ rm ./-file.txt #No problem! $ rm $PWD/-file.txt #Same thing Some commands, you can use a dash by itself (or a double dash) to end the options. However, this is not necessarily true with all commands, or even the same command on different systems. $ rm -- -file.txt #Works on Linux but not on some Unix systems
David W.
Related Q & A:
- What is a C header file?Best solution by Yahoo! Answers
- What is the maximum size of a file we can send from Yahoo?Best solution by Yahoo! Answers
- What are the best tools to analyze a competitor's website?Best solution by Quora
- What does a Unix System Administrator do?Best solution by eHow old
- What do I do if I purchased something from a fake website?Best solution by babyandbump.momtastic.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.