sshukla has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Normally in Unix one can preced with \ for any special charactors, however it doesn't work here is there any way to do delete same using PERL? Thanks in advance. Saumil

Replies are listed 'Best First'.
Re: deleting directory named -I (as Ice).
by particle (Vicar) on May 21, 2002 at 21:13 UTC
    unlink. but you should be able to do in in the shell, by /usr/bin/rmdir -- -I or something.

    Update: oops, merlyn's right. but you can delete from the shell (it would be a pretty bad shell if you couldn't.)

    Update: after a quick read of the docs, it turns out merlyn's right in spirit, but not in fact. unlink can be used to delete directories, but it probably shouldn't. use rmdir, as both merlyn and the doc point out.

    ~Particle *accelerates*

        from the docs...

        unlink LIST
        unlink Deletes a list of files. Returns the number of files successfully deleted.

        $cnt = unlink 'a', 'b', 'c';
        unlink @goners;
        unlink <*.bak>;

        "unlink" will not delete directories unless
        you are superuser and the -U flag is supplied to
        Perl. Even if these conditions are met, be warned
        that unlinking a directory can inflict damage on
        your filesystem. Use "rmdir" instead.

        If LIST is omitted, uses "$_".
Re: deleting directory named -I (as Ice).
by jeffenstein (Hermit) on May 22, 2002 at 05:45 UTC

    This is really more a unix question than a perl question, but... rm ./-I will delete the file. So long as the first character is not a dash, then it works fine. Quotes are stripped by the shell, so they don't count as characters.

    Update: D'oh! As pepik_knize pointed out below, it's a directory, not a file. rmdir ./-I is what I should have said.

      but... rm ./-I will delete the file

      Almost. It's a directory, so you would need rm -R to get rid of it (and everything in it).

      Pepik


      Of all the causes that conspire to blind
      Man's erring judgment, and misguide the mind,
      What the weak head with strongest bias rules,
      Is pride, the never-failing vice of fools.
      -- Pope.
      (Humble to be American.)