Sponsored Content
Top Forums Shell Programming and Scripting Subtract a file's modification date with current date Post 303019060 by murugesandins on Friday 22nd of June 2018 06:59:45 AM
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

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File date vs Current date

I am a newbie in shell scripting, hope you can advice how can I compare the date/time of file extracted from 'll' and current system date/time. I have done the following: ll -rt > $FILE_AGE_LOG FILE_DATETIME=`more $FILE_AGE_LOG | head -02 | cut -c 45-57` It returns 'May 4 19:11'. If I... (4 Replies)
Discussion started by: trexlim
4 Replies

2. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies

3. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

4. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

5. UNIX for Dummies Questions & Answers

How do you get the last modification date of a file?

I'm trying to get the date output to be in the form yyyy-mm-dd (e.g. 2013-01-18) !/bin/sh modDate=$(stat -c %y $1) echo $modDate >> $1 When I run this on another file (by typing ./dateScript theFile.txt), I keep getting this message: stat: illegal option -- c What's wrong with my code... (2 Replies)
Discussion started by: Nate18
2 Replies

6. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

7. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

8. UNIX for Beginners Questions & Answers

“sed” replace date in text file with current date

We want to call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ Code: line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to write a... (3 Replies)
Discussion started by: pradeepp
3 Replies

9. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies
GZEXE(1)						      General Commands Manual							  GZEXE(1)

NAME
gzexe - compress executable files in place SYNOPSIS
gzexe name ... DESCRIPTION
The gzexe utility allows you to compress executables in place and have them automatically uncompress and execute when you run them (at a penalty in performance). For example if you execute ``gzexe /usr/bin/gdb'' it will create the following two files: -rwxr-xr-x 1 root root 1026675 Jun 7 13:53 /usr/bin/gdb -rwxr-xr-x 1 root root 2304524 May 30 13:02 /usr/bin/gdb~ /usr/bin/gdb~ is the original file and /usr/bin/gdb is the self-uncompressing executable file. You can remove /usr/bin/gdb~ once you are sure that /usr/bin/gdb works properly. This utility is most useful on systems with very small disks. OPTIONS
-d Decompress the given executables instead of compressing them. SEE ALSO
gzip(1), znew(1), zmore(1), zcmp(1), zforce(1) CAVEATS
The compressed executable is a shell script. This may create some security holes. In particular, the compressed executable relies on the PATH environment variable to find gzip and some standard utilities (basename, chmod, ln, mkdir, mktemp, rm, sleep, and tail). BUGS
gzexe attempts to retain the original file attributes on the compressed executable, but you may have to fix them manually in some cases, using chmod or chown. GZEXE(1)
All times are GMT -4. The time now is 05:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy