Stat -- get more information than ls

Source: Internet
Author: User

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:

  1. $ Ls-l trythis
  2. -Rwx ------ 1 shs unixdweebs limit nov112013 trythis

Using the stat command, you will see the following:

  1. $ Stat trythis
  2. File: 'tryfoo'
  3. Size: 109 Blocks: 8 IO Block: 262144 regular file
  4. Device: 18 h/24d Inode: 12731691 Links: 1
  5. Access: (0700/-rwx ------) Uid: (263/shs) Gid: (100/unixdweebs)
  6. Access: 19:27:58. 000000000-0400
  7. Modify: 08:40:10. 000000000-0500
  8. 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.

  1. $ Mv trythis trythat
  2. $ Stat trythat
  3. File: 'trythat'
  4. Size: 109 Blocks: 8 IO Block: 262144 regular file
  5. Device: 18 h/24d Inode: 12731691 Links: 1
  6. Access: (0700/-rwx ------) Uid: (263/shs) Gid: (100/unixdweebs)
  7. Access: 19:27:58. 000000000-0400
  8. Modify: 08:40:10. 000000000-0500
  9. 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:

  1. $ Stat myfile *
  2. File: 'myfile'
  3. Size: 20 Blocks: 8 IO Block: 262144 regular file
  4. Device: 18 h/24d Inode: 12731803 Links: 1
  5. Access: (0640/-rw-r -----) Uid: (263/shs) Gid: (100/unixdweebs)
  6. Access: 03:00:36. 000000000-0400
  7. Modify: 12:02:12. 000000000-0400
  8. Change: 12:02:12. 000000000-0400
  9. File: 'myfile2'
  10. Size: 20 Blocks: 8 IO Block: 262144 regular file
  11. Device: 18 h/24d Inode: 12731806 Links: 1
  12. Access: (0640/-rw-r -----) Uid: (263/shs) Gid: (100/unixdweebs)
  13. Access: 03:00:36. 000000000-0400
  14. Modify: 12:03:30. 000000000-0400
  15. Change: 12:03:30. 000000000-0400
  16. File: 'myfile3'
  17. Size: 40 Blocks: 8 IO Block: 262144 regular file
  18. Device: 18 h/24 dInode: 12730533 Links: 1
  19. Access :( 0640/-rw-r -----) Uid :( 263/shs) Gid :( 100/unixdweebs)
  20. Access: 2014-08-2303: 00: 36.000000000-0400
  21. Modify: 2014-08-22:0:59. 000000000-0400
  22. 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 ).

  1. $ Ls-lu trythat
  2. -Rwx ------ 1 shs unixdweebs eclipsep919: 27 trythat
  3. $ Ls-lc trythat
  4. -Rwx ------ 1 shs unixdweebs eclipsep2112: 46 trythat

The stat command can also be used with folders.

In this example, we can see many links.

  1. $ Stat bin
  2. File: 'bin'
  3. Size: 12288 Blocks: 24 IO Block: 262144 directory
  4. Device: 18 h/24d Inode: 15089714 Links: 9
  5. Access: (0700/drwx ------) Uid: (263/shs) Gid: (100/unixdweebs)
  6. Access: 03:00:45. 000000000-0400
  7. Modify: 17:54:41. 000000000-0400
  8. Change: 17:54:41. 000000000-0400

Here, we can also view a file system.

  1. $ Stat-f/dev/cciss/c0d0p2
  2. File: "/dev/cciss/c0d0p2"
  3. ID: 0 Namelen: 255 Type: tmpfs
  4. Block size: 4096 Fundamental block size: 4096
  5. Blocks: Total: 259366 Free: 259337 Available: 259337
  6. 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.

  1. $ Stat -- format = % F trythat
  2. Regular file
  3. $ Stat -- format = % h trythat
  4. 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.

  1. $ Stat -- format = % a trythat
  2. 700
  3. $ Stat -- format = % A trythat
  4. -Rwx ------
  5. $ Stat -- format = % C trythat
  6. (Null)
  7. $ Stat -- format = % X bin
  8. 1411282845

All of the following are available options:

  1. % A indicates the access permission
  2. Access permission in % A readable format
  3. % B Number of allocated blocks (see % B)
  4. The number of bytes per block displayed by the % B % B Parameter
  5. Device number in % d decimal format
  6. % D device number in hexadecimal notation
  7. Raw mode in hexadecimal notation of % f
  8. % F file type
  9. Group ID of the % g owner
  10. Group Name of the % G owner
  11. % H hard connections
  12. % I Inode No.
  13. % N file name
  14. % N the name of the linked display file if it is a symbolic link
  15. % O I/O BLOCK SIZE
  16. The size of all bytes occupied by % s
  17. % T hexadecimal primary device number
  18. % T hexadecimal sub-device number
  19. % U owner user ID
  20. % U owner Username
  21. % X last access time
  22. % X last access time, number of seconds since Epoch
  23. % Y last modification time
  24. % Y last modification time, number of seconds since Epoch
  25. % Z last changed time
  26. % Z last modified time, number of seconds since Epoch

The following format options are available for file systems:

  1. % A common user available Blocks
  2. % B Total number of data blocks in the file system
  3. % C Total number of file nodes in the file system
  4. % D number of available File nodes in the file system
  5. % F number of available nodes in the file system
  6. Security Context of % C SELinux
  7. % I File System ID in hexadecimal notation
  8. % L Maximum File Name Length
  9. % N File System File Name
  10. % S block size (for faster transmission)
  11. % S basic block size (used for block count)
  12. % T file system type in hexadecimal notation
  13. 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:

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.