≡ Menu

stat command

Linux / Unix: Find Inode Of a File Command

How do I find out an inode (index-node) of a file under Unix like operating systems?
[click to continue…]

HowTo: Linux / Unix See File Size Command

I am a new Linux user. How do I find and list file sizes on Linux? How can I find out file size under Linux using bash shell/terminal prompt?
[click to continue…]

Shell Scripting: Check File Size ( Find File Size )

How do I find out file size under UNIX / Linux operating system and store the same to a variable called s?
[click to continue…]

Linux / UNIX: Create Large 1GB Binary Image File With dd Command

How do I create 1 GB or 10 GB image file instantly with dd command under UNIX / Linux / BSD operating systems using a shell prompt?
[click to continue…]

Find and Delete File If It Is More Than One Hour Old in UNIX Shell

Q. How do I find out file last modification time using a shell script or command? How do I delete or take any other custom action for all files more than one hour old in /home/ftp/incoming/raw/ directory?
[click to continue…]

Find the file permission without using ls -l command

Q. I need to find file permission and store the same to a shell variable. How do I find out the file permission without parsing ls -l output?

A. Use GNU stat command to display file or file system status. It has option to display output in specific format.

Display stat for /etc/passwd file

Type the following command:
$ stat /etc/passwd

  File: `/etc/passwd'
  Size: 1675            Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 7899368     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2007-12-05 08:20:31.000000000 +0530
Modify: 2007-12-04 23:36:50.000000000 +0530
Change: 2007-12-04 23:36:50.000000000 +0530

Print access rights in octal format:
$ stat -c %a /etc/passwd


Print access rights in human readable format, enter:
$ stat -c %A /etc/passwd


Store access rights in octal format to a shell variable:
$ VAR=$(stat -c %a /etc/passwd)
$ echo $VAR

Other valid format sequences for files

  • %b : Number of blocks allocated (see %B)
  • %B : The size in bytes of each block reported
  • %d : Device number in decimal
  • %C : SELinux security context
  • %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
  • %n : File name
  • %N : Quoted file name with dereference if symbolic link
  • %o : I/O block size
  • %s : Total size, in bytes
  • %t : Major device type in hex
  • %T : Minor device type in hex
  • %u : User ID of owner
  • %U : User name of owner
  • %x : Time of last access
  • %X : Time of last access as seconds since Epoch
  • %y : Time of last modification
  • %Y : Time of last modification as seconds since Epoch
  • %z : Time of last change
  • %Z : Time of last change as seconds since Epoch

Linux / UNIX: Display file inode (index number)

Q. How do I display file inode number using command line?

A. An inode is a data structure on a Unix / Linux file system. An inode stores basic information about a regular file, directory, or other file system object. You can use following two commands to display an inode:
[a] ls command : list directory contents

[b] stat command : display file or file system status

ls command

Type ls command with -i ( print the index number of each file ) option:
$ ls -i /etc/passwd

752010 /etc/passwd

752010 is index number (inode) for /etc/passwd file.

stat command

$ stat /etc/passwd

  File: `/etc/passwd'
  Size: 2026            Blocks: 8          IO Block: 4096   regular file
Device: 811h/2065d      Inode: 752010      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2007-08-20 23:19:20.000000000 +0530
Modify: 2007-07-07 00:06:56.000000000 +0530
Change: 2007-07-07 00:06:56.000000000 +0530

You can use inode number to delete or search a file.