5

from: http://seclists.org/fulldisclosure/2011/Sep/190

[USER@MACHINE ~] mkdir ejha
[USER@MACHINE ~] cd ejha/
[USER@MACHINE ~/ejha] touch ize
[USER@MACHINE ~/ejha] touch -- -f -i
[USER@MACHINE ~/ejha] ls -l
total 0
-rw-rw-r--. 1 USER USER 0 Sep 20 19:44 -f
-rw-rw-r--. 1 USER USER 0 Sep 20 19:44 -i
-rw-rw-r--. 1 USER USER 0 Sep 20 19:44 ize
[USER@MACHINE ~/ejha] rm *
rm: remove regular empty file `ize'? y
[USER@MACHINE ~/ejha] ls -l
total 0
-rw-rw-r--. 1 USER USER 0 Sep 20 19:44 -f
-rw-rw-r--. 1 USER USER 0 Sep 20 19:44 -i
[USER@MACHINE ~/ejha] 

infos about the system:

[USER@MACHINE ~/ejha] echo $SHELL
/bin/bash
[USER@MACHINE ~/ejha] lsb_release -a
LSB Version:    :core-4.0-ia32:core-4.0-noarch
Distributor ID: Fedora
Description:    Fedora release 14 (Laughlin)
Release:    14
Codename:   Laughlin
[USER@MACHINE ~/ejha] rpm -qa | fgrep bash
bash-4.1.7-4.fc14.i686
[USER@MACHINE ~/ejha] 

Why? Why doesn't it deletes the "-f" and "-i" file?

manatwork
  • 31,277
LanceBaynes
  • 40,135
  • 97
  • 255
  • 351

2 Answers2

18

It isn't deleting them because it recognises the filenames as arguments (unquoted, in this situation * expands to -f -i ize). To delete these files, either do rm -- *, or rm ./*. -- signifies the end of arguments, ./ uses the link to the current directory to circumvent rm's argument detection.

Generally ./* is preferable, as some programs do not accept -- to stop checking for arguments.

This is not a bug. This is something that should be handled by calling rm in the correct fashion to avoid such issues.

Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • "as some programs do not accept -- to stop checking for arguments" and other programs except a single dash for that purpose. – dmckee --- ex-moderator kitten Sep 20 '11 at 18:57
  • @dmckee some other programs do, not all. – Chris Down Sep 20 '11 at 19:21
  • 4
    It's not a bug, you're right. But I would call it a design flaw, as it shows the fragility of shells not distinguishing between things that should be taken literally (filenames) and things that should be interpreted (switches). The way most programming languages work (string literals are in quotes, everything else is interpreted) seems to be a much better design. Of course you can always use quotes, but Unix shells were not designed around the idea that we'd always use quotes. Quite the opposite. – iconoclast Jun 13 '12 at 03:19
4

Just to add a bit of clarity, the "*" is being expanded by your shell, not by 'rm', so the 'rm' command just gets the list of files as arguments. So in your example 'rm *' is exactly equivalent to typing 'rm -f -i ize'.

In other systems (e.g., the Windows/DOS command line) the "*" gets expanded to a list of files by the command itself, so the command 'knows' all matching files are files. The downside of that approach is that each command needs to implement the globbing and re-implement globbing configuration and quoting rules, etc.

P.T.
  • 1,635
  • 2
    "The downside of that approach is that each command needs to implement the globbing and re-implement globbing configuration and quoting rules, etc." well, it could be part of a library. It's really a historical artifact that UNIX didn't have the ability to share anything except the kernel, so it was better to have it in one process rather than in a library linked by each program. Whereas, in DOS, this was part of the "library/kernel blob" that was the DOS interrupt handlers. – Random832 Sep 27 '11 at 13:25