Deleting files with rm and getting
rm: invalid option –Try ‘rm –help’ for more information.
In Linux, trying to delete a file its name starts with dash(‘-‘) or double dashes(‘–‘) will fails, as dash or ‘-‘ is interpreted as an option by the rm and most linux commands.
Here are two files with filenames starting with ‘-‘ and ‘–‘ and the typical rm command deletion attempt fails with an error –
1 2 3 4 5 6 7 8 9 10 11 12 13 | $ ls -1 -tempfile1 --tempfile2 $ rm -tempfile1 rm: invalid option -- 't' Try 'rm ./-tempfile1' to remove the file '-tempfile1'. Try 'rm --help' for more information. $ rm --tempfile2 rm: unrecognized option '--tempfile2' Try 'rm ./--tempfile2' to remove the file '--tempfile2'. Try 'rm --help' for more information. |
There are several ways of addressing this – you can precede the file name with ./ OR pass double dash after rm to end all option processing.
1 2 3 4 5 6 7 8 | $ ls -tempfile1 --tempfile2 $ rm -- -tempfile1 $ mv -- --tempfile2 tempfile2 $ rm ./-tempfile1 |
References –