What if I told you there is a better way to check file attributes? It’s a simple but useful command called stat.
stat command in Linux
The stat command lists important attributes of files and directories. It can also display information on the filesystem, instead of the files.
It has simple syntax:
stat [options] files
There are only a few options with stat command:
- -f : Show the information for the filesystem instead of the file
- -L : Follow symbolic links and show information on the file they point to
- -t : Terse mode prints the information in a single line
Using stat command to get file information
Here’s a sample output if you use the stat command without any options:
stat sample.txt File: sample.txt Size: 426 Blocks: 8 IO Block: 4096 regular file Device: 10302h/66306d Inode: 8259907 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/abhishek) Gid: ( 1000/abhishek) Access: 2020-01-06 09:48:02.908946552 +0530 Modify: 2020-01-05 10:16:33.225203223 +0530 Change: 2020-01-06 19:51:28.737207649 +0530 Birth: -
Let me explain the output to you:
- File: Name of the file.
- Size: Size of the files in bytes.
- Blocks: Number of filesystem blocks the file needs on the disk.
- IO Block: Size of a filesystem block. It is usually 4 KB.
- File type: It could be file, directory, link, sockets.
- Device: The device number (ID of the hard drive) in hexadecimal and decimal.
- Inode: The inode number. Read more about inodes in Linux.
- Links: Number of hard links to the file.
- Access: File permissions in absolute mode and symbolic mode.
- Uid: User id and the user name of the file owner
- Gid: Group id and group name of the file owner.
- Access: This access is about access timestamp (last time the file was accessed), also called atime.
- Modify: The modify timestamp (last time the file was modified), also called mtime.
- Change: The change timestamp (last time the file was called), also called ctime. Read this article to know more about atime, mtime and ctime.
- Birth: Original creation date of the file. This is not used in Linux.
If you use the terse mode with option -t, you’ll see pretty much the same information (some orders could be different) in a single line but without any description of which field is what.
[email protected]:$ stat -t sample.txt sample.txt 426 8 81b4 1000 1000 10302 8259907 1 0 0 1578284282 1578199593 1578320488 0 4096
Get filesystem information with stat command
You can use the -f option to display information of the filesystem instead of the file itself.
[email protected]:$ stat -f sample.txt File: "sample.txt" ID: 65205b688c8b079c Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 61142095 Free: 41522899 Available: 38399635 Inodes: Total: 15597568 Free: 15279217
As you can see in the output, it gives you the following information:
- File: Name of the file.
- ID: Filesystem ID in hexadecimal.
- Namelen: Maximum length for file names.
- Type: Filesystem type.
- Block size: The amount of data to request read requests for optimum data transfer rates.
- Fundamental block size: Size of a block on filesystem
- Total blocks, free blocks and available blocks (for non-root users)
- Number of total inodes and free inodes
Use stat command with multiple files
You can run the stat command with multiple files.
You can provide their names individually like this:
stat file1.txt file2.txt
You can also provide a file name pattern:
I hope you liked this handy little command. Stay tuned for more Linux command learning.