How to find a file starting and end blocks in Linux file system ?

We know in Linux, everything is a file with some blocks usage on hard disk.

Suppose you want to know the starting and end blocks of any file in Linux.

To know this information, we can use any of the below three ways.

1. Using hdparm command.
The most convenient and easiest way is to use hdparm command which shows both begin and end block numbers and other details.

[root@nglinux ~]# hdparm --fibmap file_recover
file_recover:
 filesystem blocksize 4096, begins at LBA 63; assuming 512 byte sectors.
 byte_offset  begin_LBA    end_LBA    sectors
           0   30606135   30606142          8

2. Using filefrag command in verbose mode.

[root@nglinux ~]# filefrag -v file_recover
Filesystem type is: ef53
File size of file_recover is 4096 (1 block, blocksize 4096)
 ext logical physical expected length flags
   0       0  3825759               1 eof
file_recover: 1 extent found
[root@nglinux ~]# 

3. Using debugfs command.

[root@nglinux ~]# debugfs -R "stat <`ls -i /root/file_recover | awk '{print $1}'`>" /dev/sda1
debugfs 1.41.12 (17-May-2010)
Inode: 918589   Type: regular    Mode:  0644   Flags: 0x80000
Generation: 1214074572    Version: 0x00000000:00000001
User:     0   Group:     0   Size: 4096
File ACL: 0    Directory ACL: 0
Links: 1   Blockcount: 8
Fragment:  Address: 0    Number: 0    Size: 0
 ctime: 0x5a5af7ca:b3c5f93c -- Sat Jan 13 22:25:14 2018
 atime: 0x5a5af7ce:15b3a418 -- Sat Jan 13 22:25:18 2018
 mtime: 0x5a5af7ca:b3c5f93c -- Sat Jan 13 22:25:14 2018
crtime: 0x5a5aee81:c19a30a4 -- Sat Jan 13 21:45:37 2018
Size of extra inode fields: 28
Extended attributes stored in inode body: 
  selinux = "unconfined_u:object_r:admin_home_t:s0\000" (38)
EXTENTS:
(0): 3825759
[root@nglinux ~]# 

It all depends which utility is present on your server, you can use that.

I hope you liked the above trick.

Please do post your comments/suggestions below.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments