2

I have a problem when trying removing a file with name something like -h_some_file_name, this is because of generated script that I made and forget to check the prefix.

If I run the command rm '-h_some_file_name' it return an error rm: invalid option -- 'h'.

If I try to change the name and then remove it using mv '-h_some_file_name' new_filename it return mv: invalid option -- 'h'

Question: How to remove a file with name like -h_some_file_name in shell? if using gui I can right click and move to trash, but in shell only returns an error

1

2 Answers 2

2

Just:

rm -- -h_some_file_name

Or:

rm ./-h_some_file_name

See the manpage of rm:

   To remove a file whose name starts with a `-', for example `-foo', use one of these
   commands:

          rm -- -foo

          rm ./-foo

The -- argument tells rm that all following argument should not be treated as parameters. A variety of other Linux/Unix command line tools support that argument. This interpretation of -- follows the POSIX argument parsing conventions1). The mv utility also supports it:

mv -- -h_some_file_name new_file_name

1) The Open Group Base Specifications Issue 7, IEEE Std 1003.1, Guideline 10

0
0

You can use wildcards. Here in this case you can use '*' wildcard.

Go to the directory where files of this type is to be deleted or you need to mention the complete path in the command.

After setting to the terminal prompt to your directory, type in the following

rm ./-h*

For little bit more information you can see this link: http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm

For indepth information follow some book related to bash.

Not the answer you're looking for? Browse other questions tagged .