System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Removing files containing "-" character at the beginning of its filename

SOLVED
Go to solution
Danny Fang
Frequent Advisor

Removing files containing "-" character at the beginning of its filename

Hi,
I'm attempting to remove files which contain the "-" at the start of its filename, which however failed (see output below).

danny@dimebag ~/nokia-bss-input $ rm -rf "-detail.cs"
rm: invalid option -- e
Try `rm --help' for more information.
danny@dimebag ~/nokia-bss-input $ ls -l
total 574
-rw-r--r-- 1 danny danny 39071 Sep 4 19:07 -detail.csv
-rw-r--r-- 1 danny danny 1340 Sep 4 19:07 -summary.csv


danny@dimebag ~/nokia-bss-input $ rm -i "-summary.csv"
rm: invalid option -- s
Try `rm ./-summary.csv' to remove the file `-summary.csv'.
Try `rm --help' for more information.

May I know what other methods that I could use to remove files with such names?

Thanks
Danny
7 REPLIES
labadie_1
Honored Contributor
Solution

Re: Removing files containing "-" character at the beginning of its filename

Specify --, for example

$ rm -i -- "-1.tada"


to remove the file "-1.tada"
Matti_Kurkela
Honored Contributor

Re: Removing files containing "-" character at the beginning of its filename

Time to use "--", the POSIX standard "end-of-options" marker:

rm -- -summary.csv

This works with all programs that use a POSIX-compliant getopt() function to parse their options.

(e.g. the getopt() function included in the glibc used in almost all Linux distributions)

MK
MK
James R. Ferguson
Acclaimed Contributor

Re: Removing files containing "-" character at the beginning of its filename

Hi Danny:

Yet another way to handle non-standard file names (those containing special characters; non-printing ones; file names with whitespace; file names with hyphens as their first character) is to find their inode number and then use that to remove them. This works thusly:

# cd /tmp
# touch -- -badfile
# ls -il -- -badfile
7151 -rw-r--r-- 1 root sys 0 Oct 7 18:54 -badfile
#

Note that the inode number of the file is 7151. Inodes are *only* unique within a filesystem, so we use '-xdev' in the 'find' below to *not* cross mountpoints.

# find /tmp -xdev -inum 7151 -exec rm {} \;

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Removing files containing "-" character at the beginning of its filename

>rm -rf "-detail.cs"

Quoting doesn't help since they are removed by the shell before being passed to rm.
Besides the Standard "--", you can use "rm -i" with a wildcard or JRF's find.
But you can simply use an absolute or relative path:
rm -rf ./-detail.cs
Sandeep_Chaudhary
Trusted Contributor

Re: Removing files containing "-" character at the beginning of its filename

no this is directly from man page of rm

Remove a file in the current directory whose name starts with - or *
or some other character that is special to the shell:

rm ./-filename
rm \*filename


and this is the output

unix # rm ./-detail.cs
sandchau as root@unix [/tmp]
#
kelvinlnx
Advisor

Re: Removing files containing "-" character at the beginning of its filename

Just use the correct quoting technique. " does not ignore everything. The shell will still interprete the special characters.
You want to use the single quote '

so your command would be...

rm -rf '-detail.cs' '-summary.csv'
Dennis Handly
Acclaimed Contributor

Re: Removing files containing "-" character at the beginning of its filename

>kelvinlnx: Just use the correct quoting technique.

This has nothing to do with quoting and the shell. "-" is not special to the shell but special to each command.
It helps if you read the other replies.