Shell script to open files based on date

I am responsible for couple of *nix servers including Linux, Solaris and HP-UX system. Some time I just wanna find out and open files based upon given date i.e. open file based on date criteria. For example find out all *.pl files accessed on 12-Dec-2004. Since there is not ready to use command exists I wrote script. You can use this script or alias to automate this procedure. You can find out file access date using ls -l command or using stat (display file or filesystem status including access date or time) command. Here is simple logic:

$ ls -l /path/to/file | awk ‘{ print $6 }’


Once file access date obtained, you can easily compare it using IF command in a shell script. Here is my small script:

FDATE=$(ls -l $FILE | awk '{ print $6 }')
if [ "$DATE" == "$FDATE" ];then
vi $FILE

You can run or use script as follows:

$ ./ 2006-11-22 myfile.txt

File myfile.txt will be only open using vi text editor, if last access date was 2006-11-22. Above script works but if you want to handle multiple files then you need to modify script as follows:

#date to match
# file, it can be wild cards too
# List of file to edit
# You can use $FLIST for other purpose too
for f in $FILE
FDATE=$(ls -l $f | awk '{ print $6 }')
if [ "$DATE" == "$FDATE" ];then

# just display list of files and let the use do
# whatever they wanna do with files
[ "$FLIST" != "" ] && echo $FLIST || echo "Sorry no files found to match $DATE date."

# Uncomment following if you wanna open all files using vi
#vi $FLIST

You can run script as follows:

$ ./ “2004-12-31” “*.pl”

Above script use for loop in shell script to match file one by one, if file date is matched it start to append file names to $FLIST variable. At the end, script displays all matched files. You can also use find command, but I prefer above script as it exactly matches date.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Ravi Aug 28, 2007 @ 15:03

    This website is Really good for Linux Beginners as well as Professionals

  • Joshua Adom Jul 7, 2009 @ 15:44

    This really good but I need help.
    I have written a backup script as follows

    echo "Taking Domain Backup..."
            echo "Tarring intellectprod"
            sleep 2
            cd /usr/weblogic/bea/user_projects/domains
            tar -cvf intellectprod_`date "+%d%b%Y"`.tar intellectprod
            echo "Zipping intellectprod"
    	sleep 2
            gzip intellectprod_`date "+%d%b%Y"`.tar
    	echo 	"Moving intellectprod_date.tar.gz to Backup folder"
    	sleep 2
    	mv intellectprod_`date "+%d%b%Y"`.tar.gz /usr/weblogic/Backup

    what I want to do now is to reverse the process in case there is a disaster.
    i want to be able to identfy the most current folder and use for restore.
    can you help?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum