In Linux, there at all times appears to be an ingenious manner of getting issues accomplished. For any job, there’s at all times multiple command-line utility to execute it in a greater manner. The Linux stat command is a command-line software used to show detailed details about a file. In this information, we spotlight Eight stat command usages in Linux. This works throughout all Linux distributions.
Linux stat command
Think of stat command as a greater model of the ls -l command. While the -l flag gives extra particulars about recordsdata comparable to file possession and permissions, The stat command goes deeper underneath the hood and gives a wealth of details about a file.
The syntax for Linux stat command is as proven:
$ stat [OPTION] filename
1) Stat command with no arguments
In its easiest type – with none parameters – the stat command shows the default output. This contains file measurement and sort, gadget kind, inode quantity, UID, GID, variety of hyperlinks and entry/modification dates of the file.
For instance, to view the file particulars of a file residing within the present residence listing, execute:
$ stat file1.txt
Let’s flesh out the output:
- File : This exhibits the title of the file.
- Size : Size of the file in bytes.
- Block : Number of blocks allotted to the file.
- IO Block : This is the byte measurement of each block.
- Device : The gadget quantity in hexadecimal or decimal format.
- Inode : This is the inode variety of the file.
- Links : Number of exhausting hyperlinks related to the file.
- Access : File permissions both in symbolic or numeric format.
- Uid : User ID & title of the proprietor.
- Gid : Group ID & title of the proprietor.
- Context : SeLinux safety context
- File kind : Shows what kind the file is (Whether a daily file, symbolic hyperlink and so forth).
- Access : Shows the final time the file was accessed.
- Modify : Shows the final time the contents of the file have been modified.
- Change : Shows the final time a file’s metadata e.g permissions & possession was modified.
2) View details about a number of recordsdata
You can view an in depth report on a number of recordsdata by specifying the recordsdata on the command line one after the opposite as proven.
$ stat file1.txt file2.pdf
3) Display file system standing
You can examine the filesystem standing the place the file is sitting on by utilizing the -f possibility as proven. This provides you the block measurement, whole & out there reminiscence to say only a few attributes.
$ stat -f /residence
4) Display data in terse type
The -t possibility is used to show data in a terse format as proven:
$ stat -t file1.txt
5) Enable following of symbolic hyperlinks
Usually, if you happen to run the stat command on a symbolic hyperlink, it should solely offer you details about the hyperlink, and never the file that the hyperlink factors to. Take for instance the /usr/share/zoneinfo/America/Cayman symbolic hyperlink.
$ stat /usr/share/zoneinfo/America/Cayman
In the instance above, the symbolic hyperlink /usr/share/zoneinfo/America/Cayman factors to ( ->) Panama. The symbolic hyperlink is barely 6 bytes.
To get data on the file that the hyperlinks factors to, use the -L possibility also referred to as the dereference possibility.
$ stat -L /usr/share/zoneinfo/America/Cayman
This now shows details about the file and never the hyperlink, though the output suggests it’s the hyperlink. This is as a result of we handed it as an argument with the stat command.
6) Format sequencing
From earlier examples, we’ve seen that stat command prints out a barrage of knowledge on the terminal. If you need particular data, you’ll be able to customized the output utilizing a format sequence to offer precisely what you want and go away different particulars.
Popular expressions used to customized the output embody –printf or –format possibility
For instance, to show inode of a file solely, use the %i format sequence as proven. The n operand print a brand new line.
$ stat --printf="%in" file1.txt
To show entry rights and uid (User ID) use the %a and %u format sequences.
$ stat --printf="%a:%un" file1.txt
The –format possibility prints out a brand new line with out requiring a further operand.
$ stat --format="%a:%F" file1.txt
Here’s a whole checklist of the format sequences that you need to use:
- %a Displays the entry rights in octal format.
- %A Displays the entry rights in a human readable format.
- %b This is the variety of blocks allotted (see %B).
- %B the scale in bytes of every block reported by %b.
- %C Shows the SELinux safety context string.
- %d Displays the gadget quantity in a decimal format.
- %D The gadget quantity in hexadecimal format.
- %f Displays the uncooked mode in hexadecimal.
- %F Displays the file kind.
- %g Prints the group ID of proprietor.
- %G Prints the group title of proprietor.
- %h Displays the variety of exhausting hyperlinks.
- %i Prints out the inode quantity.
- %m Prints the mount level.
- %n Displays the file title of the file
- %N Shows quoted file title with dereference if symbolic hyperlink
- %o Prints the optimum I/O switch measurement trace.
- %s whole measurement, in bytes.
- %t main gadget kind in hex, for character/block gadget particular recordsdata
- %T minor gadget kind in hex, for character/block gadget particular recordsdata
- %u Shows the person ID of proprietor.
- %U Prints the username of proprietor.
- %w Reveals the time of file beginning, human-readable; – if unknown.
- %W Prints the time of file beginning, seconds since Epoch; zero if unknown.
- %x The time of final entry, human-readable.
- %X The time of final entry, seconds since Epoch.
- %y Displays the final time of final modification, human-readable.
- %Y Prints the time of final modification, seconds since Epoch.
- %z This is the time of final change, human-readable.
- %Z The time of final change, seconds since Epoch.
7) Getting assist with stat command
For extra command choices , use the –assist possibility with stat command as proven.
$ stat --help
You may also go to the person pages of stat command, run
$ man stat
8) Checking the model of stat
Finally to examine the model of stat command, run the command:
$ stat --version
That’s what we had in retailer for you on this information. As you’ll be able to see, the stat command goes over and above what the ls command provides you and is good in gathering detailed details about a file or file system.
Also Read : 14 Useful ‘ls’ Command Examples in Linux