June 30, 2007
How do I rename a file that starts with a '-' in bash (linux)?
I accidentally created a file name -o on my linux server. How do I move it without mv thinking it is a parameter? I usually use bash as my shell.
- Thank you
- put it in quotes. as with most OS's it will interpet everything you put in quotes as one parameter this also works when you have spaces. this works on MSDOS as well.
mv "myfile-o" "myfile2"
- note that using quotes or escaping with \ does not work. Bash processes these characters then delivers the -o to mv
- I'm choosing DanK's answer because it does appear the only way to fix this is to write a program that renames it.
- Try putting the name in quotes. If that doesn't work, you might need to use an escape sequence for the -, like "\-o". If neither of those work, you might be able to delete it by doing "rm ?o"; of course, you'll lose the file this way, and you can't have any other files that are 2 letters long with the 2nd letter an o.
If you need to preserve the file, you can write a program to rename it. Since from within a program, it won't try to use the -o as a parameter, you can open a file with any name.
I'm surprised Unix even allowed you to create a file with a - in the name; I thought that was one of the restricted characters.
Tags: linux, linux for dummy























