nixCraft Poll

Topics

Shell script to open files based on date

Posted by Vivek Gite [Last updated: February 11, 2006]

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 }'

2006-11-22

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

#!/bin/bash
DATE="$1"
FILE="$2"
FDATE=$(ls -l $FILE | awk '{ print $6 }')
if [ "$DATE" == "$FDATE" ];then
vi $FILE
fi

You can run or use script as follows:

$ ./script.sh 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:

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

# 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:

$ ./script.sh "2004-12-31" "*.pl"

getip.pl reboot.pl dnsupdate.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.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Ravi Says:

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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.