In this tutorial on Linux stat, 50 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.
Our fiftieth word, or command to memorize is
stat from our category
System.
stat allows you to show file or file
system status.
Common Linux stat Options
-options
description
--help
Print help screen
-f,
--file-system
Dispaly file system status
-c,
--format=FORMAT
Display file status according to FORMAT
Recall from video 7 on listing files with
ls, we saw lots of information, and
now we'll be able to see more, like multiple file timestamps.
Before we start, it helps to think of commands as mini programs and
most follow this structure:
command -option(s) argument(s).
The stat command has
7 options and the argument is the file or file system
name.
Like most commands, help is available with double-dash
--help. The
-f option reports on the file system,
and -c is for files. Optionally, you
can pick selected items using codes for each one.
So why is stat an important command?
Well, you'll often come across the need to dig a bit deeper into files
and drives.
And now you know how to do that.
Demonstration
Okay, the best way to embed this in your memory is by typing in your
own terminal window.
Find this on your Mac using a program called Terminal. On Linux use
Terminal or Konsole, and currently Microsoft is adding this
functionality to Windows.
Here we go. So we have this directory full of files we've created
along our journey, viewed simply with
ls -og.
$ ls -og
total 204
drwxr-xr-x 2 4096 Oct 14 19:53 stuff
-rw-r--r-- 1 176 Oct 4 18:14 video05.txt
-rw-r--r-- 1 100 Oct 10 18:42 video11.txt
-rw-r--r-- 1 0 Oct 9 21:16 video16.txt
-rw-r--r-- 1 45 Oct 12 10:11 video19.txt
-rw-r--r-- 1 149 Oct 12 16:10 video20.txt
lrwxrwxrwx 1 31 Oct 13 10:11 video21.txt -> ../ridiculouslylongfilename.txt
-rw-r--r-- 1 86 Oct 13 19:40 video23.txt
-rw-r--r-- 1 86 Oct 14 18:57 video24.txt
-rw-r--r-- 1 1203 Oct 15 13:17 video26.txt
-rw-r--r-- 1 1259 Oct 15 20:05 video27.txt
-rw-r--r-- 1 352 Oct 17 21:17 video31.txt
-rw-r--r-- 1 362 Oct 18 12:26 video32.txt
-rw-r--r-- 1 594 Oct 18 16:27 video33.txt
-rw-r--r-- 1 247 Oct 18 18:15 video34.txt
-rw-r--r-- 1 247 Oct 19 14:36 video35.txt
-rw-r--r-- 1 247 Oct 20 12:01 video36.txt
-rw-r--r-- 1 59 Oct 20 17:06 video37.txt
-rw-r--r-- 1 306 Oct 21 07:10 video38a.txt
-rw-r--r-- 1 245 Oct 21 07:23 video38b.txt
-rw-r--r-- 1 395 Oct 21 13:07 video38c.txt
-rw-r--r-- 1 149 Oct 22 13:27 video40.txt
-rw-r--r-- 1 145 Oct 24 14:19 video44.txt
-rw-r--r-- 1 2678 Oct 26 15:49 video48a.txt
-rw-r--r-- 1 114705 Oct 26 15:49 video48b.txt
$ clear
And then before we start, stat --help
shows you codes, identified with a percent
% sign. The codes I mentioned earlier.
$ stat --help
Usage: stat [OPTION]... FILE...
Display file or file system status.
Mandatory arguments to long options are mandatory for short options too.
-L, --dereference follow links
-f, --file-system display file system status instead of file status
-c --format=FORMAT use the specified FORMAT instead of the default;
output a newline after each use of FORMAT
--printf=FORMAT like --format, but interpret backslash escapes,
and do not output a mandatory trailing newline;
if you want a newline, include \n in FORMAT
-t, --terse print the information in terse form
--help display this help and exit
--version output version information and exit
The valid format sequences for files (without --file-system):
%a access rights in octal
%A access rights in human readable form
%b number of blocks allocated (see %B)
%B the size in bytes of each block reported by %b
%C SELinux security context string
%d device number in decimal
%D device number in hex
%f raw mode in hex
%F file type
%g group ID of owner
%G group name of owner
%h number of hard links
%i inode number
%m mount point
%n file name
%N quoted file name with dereference if symbolic link
%o optimal I/O transfer size hint
%s total size, in bytes
%t major device type in hex, for character/block device special files
%T minor device type in hex, for character/block device special files
%u user ID of owner
%U user name of owner
%w time of file birth, human-readable; - if unknown
%W time of file birth, seconds since Epoch; 0 if unknown
%x time of last access, human-readable
%X time of last access, seconds since Epoch
%y time of last data modification, human-readable
%Y time of last data modification, seconds since Epoch
%z time of last status change, human-readable
%Z time of last status change, seconds since Epoch
Valid format sequences for file systems:
%a free blocks available to non-superuser
%b total data blocks in file system
%c total file nodes in file system
%d free file nodes in file system
%f free blocks in file system
%i file system ID in hex
%l maximum length of filenames
%n file name
%s block size (for faster transfers)
%S fundamental block size (for block counts)
%t file system type in hex
%T file system type in human readable form
NOTE: your shell may have its own version of stat, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/stat>
or available locally via: info '(coreutils) stat invocation'
$ clear
Now, let's use stat to explore that
file I had you review for homework in
video48b.txt, and we'll do it without
options.
Okay now you know how to use stat.
And you know the syntax for commands, options and arguments.
One last tip about the stat command.
So please review the output options here. That's helpful. And look at
this! 50 video (tutorials) down, 50% of the way. Very nice!
Okay, thanks for visiting today. I hope this was a helpful
introduction to the stat command.
(Sorry about the low energy in this video, I created the
whole 100-video series, start to finish in 51 days, so I must have
been exhausted at the half-way mark. Or I skipped coffee that
morning. -Paul ;)
Learn More About The Series
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the
categories and a Q&A section, visit:
Linux Essentials provides quick summaries. We have more elaborate
content on our YouTube Channel, especially for aspiring data
scientists. So please join us and subscribe now.
For the Outline to all 100 tutorials, click Outline.
To go back to the unset command,
hit Back.
Support ad-free learning, click Tip.
To keep exploring the system with the
df command, click Next.