When a file or directory is "deleted" its inode number is removed from the directory which contains the file. You can see the list of inodes that a given directory contains using the tree command.
Example
$ tree -a -L 1 --inodes . . |-- [9571121] dir1 |-- [9571204] dir2 |-- [9571205] dir3 |-- [9571206] dir4 |-- [9571208] dir5 |-- [9571090] file1 |-- [9571091] file2 |-- [9571092] file3 |-- [9571093] file4 `-- [9571120] file5 5 directories, 5 files
Links
It's important to understand how hardlinks work. This tutorial titled: Intro to Inodes has excellent details if you're just starting out in trying to get a fundamental understanding of how inodes work.
excerpt
Inode numbers are unique, but you may have noticed that some file name and inode number listings do show some files with the same number. The duplication is caused by hard links. Hard links are made when a file is copied in multiple directories. The same file exists in various directories on the same storage unit. The directory listing shows two files with the same number which links them to the same physical on te storage unit. Hard links allow for the same file to "exist" in multiple directories, but only one physical file exists. Space is then saved on the storage unit. For example, if a one megabyte file is placed in two different directories, the space used on the storage is one megabyte, not two megabytes.
Deleting
That same tutorial also had this to say about what happens when a inode is deleted.
Deleting files causes the size and direct/indirect block entries are zeroed and the physical space on the storage unit is set as unused. To undelete the file, the metadata is restored from the Journal if it is used (see the Journal article). Once the metadata is restored, the file is once again accessible unless the physical data has been overwritten on the storage unit.
Extents
You might want to also brush up on extents and how they work. Again from the linux.org site, another good tutorial, titled: Extents will help you get the basics down.
You can use the command filefrag to identify how many extents a given file/directory is using.
Examples
$ filefrag dir1 dir1: 1 extent found $ filefrag ~/VirtualBox\ VMs/CentOS6.3/CentOS6.3.vdi /home/saml/VirtualBox VMs/CentOS6.3/CentOS6.3.vdi: 5 extents found
You can get more detailed output by using the -v switch:
$ filefrag -v dir1 Filesystem type is: ef53 File size of dir1 is 4096 (1 block of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0.. 0: 38282243.. 38282243: 1: eof dir1: 1 extent found
NOTE: Notice that a directory always consumes at a minimum, 4K bytes.
Giving a file some size
We can take one of our sample files and write 1MB of data to it like this:
$ dd if=/dev/zero of=file1 bs=1k count=1k 1024+0 records in 1024+0 records out 1048576 bytes (1.0 MB) copied, 0.00628147 s, 167 MB/s $ ll | grep file1 -rw-rw-r--. 1 saml saml 1048576 Dec 9 20:03 file1
If we analyze this file using filefrag:
$ filefrag -v file1 Filesystem type is: ef53 File size of file1 is 1048576 (256 blocks of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0.. 255: 35033088.. 35033343: 256: eof file1: 1 extent found
Deleting and recreating a file quickly
One interesting experiment you can do is to create a file, such as file1 above, and then delete it, and then recreate it. Watch what happens. Right after deleting the file, I re-run the dd ... command and file1 shows up like this to the filefrag command:
$ filefrag -v file1 Filesystem type is: ef53 File size of file1 is 1048576 (256 blocks of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0.. 255: 0.. 255: 256: unknown,delalloc,eof file1: 1 extent found
After a bit of time (seconds to minutes pass):
$ filefrag -v file1 Filesystem type is: ef53 File size of file1 is 1048576 (256 blocks of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0.. 255: 38340864.. 38341119: 256: eof file1: 1 extent found
The file finally shows up. I'm not entirely sure what's going on here, but it looks like it takes some time for the file's state to settle out between the journal & the disk. Running stat commands shows the file with an inode so it's there, but the data that filefrag uses hasn't been resolved so we're in a bit of a limbo state.