About nixCraft

Topics

How to: Linux / UNIX Delete or Remove Files With Inode Number

Posted by Vivek Gite [Last updated: December 14, 2007]

An inode identifies the file and its attributes such as file size, owner, and so on. A unique inode number within the file system identifies each inode. But, why to delete file by an inode number? Sure, you can use rm command to delete file. Sometime accidentally you creates filename with control characters or characters which are unable to be input on a keyboard or special character such as ?, * ^ etc. Removing such special character filenames can be problem. Use following method to delete a file with strange characters in its name:

Please note that the procedure outlined below works with Solaris, FreeBSD, Linux, or any other Unixish oses out there:

Find out file inode

First find out file inode number with any one of the following command:

stat {file-name}

OR

ls -il {file-name}

Use find command to remove file:

Use find command as follows to find and remove a file:

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

When prompted for confirmation, press Y to confirm removal of the file.

Delete or remove files with inode number

Let us try to delete file using inode number.

(a) Create a hard to delete file name:
$ cd /tmp
$ touch "\+Xy \+\8"
$ ls

(b) Try to remove this file with rm command:
$ rm \+Xy \+\8

(c) Remove file by an inode number, but first find out the file inode number:
$ ls -ilOutput:

781956 drwx------  3 viv viv 4096 2006-01-27 15:05 gconfd-viv
781964 drwx------  2 viv viv 4096 2006-01-27 15:05 keyring-pKracm
782049 srwxr-xr-x  1 viv viv    0 2006-01-27 15:05 mapping-viv
781939 drwx------  2 viv viv 4096 2006-01-27 15:31 orbit-viv
781922 drwx------  2 viv viv 4096 2006-01-27 15:05 ssh-cnaOtj4013
781882 drwx------  2 viv viv 4096 2006-01-27 15:05 ssh-SsCkUW4013
782263 -rw-r--r--  1 viv viv    0 2006-01-27 15:49 \+Xy \+\8

Note: 782263 is inode number.

(d) Use find command to delete file by inode:
Find and remove file using find command, type the command as follows:
$ find . -inum 782263 -exec rm -i {} \;

Note you can also use add \ character before special character in filename to remove it directly so the command would be:
$ rm "\+Xy \+\8"

If you have file like name like name "2005/12/31" then no UNIX or Linux command can delete this file by name. Only method to delete such file is delete file by an inode number. Linux or UNIX never allows creating filename like 2005/12/31 but if you are using NFS from MAC OS or Windows then it is possible to create a such file.

See also:

Tell us how we're doing: Please answer a few questions about your experience to help us improve nixCraft.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Anonymous Says:

    What I’m wondering is:”how to do that on a remote server via ftp?” :)

  2. nixcraft Says:

    Very easy :) Login to your remote ftp server.

    ftp http://ftp.some.com

    Goto directory where special character files placed. And type command:

    mdel *

    mdel will ask you if you really wish to delete each file in the directory. Make sure you answer y (for yes) for each file containing the special character that you wish to delete. Hope this helps.

  3. Anonymous Says:

    how about deleting the file using
    rm ./”filename”

    as in this case $ rm “+Xy +8″
    ./+Xy +8: ? (y/n) y
    $

  4. Anonymous Says:

    adding to my previous post there is an instance when your rm “filename” wont work.
    say u created a file –help
    it wont get deleted by using rm “–help”

    so we have to rm ./”–help”

  5. Anonymous Says:

    Anonymous: your tip won’t work if the filename has very weird characters (non-printable characters, that is)

  6. calib0rx Says:

    “Anonymous: your tip won’t work if the filename has very weird characters (non-printable characters, that is)”

    Um, yes it will, did you actually try it? Eg, go in to vi and save a file with :wq [ctrl-v backspace]q[ctrl-v backspace]

    which would look like “^?q^?” and continue. Now you have:

    -rw-r–r– 1 root wheel 0B Sep 12 00:55 ?q?

    Where’s the ^!!!!

    So now with ls -li:

    516 -rw-r–r– 1 root wheel 0 Sep 12 00:55 ?q?

    and:

    [root@host ~]# find . -inum 516 -exec rm -i {} ;
    remove ./q? y
    [root@host ~]#

    file is gone.

  7. Anonymous Says:

    To make sure find only finds the files in the correct directory use the -mount switch to prevent it jumping across mount points.

  8. Liam Says:

    This is a simple process:
    root@liam # mkdir ^Hms^?
    root@liam # ls -l
    total 14
    drwxr-xr-x 2 root other 512 Dec 22 12:59ms

    list the directory using octal representation. So you know the control characters in the name.

    root@liam # ls -lb
    total 14
    drwxr-xr-x 2 root other 512 Dec 22 12:59 10ms\177

    Now simply rm “Control V”"Backspace”ms”Control V”"Delete”

    Where Control V is a key combination and Backspace is a key. As is Delete. In the above example 10 is BS (BackSpace) and \177 is Delete.

    Very simple…

  9. William Says:

    I have a directory structure:

    # ls -ila
    total 20
    2179132 drwxrwsr-x 2 www www 16384 2007-09-14 00:16 .
    2081399 drwxrwsr-x 101 www www 4096 2007-06-27 14:32 ..
    ? ?——— ? ? ? ? ? 1a6b29c00be.jar

    the last entry couldn’t remove (fsck.ext3 /dev/sd12, rm -f, rm -rf to parent dir, stb…), because no inode. I no idea how to remove it, only found format the partition :(

  10. FiNeX Says:

    William, I’ve your problem too:

    ls -lia
    total 40
    14663798 drwxr-xr-x 2 1001 1001 36864 2007-10-26 13:27 .
    14663789 drwxr-xr-x 3 1001 1001 4096 2007-10-26 13:27 ..
    ? ?——— ? ? ? ? ? PROVA D’ABILIT? FF8.htm
    ? ?——— ? ? ? ? ? Prova d’abilit? ff9.htm

    I can’t format the partition! There is more than 100Gb of backup file in it!!!!

    someone have some further suggestions?

  11. rubberband Says:

    I was able to remove a weird file name with unlink in Linux.
    I just inputted:
    unlink ‘weird file !@#$%&.txt’

  12. Markus Says:

    other way to remove files: try e2fsprogs
    it is working on hardly not removing files with wrong attributes

    debugfs
    open -w /dev/…
    rm File
    close
    q

    be carefull!

  13. bd Says:

    Desi Style. Move all others which you can delete to a different folder.
    Then rm -rf * & restore all those back

  14. Balakumar Says:

    It is very useful command to delete files which contain special Characters..

  15. franklin Says:

    what is rootkit

  16. Les Says:

    Thanks, that worked very well!

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.