👤
Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Subtract a file's modification date with current date

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 06-20-2018
Subtract a file's modification date with current date

SunOS -s 5.10 Generic_147440-04 sun4u sparc SUNW,SPARC-Enterprise

Hi,

In a folder, there are files. I have a script which reads the current date and subtract the modification date of each file.

How do I achieve this?

Regards,

Joe
# 2  
Old 06-20-2018
Convert either to epoch seconds and subtract those.
# 3  
Old 06-22-2018
Code:
for EachFile in $(/usr/bin/find ./ -maxdepth 1 -type f | /bin/sed "s/ /SPACEERRORHANDLER/;")
do
        CURRENTFILE=$(echo "$EachFile" | /bin/sed "s/SPACEERRORHANDLER/ /g;")
        if [ -f "$CURRENTFILE" ]
        then
                MODIFIED_DATE=$(/bin/stat --format="%y" "$CURRENTFILE")
                SECONDS_DIFFERENCE=''`echo "$(/bin/date "+%s") - $(/bin/date --date="$MODIFIED_DATE" "+%s")" | /usr/bin/bc -q`''
                MINUTE_DIFFERENCE=$(echo "scale=2;$SECONDS_DIFFERENCE / 60" | /usr/bin/bc -q)
                HOUR_DIFFERENCE=$(echo "scale=2;$MINUTE_DIFFERENCE / 60" | /usr/bin/bc -q)
                DAYS_DIFFERENCE=$(echo "scale=2;$HOUR_DIFFERENCE / 24" | /usr/bin/bc -q)
                APPROX_YEAR_DIFFERENCE=$(echo "scale=2;$DAYS_DIFFERENCE / 365" | /usr/bin/bc -q)
                echo "$CURRENTFILE SECONDS $SECONDS_DIFFERENCE MINUTE $MINUTE_DIFFERENCE HOUR $HOUR_DIFFERENCE DAYS $DAYS_DIFFERENCE APPROX_YYYY $APPROX_YEAR_DIFFERENCE"
                /bin/ls -lid "$CURRENTFILE"
        fi
done

Output:
Code:
./278960.sh SECONDS 33 MINUTE .55 HOUR 0 DAYS 0 APPROX_YYYY 0
3096224744023234 -rwxr-xr-x+ 1 murugesandins TES 1119 Jun 22 16:28 ./278960.sh
./mycareer.txt SECONDS 466661830 MINUTE 7777697.16 HOUR 129628.28 DAYS 5401.17 APPROX_YYYY 14.79
12384898975474861 -rw-r--r--+ 1 murugesandins TES 0 Sep  8  2003 ./mycareer.txt
./starting.txt SECONDS 466661831 MINUTE 7777697.18 HOUR 129628.28 DAYS 5401.17 APPROX_YYYY 14.79
208010007789334161 -rw-r--r--+ 1 murugesandins TES 0 Sep  8  2003 ./starting.txt
./testing.txt SECONDS 865 MINUTE 14.41 HOUR .24 DAYS .01 APPROX_YYYY 0
3096224744023218 -rw-r--r--+ 1 murugesandins TES 0 Jun 22 16:14 ./testing.txt

👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do you get the last modification date of a file? Nate18 UNIX for Dummies Questions & Answers 2 01-30-2013 10:35 PM
Adding hours and minutes to current date (Only to date not to time) manojgarg UNIX for Dummies Questions & Answers 4 08-22-2012 10:50 AM
Fetch date of 7 years back from current date in Perl parthmittal2007 Shell Programming and Scripting 3 04-12-2012 12:48 AM
Delete a row from a file if one column containing a date is greater than the current system date chumsky UNIX for Dummies Questions & Answers 4 07-06-2011 02:07 AM
Number of days between the current date and user defined date hiten.r.chauhan Shell Programming and Scripting 1 03-01-2011 09:13 AM
Date One Week Ago From Given Date, Not From Current Date Donkey25 Shell Programming and Scripting 4 11-10-2010 07:08 AM
how to get what date was 28 days ago of the current system date IN UNIX kandi.reddy Shell Programming and Scripting 15 03-19-2010 12:06 AM
Perl: Extracting date from file name and comparing with current date MKNENI Shell Programming and Scripting 4 03-26-2008 04:01 PM
File date vs Current date trexlim Shell Programming and Scripting 4 05-04-2007 09:35 PM
command for modification date of a file scampsd UNIX for Dummies Questions & Answers 5 12-28-2005 07:14 AM


All times are GMT -4. The time now is 08:12 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password