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

Moderator: cah

Post Reply
cah
General of the Army / Fleet Admiral / General of the Air Force
General of the Army / Fleet Admiral / General of the Air Force
Posts: 1342
Joined: Sun Aug 17, 2008 5:05 am

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

Post 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
CAH, The Great
Post Reply