stat command in Linux
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
In this article, we have discussed the stat command in Linux which is used to display information regarding files and file systems in Linux.
Table of contents.
- Introduction.
- Syntax.
- Commands.
- Summary.
- References.
Introduction.
The stat command is used to print out the status of Linux files, directories and file systems.
Unlike ls command, stat prints out a lot of information regarding files, directories and file systems such as their sizes, blocks, inodes, permissions, timestamps for modification, access, change dates etc.
Metadata about a file is stored by the inode, by using ls command to list files and directories it will fetch its output from inodes however ls is limited in terms of the information we can obtain about a file or directory, to go deeper stat command is used.
Syntax.
The syntax is as follows,
stat [OPTION]... FILE...
Commands.
To get started, let's execute the stat command on a file or directory and analyze its output:
$ stat Desktop
From the output from the above command;
File, which is the name of the file or directory, this can be different if we are getting the status of a symbolic link.
-
Size, this is its size in bytes.
-
Blocks, the number of blocks this file or directory occupies in the disk.
-
IO Block, the size of a single block.
-
File type, this can be a regular file, a directory, a symbolic link, named pipes, sockets etc.
-
Device, the ID of the disk in which the file is stored.
-
Inode, ID of the inode. This together with the disk ID are used to uniquely identify a file.
-
Links, number of hard links pointing to the file.
-
Access, permissions for the file.
-
Uid, the ID and username of the the owner of the file.
-
Gid, the group ID and name of the group to which the file owner belongs to.
-
Access, timestamps for when the file was last accessed.
-
Modify, when the file contents were last modified
-
Change, when it was last changed for example, file attributes.
-
Birth, when the file was created.
-
To display information about a file system on which a file or directory is housed we use the -f option,
$ stat -f /
- We can also specify the output we want to print out:
$ stat --format "%n %a %U %G %s %w" Desktop/
The above command prints out the file name, file permissions in octal form, the file owner's username, the group name to which the owner belongs to, the total file size in bytes and the timestamp when it was created in that order.
We can build other formatting sequences using the options provided in the manual page.
- The -t option prints out the output in a short simple form(terse form) which can be used for parsing by other Linux utilities.
$ stat -t DIR/
DIR/ 4096 8 41ed 1112 1117 803 18612273 2 0 0 1648935161 1646714213 1645561413 1646652160 4096
- We can also pass multiple files or directories as arguments to the stat command.
$ stat -t DIR1/ DIR2/
- A symbolic link symlink is a link pointing to another directory or file.
Create a symlink as follows,
$ ln -s file fileLink
Now when we try to get the status of fileLink symbolic link, stat command will return information about the symbolic link instead of the file it points to.
To get information about file we use the -L option:
$ stat -L fileLink
Summary.
Although the ls command works just fine, sometimes we may need all of the information regarding files and file systems and this is where stat command is useful.
References.
- Execute man stat or stat --help for its manuals.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.