Page 1 of 1

UNIX: Remove A File With A Name Starting With – (dash) Character

Posted: Tue Apr 26, 2016 1:36 pm
by cah
Sometimes, filename start with a dash/hyphen (-) appear in UNIX file systems.

Code: Select all

-rw-r--r--   1 www      www         1021 Apr 26 13:17 -20160426131707.csr
Regular 'rm' cannot remove the files.
E.g.

Code: Select all

# rm -f -20160426131707.key
rm: illegal option -- 2
rm: illegal option -- 0
rm: illegal option -- 1
rm: illegal option -- 6
rm: illegal option -- 0
rm: illegal option -- 4
rm: illegal option -- 2
rm: illegal option -- 6
rm: illegal option -- 1
rm: illegal option -- 3
rm: illegal option -- 1
rm: illegal option -- 7
rm: illegal option -- 0
rm: illegal option -- 7
rm: illegal option -- .
rm: illegal option -- k
rm: illegal option -- e
rm: illegal option -- y
usage: rm [-fiRr] file ...
There are 2 ways to remove them.

1. Remove from current directory

Code: Select all

# rm ./-20160426131707.key
2. Use "--" in front

Code: Select all

# rm -- -20160426131707.key