Stat in linux example

stat command in Linux with Examples

stat is a linux command line utility that displays a detailed information about a file or a file system. It retrieves information such as file type; access rights in octal and human-readable; SELinux security context string; time of file creation, last data modification time, and last accessed in both human-readable and in seconds since Epoch, and much more.

It has an option to specify a custom format instead of the default, for displaying information.

Options

The options for stat commands are:

-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 sequence for files are:

%a access rights in octal (note '#' and '0' printf flags) %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 %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

The valid FORMAT sequence for file-system are:

%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

Examples

1. When invoked without any options, stat command displays the following file information like — File name, file size, number of allocated blocks, blocksize, file type, device number, inode number, number of hard links, access file permissions in numiric or symbolic form, last time the file was accessed, last time the file’s content was modified, and last time the file’s attribute or content was changed.

$ stat sample.htm File: 'sample.htm' Size: 3496 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 6424759 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ expert) Gid: ( 1000/ expert) Access: 2021-05-21 18:29:56.264944616 +0530 Modify: 2021-05-05 17:04:27.680727000 +0530 Change: 2021-05-05 17:08:22.448449292 +0530 Birth: -

2. Use -f option to get the information about the file system on which the file resides.

$ stat -f sample.htm File: "sample.htm" ID: 27a574dcbaa73116 Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 36999792 Free: 6519334 Available: 4634074 Inodes: Total: 9412608 Free: 8940847

When invoked with -f option, it displays file sytem information like — File, file system ID in Hex format, maximum length of file names, type of file system, size of each block on the file system, number of total blocks on the file system, number of free blocks in the file system, number of free block available to non-root users, total number of inodes and number of free inodes in the file system.

Читайте также:  Displayport to hdmi linux

3. The stat command with -t option displays the information in a terse format.

$ stat --terse sample.htm sample.htm 3496 8 81b4 1000 1000 805 6424759 1 0 0 1621601996 1620214467 1620214702 0 4096

4. For files that are symbolic link, stat command would display information related with symbolic file, which means it would not follow the symbolic link. To follow symbolic link and show information about the linked file use -L option.

$ stat sample.html File: 'sample.html' -> 'sample.htm' Size: 10 Blocks: 0 IO Block: 4096 symbolic link Device: 805h/2053d Inode: 6424790 Links: 1 Access: (0777/lrwxrwxrwx) Uid: ( 1000/ expert) Gid: ( 1000/ expert) Access: 2021-05-22 14:39:39.656058591 +0530 Modify: 2021-05-22 14:39:39.652058596 +0530 Change: 2021-05-22 14:39:39.652058596 +0530 Birth: - $ stat -L sample.html File: 'sample.html' Size: 3496 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 6424759 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ expert) Gid: ( 1000/ expert) Access: 2021-05-21 18:29:56.264944616 +0530 Modify: 2021-05-05 17:04:27.680727000 +0530 Change: 2021-05-05 17:08:22.448449292 +0530 Birth: -

5. The stat command with -c option allows you to use a particular or custom format instead of the default, it prints a newline after each use of format sequence. Some examples are shown below.

$ stat -c=%A views.py =-rw-rw-r-- $ stat -c="%F" views.py =regular file $ stat -c="%n,%F" views.py =views.py,regular file $ stat -c='%a:%F' views.py =664:regular file $ stat -c=%d:%i views.py =2069:3806061 $ stat -c="%U,%F,%s" views.py =expert,regular file,1779

6. The stat command with -c option enables interpreting of backslash escapes sequences and turns off printing of a trailing newline. You need to use \n in the format to print a new line.

$ stat --printf='Name: %n\nPermissions: %a\n' apps.py Name: apps.py Permissions: 664 $ stat --printf='%U\n%G\n%C\n%z\n' apps.py expert expert stat: failed to get security context of 'apps.py': No data available ? 2021-04-17 06:15:26.011158935 +0530 $ stat --printf='%n\n%a\n%b\n' apps.py apps.py 664 8 $ stat --printf='%i\n' apps.py 3806053 $ stat --printf='%a:%u\n' apps.py 664:1000 $ stat --printf='%d:%i\n' apps.py 2069:3806053 $ stat --printf='Name: %n\nThe time of last data modification: %y\n' apps.py Name: apps.py The time of last data modification: 2021-04-17 06:15:26.011158935 +0530 $ stat --printf="%A %U %s" apps.py -rw-rw-r-- expert 142$ $ stat --printf="%A %U %s\n" apps.py -rw-rw-r-- expert 142

Источник

Читайте также:  Realtek audio driver linux установка

Landoflinux

The following are examples of how to use the Linux «stat» command to display detailed information relating to files and file systems.

Linux stat command examples

The Linux stat command is a tool that can be used for displaying detailed information relating to a file or file system on a Linux system. By default the stat command should be available on most distributions of Linux. In the examples below, a CentOS Linux distribution was used.

Linux Stat Command Syntax

The basic syntax of the «statstrong>» command is as follows:

 stat [OPTION]. FILE. DESCRIPTION Display file or file system status. -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 new‐ line after each use of FORMAT --printf=FORMAT like --format, but interpret backslash escapes, and do not out‐ put 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 stat - no parameters passed 

The simplest use of the «stat» command is to pass the name of a file to the command. In the example below you can see the default output from the stat command. Basic information such as size, file type, Inode information, Number of links, Access, Modification and Change date and time stamps are displayed. The stat command is often used to view Modification/access times on a file. (A more detailed way of displaying creation time and access time stamps on a file is to use the debugfs command)

 # stat anaconda-ks.cfg File: ‘anaconda-ks.cfg’ Size: 1967 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 135 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0 Access: 2015-11-21 16:52:25.992046601 +0000 Modify: 2015-11-21 16:52:25.998046601 +0000 Change: 2015-11-21 16:52:25.998046601 +0000 Birth: - 

stat -f — Displaying File System Status

If you wish to view information that relates to a file system and not an individual file, you may pass the «-f» parameter followed by a filesystem:

 # stat -f /opt File: "/opt" ID: fd0600000000 Namelen: 255 Type: xfs Block size: 4096 Fundamental block size: 4096 Blocks: Total: 509440 Free: 501208 Available: 501208 Inodes: Total: 2048000 Free: 2047997 

Format Sequences

To display specific information relating to a file or file system, you may use what is known as a format sequence. By passing the parameter «-c» or «—format» for files and «-f» or «—file-system» for file systems. You can specify individual points of interest such as inode number, mount point etc.. A list of all the parameters that can be passed for files and file systems is listed at the bottom of the page.

stat —format=%i FileName

As an example we are specifying that we would like to display the inode information of the specified file.

 # stat --format=%i anaconda-ks.cfg 135 

From the above we can see that the inode number for the file «anaconda-ks.cfg» is «135«.

Читайте также:  Linux find execute file

stat —format=%i%g%G FileName

In this example we are specifying multiple format sequences that we would like to display.

 # stat --format=%i%g%G anaconda-ks.cfg 1350root 

In the above example, multiple parameters were passed to the «—format» option. An explanation of these is listed below:

inode number: %i
group ID of owner: %g
group name of owner: %G
file name: anaconda-ks.cfg

The output from the file indicates that the inode number is «135«, the group ID of the owner is «0» and the group name of the owner is «root«.

stat -f -c %a /opt

The above example of the stat command is used against a file system. In this example, we use the «-f» option indicating that we are passing a file system. The «-c» flag is used to specify the format sequence.

%a — free blocks available to non-superuser

 # stat -f -c %a /opt 501208 

For a full list of parameters that can be used against a file system, see the list below.

Format Sequences for files

The following format sequences are for use with files:

 %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 modification, human-readable %Y time of last modification, seconds since Epoch %z time of last change, human-readable %Z time of last change, seconds since Epoch 

Format Sequences for File Systems

The following format sequences are for use with 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 

Источник

Оцените статью
Adblock
detector