Find the answer to your Linux question:
Results 1 to 6 of 6
Hi Guys While programming java sometimes files with following type of name are built: #filename# The command rm -rf #* fails to delete them, while rm -rf *# is successful. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    7

    Simple rm command on console


    Hi Guys

    While programming java sometimes files with following type of name are built:

    #filename#

    The command

    rm -rf #*

    fails to delete them, while

    rm -rf *#

    is successful.
    Why is this the case? Has someone an idea?

    Thanks for your help!

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    # is interpreted by the shell as "here starts a comment"

    Hence rm -rf #* is actually the same as rm -rf
    It you wouldnt use -f, rm would complain about a missing operand.

    In the same way, rm -rf *# boils down to rm -rf *
    Which is potentially dangerous, as it matches any file and directory.

    You can use quotes, doublequotes or the inode numbers to remove these files.
    Last edited by Irithori; 12-07-2012 at 07:40 AM.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Nov 2011
    Posts
    7
    Thank you.

    Quote Originally Posted by Irithori View Post
    In the same way, rm -rf *# boils down to rm -rf *
    Which is potentially dangerous, as it matches any file and directory.
    This actually never happened. There were always files of type filename.java in the same directory, which never were erased.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    195
    What Irithori said... You have to mask the naked "#" somehow. One trick I've used (in addition to the suggestion to use single quotes or "\" escape characters) to help deal with files with odd characters (sometimes binary garbage) in name is to pre-pend "./" to the filename. So you can try: rm ./#file and it will work. That helps with annoying cases like filenames that start with "-" as well.

  6. #5
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    Quote Originally Posted by Meternagel View Post
    There were always files of type filename.java in the same directory, which never were erased.
    Are you sure?
    rm -rf * WILL match any file and directory.
    If they didnt get removed, then they are maybe protected by a different uid.
    Or the files get copied back somehow.

    @cnamejj: For the sake of completeness, there is also the -- syntax.
    rm -f -- -foo
    You must always face the curtain with a bow.

  7. #6
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    Correction:
    rm -rf *# is not the same as rm -rf *

    Interesting, one learns something new each day
    You must always face the curtain with a bow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •