inode – What is it?

By | 2010/07/29

What is an inode?

Linux (and Unix) stores information about files and directories in what is known as an inode. An inode contains the file metadata.

Each file on your system, for example, has a unique inode number.

To view the inode number for a given directory or file, run ls -i:

$ ls -i thinkpad.txt 
2622442 thinkpad.txt

A related command to know is the stat command.

Running stat filename for example shows information about the file including the inode number, size in bytes, and other cool stuff.

$ stat thinkpad.txt 
  File: `thinkpad.txt'
  Size: 764       	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 2622442     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/stmiller)   Gid: ( 1000/stmiller)
Access: 2010-07-27 20:47:50.076627420 -0700
Modify: 2010-03-18 21:05:31.489674000 -0700
Change: 2010-06-20 20:49:06.197021789 -0700


Ok that’s great, so what is a real-world usage with this?

Well, one famous example from the historic Unix FAQ is if you are cannot move or delete a file that has an odd character in the filename, you can use the inode number instead.

First find the inode number by running:

ls -i .

Then you can do something like this:

find . -inum 12345 -ok rm '{}' \;

or

find . -inum 12345 -ok mv '{}' new-file-name \;

Leave a Reply

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

Notify me of followup comments via e-mail. You can also subscribe without commenting.