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

Hello Monks,

Inadvertently, I created a file named: 'popdatck . $dept' and now I have a problem to remove it.
The suggestions to put this name in quotes or to place a backslash before the special characters don't help, neither with the 'rm'(delete)command nor(attempting to first change the filename) with the 'mv' command.

That is,
'rm popdatck . $dept'
or 'rm "popdatck . $dept"'
or 'rm "popdatck \. \$dept"'
or even "popdatck$dept"
gives me:
'dept: Undefined variable.'

and,
'rm popdatck \. \$dept'
gives me:
rm: cannot remove `popdatck': No such file or directory
rm: cannot remove `.' or `..'
rm: cannot remove `$dept': No such file or directory

Any more suggestions?
lev

Replies are listed 'Best First'.
Re: Deleting an Odd Filename
by shmem (Chancellor) on Jun 01, 2009 at 10:34 UTC
Re: Deleting an Odd Filename
by jethro (Monsignor) on Jun 01, 2009 at 10:07 UTC
    it seems you also have spaces in the filemname. the spaces must be escaped too. And you don't need to escape the '.'

    One way to do it automatically right is to write 'rm popda' on the command line, then use the tab key for command completion. Normally the shell will write the rest fo the filename for you correctly escaped.

    Another way might be to use a graphical file manager (not sure if this will work)

    Or just use

    rm popdatck\ .\ \$dept
Re: Deleting an Odd Filename (avoid typing name)
by ikegami (Patriarch) on Jun 01, 2009 at 14:41 UTC

    Use name completion.

    $ rm popd<tab>

    Or wildcards

    $ rm *popd*

    Or filtered directory listings

    $ find -maxdepth 1 -name "*popd*" -delete
    $ perl -e'opendir($dh,".") or die $!; for (readdir($dh)) { unlink $_ i +f /popd/ }'
Re: Deleting an Odd Filename
by Bloodnok (Vicar) on Jun 01, 2009 at 10:06 UTC
    I'm inferring (from the examples) you're attempting to use the system built-in.

    As it's just a one-off, why not drop into the shell (it _is_ allowed ;-) and since I'll bet you've only one file whose name starts with the string popdatck, try rm popdtack*

    Either way, AFAICT, (one of the) the problems with both attempts is that there is a space you appear to have omitted to escape e.g.

    'rm popdatck \. \$dept'
    should read
    'rm popdatck\ \. \$dept'
    ...or, you could just let the shell do the work and use tab completion...

    Update

    Added tab completion - thanx to jethro for the singularly timely reminder

    A user level that continues to overstate my experience :-))
      Or you can quote rm 'popdatck . $dept'

        That would be the most natural solution, IMHO (when using the shell).  Like Perl, most Unix shells interpolate variables ($dept) in double-quoted strings, while leaving them alone when single quotes are being used.

        The only case I'm aware of where this doesn't work (except when having single quotes in the name itself) is when you have a leading dash in the file name, like '-foo':

        $ rm '-foo' rm: invalid option -- o Try `rm ./-foo' to remove the file `-foo'. Try `rm --help' for more information.

        But, as you can see, rm is being helpful (at least some versions of it on Linux) by suggesting the workaround for this special case, i.e. rm ./-foo

Re: Deleting an Odd Filename (inode)
by toolic (Bishop) on Jun 01, 2009 at 14:24 UTC
    I learned this technique from the Unix Power Tools book: use the two-step process of ls (to identify the file's i-number) and find (to remove the file). For example, if ls shows the i-number to be 6239:
    $ find . -inum 6239 -exec rm {} \;

      Nice, but it will delete ALL hardlinks to the file that are in or below the current directory, not just the file you want to delete. (OK, files with more than one hardlink are rare except when busybox is near.)

      # touch foo
      # ln foo bar
      # ls -li
      total 0
      54804566 -rw-r--r-- 2 alex users 0 2009-06-03 00:52 bar
      54804566 -rw-r--r-- 2 alex users 0 2009-06-03 00:52 foo
      # find . -inum 54804566 -exec rm {} \;
      # ls -l
      total 0
      #
      

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Deleting an Odd Filename
by lakshmananindia (Chaplain) on Jun 01, 2009 at 10:01 UTC

    Try rm popdatck\ .\ \$dept

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.