Thursday, 17 December 2015

How To Use Stat Command In Unix/Linux

stat command provides very handy switches which can be very helpful while writing shell scripts. for example you want to fetch access time of the file, you can easily find it out using %x switch likewise if you want to check the permission you can find it out using switch %a. There are many switches available, you can check them using man page. We will see the examples for some of the switches which will help us in day to day scripting.

%a     Access rights in octal
%s     Total size, in bytes
%x     Time of Birth 
%x     Time of last access
%y     Time of last modification
%z     Time of last change

$ stat abc.txt
Output:
  File: `abc.txt'
  Size: 21              Blocks: 8          IO Block: 4096   regular file
Device: fd0ch/64780d    Inode: 1785        Links: 1
Access: (0600/-rw-------)  Uid: (23081988/  dwetl)   Gid: (23081988/  dwetl)
Access: 2015-12-17 01:14:33.890227923 -0500
Modify: 2015-12-17 01:14:39.128002707 -0500
Change: 2015-12-17 01:14:39.128002707 -0500

 Birth: 2015-12-17 01:14:33.890227923 -0500


We have two syntax to use the flag to list out the required information. I have presented both, use whichever you comfortable with.

Find out the user who created the file

$ stat --format=%U abc.txt
$ stat -c %U abc.txt
Output:
dwetl

Find out the the permission of the file

$ stat --format=%a abc.txt
$ stat -c %a abc.txt
Output:
644

Find out the the size of the file (in byte)

$ stat --format=%s abc.txt
$ stat -c %s abc.txt
Output:
21

Find out the the Birth time of the file

$ stat --format=%w abc.txt
$ stat -c %w abc.txt
Output:
2015-12-17 01:14:33.890227923 -0500

Find out the the Access time of the file

$ stat --format=%x abc.txt
$ stat -c %x abc.txt
Output:
2015-12-17 01:14:33.890227923 -0500

Find out the the Modified time of the file

$ stat --format=%y abc.txt
$ stat -c %y abc.txt
Output:
2015-12-17 01:14:39.128002707 -0500

Find out the the Change/Update time of the file

$ stat --format=%z abc.txt
$ stat -c %z abc.txt
Output:
2015-12-17 01:14:39.128002707 -0500

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...