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.

#!/bin/bash
# Write a shell script to display / read file date / time in following format:
# Time of last access
# Time of last modification
# Time of last change
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
FILE="$1"
 
# make sure we got file-name as command line argument 
if [ $# -eq 0 ]
then
	echo "$0 file-name"
	exit 1
fi
 
which stat > /dev/null
 
# make sure stat command is installed 
if [ $? -eq 1 ]
then
	echo "stat command not found!"
	exit 2
fi
 
# make sure file exists 
if [ ! -e $FILE ]
then
	echo "$FILE not a file"
	exit 3
fi
 
# use stat command to get info
echo "Time of last access : $(stat -c %x $FILE)"
echo "Time of last modification : $(stat -c %y $FILE)"
echo "Time of last change : $(stat -c %z $FILE)"
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Chris F.A. Johnson Jun 22, 2009 @ 1:30

    This script contains three calls to stat (when one would do) all of which will fail if the filename contains spaces.

    It will also fail if the version of stat takes a different syntax (as the *BSD version does).

    stat -c ‘Time of last access : %x
    Time of last modification : %y
    Time of last change : %z’ “$file”

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.