- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Checking if file is older than 1 week
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 10:07 AM
тАО10-12-2006 10:07 AM
Re: Checking if file is older than 1 week
By the way, all you need to do to create a file with a timestamp of your choosing is to 'touch' the file:
# touch -amt 2006100500.00 myfile
...would create "myfile" or update the timestamp of "myfile" to set its both its 'atime' and 'mtime' to October 5, 2006 at 00:00:00 hours. If you don't want to alter a timestamp of an existing file but only want to create one if it *doesn't* exist, add the '-c' swithch to the 'touch':
# touch -camt 2006100500.00 myfile
Look at the manpages for 'touch' for more information.
Regards!
...JRF...
- Tags:
- Touch
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 02:16 AM
тАО10-13-2006 02:16 AM
Re: Checking if file is older than 1 week
It occurs to me that your "concern" is having to create and cleanup a temporary file to which you can compare another file's age.
My first choice would be a one-line Perl snippet, but since you would like to keep things in shell only, consider something like this:
# cat .fileage
#!/usr/bin/sh
MYT=${1}
MYF=${2}
REF=/var/tmp/${0##*/}.${$}
if [ ${#} -ne 2 ]; then
echo "Usage: timestamp file"
exit 2
fi
[ ! -f ${MYF} ] && { echo "${MYF} doesn't exist"; exit 2; }
trap 'rm -f ${REF}' EXIT
touch -amt ${MYT} ${REF} || exit 1
if [ ${MYF} -nt ${REF} ]; then
echo "${MYF} is newer than ${MYT}"
else
echo "${MYF} isn't newer than ${MYT}"
fi
exit 0
...run, passnig a timestamp value and the name of a file for comparison of its age; e.g.:
# ./fileage 200510131001 /etc/hosts
/etc/hosts is newer than 200510131001
# .fileage 10131001 /etc/hosts
/etc/hosts isn't newer than 10131001
The script creates a reference file for comparison purposes, but uses a 'trap' to cleanup (remove) the file. This simplifies coding.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 04:21 AM
тАО10-13-2006 04:21 AM
Re: Checking if file is older than 1 week
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 04:22 AM
тАО10-13-2006 04:22 AM
Re: Checking if file is older than 1 week
Thanks, the ksh is really good stuff too, I will implement it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 04:53 AM
тАО10-13-2006 04:53 AM
Re: Checking if file is older than 1 week
OK, in Perl we have the '-M' file test operator to ascertain the number of days since modification. We also have '-A' and '-C' for access and inode change times, if you want.
Again, since your original question was about a shell script, and since I suggested that at the least you could add a snippet of Perl to the shell for your purposes, here's a different variation --- without any temporary files:
# cat .fileage2
#!/usr/bin/sh
MYT=${1}
MYF=${2}
if [ ${#} -ne 2 ]; then
echo "Usage: days file"
exit 2
fi
[ ! -f ${MYF} ] && { echo "${MYF} doesn't exist"; exit 2; }
R=`perl -e '$x=(@ARGV && -f $ARGV[1] && -M $ARGV[1] > $ARGV[0]) ? 1:0;print $x'
${MYT} ${MYF}`
if [ ${R} -eq 1 ]; then
echo "${MYF} is older than ${MYT} days"
else
echo "${MYF} is too young"
fi
exit 0
...run this one ...passnig the number of days and the name of a file for comparison of its age; e.g.:
# .fileage2 100 /etc/hosts
/etc/hosts is older than 100 days
To integrate the Perl code into the shell, I elected to pass two arguments to Perl; the number of days and the file's name. The code returns a one (1) or a zero (0) which can be captured by the shell and handled there.
Regards!
...JRF...
- « Previous
-
- 1
- 2
- Next »