Skip to content

August 31, 2010

9

How to remove files with special characters in Linux

I recently found myself having to remove a file with special characters. Using rm only gave me “rm: unrecognized option”, which forced me to take off the gloves.

To remove a file with dashes, slashes or other special characters, the easiest way is to access the file using its inode. To get the inode of a file, just do a ls -li. When you have that number, use find to delete the file using the following command:

find . -inum [inode] -exec rm -i {} \;

Good hunting!

Read more from Articles
9 Comments Post a comment
  1. Ben
    Aug 31 2010

    rm has an option built-in for doing this (see the man page):


    To remove a file whose name starts with a `-', for example `-foo', use one of these commands:

    rm -- -foo
    rm ./-foo

    so,


    $ rm -- -\,my\?\ funky.file\$name

    Reply
    • Aug 31 2010

      Excellent! There is one thing rm can’t handle though as far as I know, and that is when filenames contain the slash (/) character.

      Reply
  2. Ben
    Aug 31 2010

    I don’t think Linux allows a ‘/’ in a filename. ‘\’ is ok:

    ~/tmp2|$ touch — file/foo
    touch: file/foo: No such file or directory
    ~/tmp2|$ touch “file/foo”
    touch: file/foo: No such file or directory
    ~/tmp2|$ touch — file\/foo
    touch: file/foo: No such file or directory
    ~/tmp2|$ touch “file\/foo”
    touch: file\/foo: No such file or directory
    ~/tmp2|$ touch — ‘file/foo’
    touch: file/foo: No such file or directory
    ~/tmp2|$ touch ‘file/foo’
    touch: file/foo: No such file or directory
    ~/tmp2|$ touch file\foo
    ~/tmp2|$ ls
    filefoo
    ~/tmp2|$ touch file\\foo
    ~/tmp2|$ ls
    file\foo filefoo

    An exception is a unicode ‘/’ maybe.

    Reply
  3. Aug 31 2010

    Mac OS for instance, seems to be able to handle using slashes in filenames. And if creating a file on say a NFS mount, and using that share in Linux could make things interesting.

    Reply
    • Ben
      Aug 31 2010

      Interesting, FWIW, all the tests I did above were in Terminal on a Macbook. Didn’t think to try from Finder.

      But… here’s your answer… OS X saves the ‘/’ as ‘:’ on the file system, so it’s no problem:

      ~/tmp2|$ ls -l
      total 8
      -rw-r–r–@ 1 ben staff 317 Aug 31 09:35 file:foo::bar.rtf

      ~/tmp2|$ rm file:foo::bar.rtf

      ~/tmp2|$ ls -l
      ~/tmp2|$

      Reply
    • Aug 31 2010

      Interesting indeed. I just tried using Finder, assuming that it’s the same as using the terminal.

      On a side note, he//o is converted to he��o? when saved to a linux server over smb.

      Reply
      • Aug 31 2010

        Creating a file in Mac OS and saving it to a linux server using smb :D

Trackbacks & Pingbacks

  1. How to remove files with special characters in Linux | Ubuntu Geek

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments