inside the mind of a linux admin

Howto: Delete files by inode number

Ever mistakenly pipe output to a file with special characters that you couldn’t remove?

-rw-r–r– 1 eriks eriks 4 2011-02-10 22:37 –fooface

Good luck. Anytime you pass any sort of command to this file, it’s going to interpret it as a flag. You can’t fool rm, echo, sed, or anything else into actually deeming this a file at this point. You do, however, have a inode for every file.

Traditional methods fail:

[eriks@jaded: ~]$ rm -f –fooface
rm: unrecognized option ‘–fooface’
Try rm ./--fooface' to remove the file –fooface’.
Try rm --help' for more information.
[eriks@jaded: ~]$ rm -f '--fooface'
rm: unrecognized option '--fooface'
Try
rm ./–fooface’ to remove the file --fooface'.
Try
rm –help’ for more information.

So now what, do you live forever with this annoyance of a file sitting inside your filesystem, never to be removed or touched again? Nah.

We can remove a file, simply by an inode number, but first we must find out the file inode number:

$ ls -il | grep foo

Output:

[eriks@jaded: ~]$ ls -il | grep foo
508160 drwxr-xr-x 3 eriks eriks 4096 2010-10-27 18:13 foo3
500724 -rw-r–r– 1 eriks eriks 4 2011-02-10 22:37 –fooface
589907 drwxr-xr-x 2 eriks eriks 4096 2010-11-22 18:52 tempfoo
589905 drwxr-xr-x 2 eriks eriks 4096 2010-11-22 18:48 tmpfoo

The number you see prior to the file permission set is actually the inode # of the file itself.

Hint: 500724 is inode number we want removed.

Now use find command to delete file by inode:

# find . -inum 500724 -exec rm -i {} \;

There she is.

[eriks@jaded: ~]$ find . -inum 500724 -exec rm -i {} \;
rm: remove regular file `./–fooface’? y

And done.

Related Posts

synergy: How to enable crypto (encryption) and generate SSL certificate

The newer Linux versions of the popular mouse/keyboard sharing application “synergy” now has built in encryption. Here’s how to configure it: Just simply passing the –enable-crypto flag on your synergy server without having a proper SSL certificate will result in the inability to connect to clients and generate an error message similar to this in […]

Read More

Change Number Pad Delete (dot) key from a comma in Ubuntu Linux

I recently purchased a new keyboard and updated to the latest Ubuntu, I’m also an avid user of the number pad for quick input when dealing with spreadsheets or accounting. I found that my num pad’s delete key (“.”) was outputting a comma (“,”) instead. Pretty annoying? I agree, but this can be very easily […]

Read More

6 Comments

  • kire on Thursday, February 10, 2011

    WOW KIREGUY, YOU’RE AWESOME. THANKS KIREGUY.

  • Lee on Friday, February 18, 2011

    Seems rather unnecessary in this example, sir. rm ./-fooface will work

    BUT GOOD JOB ANYWAYS AIRICK

  • Marlon on Friday, November 18, 2011

    THANKS!!!

    GREETINGS FROM BRAZIL!!

  • Dan on Friday, August 31, 2012

    The correct way to prevent interpreting option arguments is: `rm — -fooface’.

    To get filenames with unprintable characters and other such braindamage in shell-escaped format, in Bash, ksh93, or Zsh, use:

    printf ‘%q\n’ ./*

    and just pass the relevant string or expansion. e.g.

    rm — $’./\31204′

  • Yaar on Friday, September 27, 2013

    Alternatively, if you don’t want to mess with -exec, you can pass -delete instead. You should also pass -depth 1.

  • Yonatan on Sunday, September 20, 2015

    “unlink –fooface” would work as well, as unlink doesn’t accept any flags.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Twitter: kireguy

Tweeter button Facebook button Myspace button