I am writing a script to find the difference between file creation/modification times using a bash script. Running QNX I cannot use any common themed date functions that would make this easy. I am currently approaching this modifying the date from the ls command:
last=0
current=0
#ls -l /path/*.log | awk '{print $8}' | sed s/:/*60+/g | bc |
ls -l /path/*.log | awk '{print $8}' |
while read fname
do
current=$(fname | sed s/:/*60+/g | bc)
echo $current
echo $fname
if [ $last -gt 0 ]; then
echo "the difference is $($current - $last) minutes"
last=$current
else
last=$current
echo $fname
fi
done
the first commented ls produces what I need, the time in seconds, the while statement doesnot work though, not being able to find an integer based file. If I use the second ls command the sed will not modify the hh:mm based date and the difference won't work. Any ideas?
Few points to your query:
1. File creation timestamp is not stored in linux. Only accessed time, modified time, and changed time are stored for any file. [refer https://stackoverflow.com/a/79824/2331887 for the differences between them.]
2. Now, reconsider and decide which timestamp difference you need to find. One would be the latest modified time would be best to use for calculation based on fact whenever the content gets modified, and the other time for the difference would be ? [are you looking for difference between previous modified time and latest modified time]
Few points to your code:
1. 8th parameter of long listing file description would output
a) hh:mm for the files that are modified in current year and
b) yyyy instead of hh:mm for the files that were modified in previous years.
Hence, ambiguity would occur in your calculation.
Solution : You can use ls --time-style='+%d-%m-%Y %H:%M' -l *.log | awk '{print $7}'
for the case you want calculation based on hh*60+mm . This calculation further is not taking care of different dates.
Instead, for calculating time difference, I would recommend to use stat -c %Y *.log
[ which provides last modified time (in seconds) since Epoch (midnight 1/1/1970) ]
2. The bug in your code is very small. Change current=$(fname | sed s/:/*60+/g | bc);
with current=$(echo $fname | sed s/:/*60+/g | bc);
Please note, this would provide correct output, only and only if modified dates for the file are same.