Pages

Thursday, 2 July 2020

Linux files' metadata

The metadata of a file is all the information about this file except its content. Usually, the metadata is stored in the i-node directly with a special format. Common metadata includes file size, owner, permissions, ACL, SELinux context.

Linux provides there different groups of commands to operate on three different classes of metadata.

1 Basic metadata

This is the most common info and almost every Linux user is familiar with it. 

$ stat /usr/bin/ls
  File: /usr/bin/ls
  Size: 166448          Blocks: 328        IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 201329808   Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:bin_t:s0
Access: 2020-07-02 07:55:19.074306673 -0400
Modify: 2019-05-11 12:07:58.000000000 -0400
Change: 2019-10-11 23:05:41.658933183 -0400
 Birth: -

2 i-node flags

This kind of metadata is not supported by all Unix platforms but very popular in Linux.

$ lsattr /usr/bin/ls
------------------- /usr/bin/ls

3 Extended attributes

Extended attributes or EA are added to Linux in version 2.6. Even though EAs are mainly used for ACL/Capabilities/SElinux, they are designed for general purpose.

$ getfattr -d -m- /usr/bin/ls
getfattr: Removing leading '/' from absolute path names
# file: usr/bin/ls
security.selinux="system_u:object_r:bin_t:s0"

4 A single command for all metadata

Due to historical reasons, Linux doesn't provide a single command to operate all the metadata at the same time. However, DIY such a command is very easy with bash script. An example is given below.

$ cat lsmeta.sh

#!/bin/bash
# A simple script to get all metadata of a file

echo "(1) normal attributes"
stat $1

echo "(2) i-node flags"
lsattr $1

echo "(3) extended attributes"
getfattr -d -m- $1



No comments:

Post a Comment