Linuxopsys Profile picture
Jan 4 25 tweets 7 min read Read on X
In Linux, most people use the ls command to check a file's creation, access, and modification times, ownership, and permissions.

What if I told you there is another great way to display detailed information about files and file systems?

Learn more about it in this thread: Image
The Linux ls command typically displays basic or chunk of information about a file; however, what if you want to print more information about the file? This is where the stat command comes in.
stat (short for status) is a command-line utility for displaying detailed information about specific files or file systems. It is commonly used to obtain file timestamps.
How to use the stat command:

The stat command has a simple syntax which is similar to that of the ls command:

$ stat [OPTION]... [FILE]...

The stat command takes one or more FILE names as input and has a number of options that control the command's behavior and output.
With the stat syntax out of the way, let's take a look at our first example to display file status such as size, inode number links, and file timestamps:

$ stat log.txt

The command's output will look like this: Image
In this case, we ran the stat command with no options other than the file name, and it returned the following file information:

• File – The name of the file.

• Size – The size of the file in bytes.

• Blocks – The number of allocated blocks the file takes.
• IO Block – The size in bytes of every block.

• File type – (ex. regular file, directory, symbolic link, block file, sockets.)

• Device – Device number in hex and decimal.

• Inode – Inode number ( uniquely existing number for all the files in Linux).
• Links – Number of hard links.

• Access – File permissions in both numeric and symbolic modes.

• Uid – User ID and name of the owner .

• Gid – Group ID and name of the owner.

• Context – The SELinux security context.

• Access – The last time the file was accessed.
• Modify – The last time the file’s content was modified.

• Change – The last time the file’s attribute or content was changed.

• Birth – File creation time (some Linux distros may not support this, so you will probably see it blank).
Displaying filesystem status:

Instead of getting information about the file itself, use the -f option or the long format option --file-system to get information about the file system where the given file is located:

$ stat -f logt.txt
The command's output will look like this: Image
The stat command only displays less information when the -f option is used. In the preceding example, the following information was displayed:

• File - The name of the file.

• ID - File system ID in hexedecimal.

• Namelen (name legth) - Maximum length of file names.
• Fundamental block size - The size of each block on the file system.

• Blocks:
• Total - Number of total blocks in the file system
.
• Free - Number of free blocks in the file system.

• Available - Number of free blocks available to non-root users.
• Inodes:
• Total - Number of total inodes in the file system.
• Free - Number of free inodes in the file system.
Follow (dereference) symbolic links:

By default, the stat command does not follow symlinks. When you run it on a symlink, the output includes information about the symlink but not the file to which it points.

$ stat sml-chkf.sh
Image
To follow (dereference) the symlink and display information about the file to which it points, use the -L (short option format) or --dereference (long option format):

$ stat -L sml-chkf.sh
Image
Customizing the stat output:

The stat command has two options for customizing the output to your needs: -c short option for (--format="format"), and --printf="format".
The difference between these two options is that when two or more files are used as operants, --format adds a newline after the output of each operand. Backslash escapes are interpreted by the --printf option.
With --format and --printf, you can use a variety of format directives for files and file systems.

For example, to view only the file type, you would run:

$ stat --format="%F" log.txt Image
You can combine any number of formatting directives and use custom separators between them if you want. A single character or a string can be used as the separator:

$ stat --format="%n-%F" logs.txt
Here combined two formating directives an used the hiphen (-) as the separator Image
You can use the —printf option to interpret special characters such as newline or tab as separators:

$ stat --printf='Name: %n\nPermissions: %a\n' logs.txt

\n characters prints a new line. Image
The stat can also display data in a terse format. This format is useful for other utilities to parse.

To print the output in terse form, run the command with the -t (—terse) option:

$ stat -t logs.txt Image
Refer to the stat man pages using the (man stat) command for a complete list of all format directives for files and file systems. You can also refer to the stat help by running (stat —help) in your terminal.

$ stat --help Image
That's all for this thread! Thank you for getting this far. I hope you find this thread useful.
If you found this thread valuable:

1. Toss us a follow for more daily threads on Linux, sysadmin and devops → @linuxopsys

2. Like and RT the first tweet so other Linux folks can find it too.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Linuxopsys

Linuxopsys Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @linuxopsys

Jan 5
What exactly are bash exit codes in Linux?🤔

Learn more in this complete guide 🧵 ↓ Image
When writing Bash scripts, you will frequently need to stop the execution of a script when a certain condition is met or perform some actions based on a command's exit code.
In this thread I will go over the built-in bash exit command as well as the exit statuses of the commands that have been executed.
Read 14 tweets
Dec 29, 2023
In addition to UGO/RWX permissions and ACLs (Access Control Lists), Linux uses file and directory attributes to control the level of access that system programs and users have to files.

Let’s dive in! Image
File and directory attributes can be set and removed using different commands and utilities. Let's explore some commonly used attributes and how to manage them.
1. Immutable Attribute:

The immutable attribute prevents a file or directory from being modified, renamed, or deleted, even by the root user. It provides an extra layer of security to critical system files or sensitive data.
Read 23 tweets
Dec 28, 2023
When it comes to creating backups, packaging software source code for distribution, and managing files in Linux, the tar command is no doubt one of the widely used archiving utilities.

Learn more in this guide: Image
Tar command which is abbreviated as tape archive is used to group files into archives called tarballs and also compress files using popular compression algorithms such as gzip, bzip2, and xz.
Common Options

The picture below lists the most common options you can use with the tar command to perform different operations. Image
Read 16 tweets
Dec 20, 2023
pushd and popd are very underutilized commands, yet incredibly powerful.

These cmds give you the ability to manage your directory stack and easily switch between directories, making them a must-have tool for any Linux user looking to maximize their efficiency and productivity Image
In this thread, I'll show you how to use the pushd and popd commands to unlock the power of easy system directory navigation and streamline your workflow.
First let's understand what a Directory Stack is?

The Directory Stack is an ordered list of directories that have been accessed during navigation.

The contents of the Directory Stack can be viewed through the execution of the dirs command.
Read 29 tweets
Dec 18, 2023
If you want to learn Linux bash scripting for FREE, open this:

A collection of comprehensive bash scripting guides. Image
1. What is Shell in Linux

In this guide, let's learn about the shell which is an important part of the Linux operating system which interprets and passes the commands you provide on your keyboard to the operating system to perform some specific tasks

linuxopsys.com/topics/what-is…
Image
2. Bash variable assignment

A variable is a named storage location in a program's memory where a value can be stored, retrieved, and manipulated. Like any programming language, Variables are an essential component in the Bash scripts.

linuxopsys.com/topics/assign-…
Image
Read 56 tweets
Dec 14, 2023
If you want to learn Linux for FREE, open this (a thread of our Linux threads): Image
1. Permissions

The operating system controls file access in Linux by utilizing file permissions, attributes, and ownership. In Linux, file permissions, attributes, and ownership determine the level of access that system programs and users have to files.

2. paste command in Linux explained

(simple yet powerful way to merge and organize data from multiple text files):

Learn more about the paste command in this master guide:

Read 40 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(