The debugfs
ext2/ext3/ext4 file system debugger has a block_dump
command that outputs bytes similarly to the default output format of the xxd
command. Unlike xxd
there doesn't appear to be a way to turn off the elision of null bytes so you will see a single *
for any number of contiguous 32-byte sequences - you will only see the "full" block if it actually is "full".
The command's syntax provides for either an absolute block offset (obtained from the debugfs blocks
command for example), or an offset relative to a "filespec" consisting of either an inode number in angle brackets or an absolute path.
So for example, given
$ ls -lis hello.txt
691662 4 -rw-rw-r-- 1 steeldriver steeldriver 6 Jun 7 08:39 hello.txt
where the filesystem is confirmed as
$ findmnt -T hello.txt
TARGET SOURCE FSTYPE OPTIONS
/ /dev/sda1 ext4 rw,relatime
then (interactively)
$ sudo debugfs /dev/sda1
[sudo] password for steeldriver:
debugfs 1.46.5 (30-Dec-2021)
debugfs:
debugfs: block_dump -f /home/steeldriver/dir/hello.txt 0
0000 6865 6c6c 6f0a 0000 0000 0000 0000 0000 hello...........
0020 0000 0000 0000 0000 0000 0000 0000 0000 ................
*
debugfs:
or
debugfs: block_dump -f <691662> 0
0000 6865 6c6c 6f0a 0000 0000 0000 0000 0000 hello...........
0020 0000 0000 0000 0000 0000 0000 0000 0000 ................
*
debugfs:
or
debugfs: blocks <691662>
5151850
debugfs: block_dump 5151850
0000 6865 6c6c 6f0a 0000 0000 0000 0000 0000 hello...........
0020 0000 0000 0000 0000 0000 0000 0000 0000 ................
*
debugfs:
If you need to do it non-interactively, the -f
option allows commands to be read from file or from standard input ex.
printf 'block_dump -f <%d> 0\n' "$(stat -c %i hello.txt)" | sudo debugfs -f - /dev/sda1
or you can use the -R
option to execute a single request
sudo debugfs -R 'block_dump -f /home/steeldriver/dir/hello.txt 0' /dev/sda1
The filesystem is opened in read-only mode by default.