Wednesday, November 19, 2014

Shell Script To Read File Date – Last Access / Modification Time

A shell script to display file date in following format:

+ Time of last access
+ Time of last modification
+ Time of last change

Please note that UNIX / Linux filesystem never stores file creation date / time stamp.
This script use stat command to find out information about file date and time using custom field format.
  1. #!/bin/bash
  2. # Write a shell script to display / read file date / time in following format:
  3. # Time of last access
  4. # Time of last modification
  5. # Time of last change
  6. # -------------------------------------------------------------------------
  7. # Copyright (c) 2007 nixCraft project <http://cyberciti.biz/fb/>
  8. # This script is licensed under GNU GPL version 2.0 or above
  9. # -------------------------------------------------------------------------
  10. # This script is part of nixCraft shell script collection (NSSC)
  11. # Visit http://bash.cyberciti.biz/ for more information.
  12. # -------------------------------------------------------------------------
  13. FILE="$1"
  14.  
  15. # make sure we got file-name as command line argument
  16. if [ $# -eq 0 ]
  17. then
  18. echo "$0 file-name"
  19. exit 1
  20. fi
  21.  
  22. which stat > /dev/null
  23.  
  24. # make sure stat command is installed
  25. if [ $? -eq 1 ]
  26. then
  27. echo "stat command not found!"
  28. exit 2
  29. fi
  30.  
  31. # make sure file exists
  32. if [ ! -e $FILE ]
  33. then
  34. echo "$FILE not a file"
  35. exit 3
  36. fi
  37.  
  38. # use stat command to get info
  39. echo "Time of last access : $(stat -c %x $FILE)"
  40. echo "Time of last modification : $(stat -c %y $FILE)"
  41. echo "Time of last change : $(stat -c %z $FILE)"

0 comments: