Stat -- get more information than ls
Get tired of ls commands and want to see more interesting information about your files? Try stat!
The ls command may be one of the first commands learned by every Unix user, but it only displays a small part of the information provided by the stat command.
The stat command gets information from the index node of the file. As you may already know, files in each system have three groups of dates and times, these include the last modification time (that is, the date and time displayed when the ls-l command is used), the last state change time (including renaming files), and the last access time.
To view the file information in the long list mode, you will see something similar to the following:
- $ Ls-l trythis
- -Rwx ------ 1 shs unixdweebs limit nov112013 trythis
Using the stat command, you will see the following:
- $ Stat trythis
- File: 'tryfoo'
- Size: 109 Blocks: 8 IO Block: 262144 regular file
- Device: 18 h/24d Inode: 12731691 Links: 1
- Access: (0700/-rwx ------) Uid: (263/shs) Gid: (100/unixdweebs)
- Access: 19:27:58. 000000000-0400
- Modify: 08:40:10. 000000000-0500
- Change: 08:40:10. 000000000-0500
In the above case, the file state change is the same as the file modification date/time, and the access time is quite near. We can also see that the file uses 8 blocks and the file permissions displayed in two formats-octal (0700) and rwx. The index node shown in the third row is 12731681. The file does not have any other hard Links (Links: 1 ). In addition, this file is a regular file.
Rename the file and you will see that the status change time has changed.
Here, the ctime information was first designed to store the date and time when the file was created (create), but later I don't know when it will become used to store the change time.
- $ Mv trythis trythat
- $ Stat trythat
- File: 'trythat'
- Size: 109 Blocks: 8 IO Block: 262144 regular file
- Device: 18 h/24d Inode: 12731691 Links: 1
- Access: (0700/-rwx ------) Uid: (263/shs) Gid: (100/unixdweebs)
- Access: 19:27:58. 000000000-0400
- Modify: 08:40:10. 000000000-0500
- Change: 12:46:22. 000000000-0400
Changing the File Permission also changes the ctime domain.
You can also use the stat command with wildcards to list the statuses of a group of files:
- $ Stat myfile *
- File: 'myfile'
- Size: 20 Blocks: 8 IO Block: 262144 regular file
- Device: 18 h/24d Inode: 12731803 Links: 1
- Access: (0640/-rw-r -----) Uid: (263/shs) Gid: (100/unixdweebs)
- Access: 03:00:36. 000000000-0400
- Modify: 12:02:12. 000000000-0400
- Change: 12:02:12. 000000000-0400
- File: 'myfile2'
- Size: 20 Blocks: 8 IO Block: 262144 regular file
- Device: 18 h/24d Inode: 12731806 Links: 1
- Access: (0640/-rw-r -----) Uid: (263/shs) Gid: (100/unixdweebs)
- Access: 03:00:36. 000000000-0400
- Modify: 12:03:30. 000000000-0400
- Change: 12:03:30. 000000000-0400
- File: 'myfile3'
- Size: 40 Blocks: 8 IO Block: 262144 regular file
- Device: 18 h/24 dInode: 12730533 Links: 1
- Access :( 0640/-rw-r -----) Uid :( 263/shs) Gid :( 100/unixdweebs)
- Access: 2014-08-2303: 00: 36.000000000-0400
- Modify: 2014-08-22:0:59. 000000000-0400
- Change: 2014-08-22:0:59. 000000000-0400
If we like it, we can also use other commands to obtain the information.
Add the "u" option to the ls-l command and you will see the following results. Note that this option will display the last access time, while adding the "c" option will display the status change time (in this example, the time when we renamed the file ).
- $ Ls-lu trythat
- -Rwx ------ 1 shs unixdweebs eclipsep919: 27 trythat
- $ Ls-lc trythat
- -Rwx ------ 1 shs unixdweebs eclipsep2112: 46 trythat
The stat command can also be used with folders.
In this example, we can see many links.
- $ Stat bin
- File: 'bin'
- Size: 12288 Blocks: 24 IO Block: 262144 directory
- Device: 18 h/24d Inode: 15089714 Links: 9
- Access: (0700/drwx ------) Uid: (263/shs) Gid: (100/unixdweebs)
- Access: 03:00:45. 000000000-0400
- Modify: 17:54:41. 000000000-0400
- Change: 17:54:41. 000000000-0400
Here, we can also view a file system.
- $ Stat-f/dev/cciss/c0d0p2
- File: "/dev/cciss/c0d0p2"
- ID: 0 Namelen: 255 Type: tmpfs
- Block size: 4096 Fundamental block size: 4096
- Blocks: Total: 259366 Free: 259337 Available: 259337
- Inodes: Total: 223834 Free: 223531
Note the Namelen (File Name Length) field. If the file name is longer than 255 characters, you will be lucky to see the heart-shaped symbol in the file name!
The stat command can also display all the information we want at one time. In the following example, we only want to view the file type and then the number of hard connections.
- $ Stat -- format = % F trythat
- Regular file
- $ Stat -- format = % h trythat
- 1
In the following example, we view the file permissions in two available formats: The SELinux security environment of the file. Finally, we can view the file access time in seconds from Epoch.
- $ Stat -- format = % a trythat
- 700
- $ Stat -- format = % A trythat
- -Rwx ------
- $ Stat -- format = % C trythat
- (Null)
- $ Stat -- format = % X bin
- 1411282845
All of the following are available options:
- % A indicates the access permission
- Access permission in % A readable format
- % B Number of allocated blocks (see % B)
- The number of bytes per block displayed by the % B % B Parameter
- Device number in % d decimal format
- % D device number in hexadecimal notation
- Raw mode in hexadecimal notation of % f
- % F file type
- Group ID of the % g owner
- Group Name of the % G owner
- % H hard connections
- % I Inode No.
- % N file name
- % N the name of the linked display file if it is a symbolic link
- % O I/O BLOCK SIZE
- The size of all bytes occupied by % s
- % T hexadecimal primary device number
- % T hexadecimal sub-device number
- % U owner user ID
- % U owner Username
- % X last access time
- % X last access time, number of seconds since Epoch
- % Y last modification time
- % Y last modification time, number of seconds since Epoch
- % Z last changed time
- % Z last modified time, number of seconds since Epoch
The following format options are available for file systems:
- % A common user available Blocks
- % B Total number of data blocks in the file system
- % C Total number of file nodes in the file system
- % D number of available File nodes in the file system
- % F number of available nodes in the file system
- Security Context of % C SELinux
- % I File System ID in hexadecimal notation
- % L Maximum File Name Length
- % N File System File Name
- % S block size (for faster transmission)
- % S basic block size (used for block count)
- % T file system type in hexadecimal notation
- File System Type in % T readable format
All of this information can be obtained. The stat command may help you understand your files from a slightly different angle.
10 Linux Interview Questions and Answers in different application scenarios
10 core Linux Interview Questions and answers
This article permanently updates the link address: