Simple way to see the content of directories in Linux/UNIX file systems

In the past, I learned that in Linux/UNIX file systems, directories are just files, which contain the filenames and inode numbers of the files inside the directory.

Is there a simple way to see the content of a directory? I mean the way the files names and inodes are stored/organized.

I’m not looking for ls, find or something similiar. I also don’t want to see the content of the files inside a directory. I want to see the implementation of the directories. If every directory is just a text file with some content, maybe a simple way exists to see the content of this text file.

In the bash in Linux it is not possible to do a cat folder. The output is just Is a directory.

Update The question How does one inspect the directory structure information of a unix/linux file? addresses the same issue but it has no helpful solution like the one from mjturner.

Asked By: Neverland

||

You can use your programming language of choice, open the directory as if it was a file and read bytes from the resulting file handle. That’s not going to tell you much, though, since it will just be garbage (with a few recognizable strings in it) as long as you don’t know how it’s organized. How it is organized is pretty much an implementation issue for the file system in question. If you want to look into these things in depth, I suggest you start by reading man dirent.h. That should be enough to point you further toward what strikes your fancy.

Answered By: Calle Dybedahl

The tool to display inode detail for a filesystem will be filesystem specific. For the ext2, ext3, ext4 filesystems (the most common Linux filesystems), you can use debugfs, for XFS xfs_db, for ZFS zdb. For btrfs some information is available using the btrfs command.

For example, to explore a directory on an ext4 filesystem (in this case / is dev/sda1):

# ls src
Animation.js    Map.js        MarkerCluster.js    ScriptsUtil.js
Directions.js   MapTypeId.js  markerclusterer.js  TravelMode.js
library.js      MapUtils.js   Polygon.js          UnitSystem.js
loadScripts.js  Marker.js     Polyline.js         Waypoint.js

# ls -lid src
664488 drwxrwxrwx 2 vagrant vagrant 4096 Jul 15 13:24 src

# debugfs /dev/sda1
debugfs: imap <664488>
Inode 664488 is part of block group 81
        located at block 2622042, offset 0x0700
debugfs: dump src src.out
debugfs: quit

# od -c src.out
0000000 250   #  n    f   001 002   .       204 030  n  
0000020  f   002 002   .   .     251   #  n   024    f 001
0000040   A   n   i   m   a   t   i   o   n   .   j   s 252   #  n  
0000060 030    r 001   D   i   r   e   c   t   i   o   n   s   .   j
0000100   s       253   #  n   024    n 001   l   i   b   r
0000120   a   r   y   .   j   s     254   #  n   030   016 001
0000140   l   o   a   d   S   c   r   i   p   t   s   .   j   s    
0000160 255   #  n   020   006 001   M   a   p   .   j   s    
0000200 256   #  n   024    f 001   M   a   p   T   y   p   e   I
0000220   d   .   j   s 257   #  n   024    v 001   M   a   p   U
0000240   t   i   l   s   .   j   s   260   #  n   024    t 001
0000260   M   a   r   k   e   r   .   j   s       261   #  n  
0000300 030   020 001   M   a   r   k   e   r   C   l   u   s   t   e
0000320   r   .   j   s 262   #  n   034   022 001   m   a   r   k
0000340   e   r   c   l   u   s   t   e   r   e   r   .   j   s    
0000360 263   #  n   024    n 001   P   o   l   y   g   o   n   .
0000400   j   s     264   #  n   024    v 001   P   o   l   y
0000420   l   i   n   e   .   j   s   265   #  n   030   016 001
0000440   S   c   r   i   p   t   s   U   t   i   l   .   j   s    
0000460 266   #  n   030    r 001   T   r   a   v   e   l   M   o
0000500   d   e   .   j   s       267   #  n   030    r 001
0000520   U   n   i   t   S   y   s   t   e   m   .   j   s      
0000540 270   #  n   240 016  v 001   W   a   y   p   o   i   n   t
0000560   .   j   s   305 031  n   214 016 022 001   .   U   n   i
0000600   t   S   y   s   t   e   m   .   j   s   .   s   w   p    
0000620 312 031  n     p 016 022 001   .   U   n   i   t   S   y   s
0000640   t   e   m   .   j   s   .   s   w   x            
0000660                                

In the above, we start by finding the inode of directory src (664488) and then dump its contents into file src.out and then display that using od. As you can see, the contents of all of the files in that directory (Animation.js, etc.) are visible in the dump.

This is just a start – see the debugfs manual page or type help within debugfs for more information.

If you’re using ext4, you can find more information about the structure and layout of directory entries in the kernel documentation.

Answered By: mjturner

You can try (partition is an example).

sudo debugfs /dev/xvda1                 

use dump to write inode data to a file.

sudo dumpe2fs /dev/xvda1

man is your friend, these should give you some ideas.

Answered By: mckenzm