Linux file status command

Linux stat command

Computer Hope

On Unix-like operating systems, the stat command displays the detailed status of a particular file or a file system.

This page describes the GNU/Linux version of stat.

Syntax

stat [OPTION]. FILE.

Options

-f, —filesystem display filesystem status instead of file status
-c, —format=FORMAT use the specified FORMAT instead of the default
-L, —dereference follow links
-Z, —context print the SELinux security context
-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 —filesystem):

%A Access rights in human readable form
%a Access rights in octal
%B The size in bytes of each block reported by ‘%b
%b Number of blocks allocated (see %B)
%C SELinux security context string
%D Device number in hex
%d Device number in decimal
%F File type
%f Raw mode in hex
%G Group name of owner
%g Group ID of owner
%h Number of hard links
%i Inode number
%N Quoted File name with dereference if symbolic link
%n File name
%o IO block size
%s Total size, in bytes
%T Minor device type in hex
%t Major device type in hex
%U Username of owner
%u User ID of owner
%X Time of last access as seconds since Epoch
%x Time of last access
%Y Time of last modification as seconds since Epoch
%y Time of last modification
%Z Time of last change as seconds since Epoch
%z Time of last change

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
%C SELinux security context string
%d Free file nodes in file system
%f Free blocks in file system
%i File System id in hex
%l Maximum length of file names
%n File name
%s Optimal transfer block size
%T Type in human readable form
%t Type in hex

Examples

Reports the status of file index.htm, displaying results similar to the following output:

File: `index.htm' Size: 17137 Blocks: 40 IO Block: 8192 regular file Device: 8h/8d Inode: 23161443 Links: 1 Access: (0644/-rw-r--r--) Uid: (17433/comphope) Gid: ( 32/ www) Access: 2007-04-03 09:20:18.000000000 -0600 Modify: 2007-04-01 23:13:05.000000000 -0600 Change: 2007-04-02 16:36:21.000000000 -0600

With the -f option, stat can return the status of an entire file system. Here, it returns the status of the first hard disk. Output resembles the following:

File: "/dev/sda" ID: 0 Namelen: 255 Type: tmpfs Block size: 4096 Fundamental block size: 4096 Blocks: Total: 2560 Free: 2560 Available: 2560 Inodes: Total: 126428 Free: 125966
stat --format "%A" /var/log/syslog

Display only the access restrictions, in human-readable form, of the system log /var/log/syslog. Output resembles the following:

Читайте также:  Запуск сервера майнкрафт на linux

. which indicates that the file is readable and writable by root, readable by the owning group (in this case the admin group), and not accessible at all by others.

ls — List the contents of a directory or directories.

Источник

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.

Читайте также:  Присвоить права файлу 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

Источник

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