Introduction to STAT command usage in Linux

Source: Internet
Author: User
Tags readable file permissions

The LS command may be one of the first commands that every Unix user learns, but it only shows a small portion of the information the Stat command can give.

The stat command obtains information from the index node of the file. As you may have learned, each file in each system has three sets of dates and times, including the most recent modification time (that is, the date and time that was displayed when the Ls-l command was used), the recent state change time (including renaming the file), and the most recent access time.

Using long list mode to view file information, you will see something like the following:

The code is as follows:

$ ls-l Trythis

-RWX------1 SHS unixdweebs 109 Nov 2013 Trythis

Using the stat command, you will see the following:

The code is as follows:

$ stat Trythis

File: ' Trythis '

size:109 blocks:8 IO block:262144 Regular file

device:18h/24d inode:12731691 links:1

Access: (0700/-rwx------) Uid: (263/SHS) Gid: (100/unixdweebs)

access:2014-09-09 19:27:58.000000000-0400

Modify:2013-11-11 08:40:10.000000000-0500

Change:2013-11-11 08:40:10.000000000-0500

In the above scenario, the file's state change and the date/time of the file modification are the same, while the access time is quite near. We can also see that the file uses 8 blocks, and the file permissions shown in both formats--eight (0700) and rwx format. The index node shown in the third row is 12731681. The file has no other hard links (links:1). Also, this file is a regular file.

Rename the file, and you'll see changes in the state change time.

The CTime information here was originally designed to store the creation date and time of the file, but later did not know when it became used to store the state modification (change) time.

The code is as follows:

$ mv Trythis Trythat

$ stat Trythat

File: ' Trythat '

size:109 blocks:8 IO block:262144 Regular file

device:18h/24d inode:12731691 links:1

Access: (0700/-rwx------) Uid: (263/SHS) Gid: (100/unixdweebs)

access:2014-09-09 19:27:58.000000000-0400

Modify:2013-11-11 08:40:10.000000000-0500

Change:2014-09-21 12:46:22.000000000-0400

Changing the permissions on the file also changes the CTime domain.

You can also match wildcards using the stat command to list the status of a set of files:

The code is as follows:

$ stat myfile*

File: ' MyFile '

Size:20 blocks:8 IO block:262144 Regular file

device:18h/24d inode:12731803 links:1

Access: (0640/-rw-r-----) Uid: (263/SHS) Gid: (100/unixdweebs)

Access:2014-08-23 03:00:36.000000000-0400

Modify:2014-08-22 12:02:12.000000000-0400

Change:2014-08-22 12:02:12.000000000-0400

File: ' Myfile2 '

Size:20 blocks:8 IO block:262144 Regular file

device:18h/24d inode:12731806 links:1

Access: (0640/-rw-r-----) Uid: (263/SHS) Gid: (100/unixdweebs)

Access:2014-08-23 03:00:36.000000000-0400

Modify:2014-08-22 12:03:30.000000000-0400

Change:2014-08-22 12:03:30.000000000-0400

File: ' Myfile3 '

size:40 blocks:8 IO block:262144 Regular file

device:18h/24d inode:12730533 links:1

Access: (0640/-rw-r-----) Uid: (263/SHS) Gid: (100/unixdweebs)

Access:2014-08-23 03:00:36.000000000-0400

Modify:2014-08-22 12:03:59.000000000-0400

Change:2014-08-22 12:03:59.000000000-0400

If we like, we can also use other commands to get this information.

Add the "U" option to the Ls-l command, and you will see the following results. Note that this option displays the last access time, and adding the "C" option will show the state change time (in this case, the time we renamed the file).

The code is as follows:

$ ls-lu Trythat

-RWX------1 SHS unixdweebs 109 Sep 9 19:27 Trythat

$ LS-LC Trythat

-RWX------1 SHS unixdweebs 109 Sep 12:46 Trythat

The stat command can also be applied to folders.

In this example, we can see that there are many links.

The code is as follows:

$ Stat Bin

File: ' Bin '

size:12288 blocks:24 IO block:262144 Directory

device:18h/24d inode:15089714 Links:9

Access: (0700/drwx------) Uid: (263/SHS) Gid: (100/unixdweebs)

Access:2014-09-21 03:00:45.000000000-0400

Modify:2014-09-15 17:54:41.000000000-0400

Change:2014-09-15 17:54:41.000000000-0400

Here, we can also look at a file system.

The code is as follows:

$ stat-f/DEV/CCISS/C0D0P2

File: "/dev/cciss/c0d0p2"

id:0 namelen:255 Type:tmpfs

Block Size:4096fundamental block size:4096

blocks:total:259366 free:259337 available:259337

inodes:total:223834 free:223531

Note the Namelen (filename length) field, and if the filename is longer than 255 characters, you will be lucky to see the heart symbol in the file name!

The stat command can also display all the information we want at once. In the following example, we only want to view the file type, and then the number of hard connections.

The code is as follows:

$ stat--format=%f Trythat

Regular file

$ stat--format=%h Trythat

1

In the following example, we looked at the file permissions--in two available formats--and then the SELinux security environment for the files. Finally, we can view the file access time in the number of seconds format starting with Epoch.

The code is as follows:

$ stat--format=%a Trythat

700

$ stat--format=%a Trythat

-RWX------

$ stat--format=%c Trythat

(NULL)

$ stat--format=%x Bin

1411282845

All of the following options are available:

Access rights represented by%a octal

Access rights in%A readable format

Number of blocks allocated by%b (see%B)

%B the number of bytes per block displayed by the%B parameter

Device number in%d decimal notation

%d Device number in hexadecimal notation

Raw mode with%f hexadecimal representation

%F file type

Group ID of the%g owner

Group name of the%G owner

%h Hard Connection number

%i Inode Number

%n file name

%N if it's a symbolic link, the file name that the monitor links to

%o I/O block size

Total bytes consumed by%s

%t 16-in-system main unit number

%T 16-in-system auxiliary unit number

%u owner's user ID

%u owner's user name

%x Last access time

%x last access time, number of seconds since Epoch

%y Last Modified Time

%Y last modified time, number of seconds since Epoch

%z last change of time

%Z last changed time, the number of seconds since Epoch started

There are also the following formatting options for the file system:

%a number of blocks available to ordinary users

%b The total number of data blocks in the file system

All file node points in%c file system

%d number of file nodes available for file system

%f the number of available nodes in the file system

Security context for%c SELinux

%i Hexadecimal file System ID

Maximum length of%l file name

filename of the%n file system

%s block size (for faster transport)

%s base block Size (for block count)

File system Type%t hexadecimal

File system types in%T readable format

This information is available, and the Stat command may help you understand your files in a slightly different way.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.