简体   繁体   English

档案时间差异

[英]Difference in file time

I am writing a script to find the difference between file creation/modification times using a bash script. 我正在编写一个脚本,以使用bash脚本查找文件创建/修改时间之间的差异。 Running QNX I cannot use any common themed date functions that would make this easy. 运行QNX我无法使用任何通用的主题日期函数来简化这一过程。 I am currently approaching this modifying the date from the ls command: 我目前正在通过ls命令修改日期:

    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. 第一个被注释的ls产生了我所需要的东西,以秒为单位的时间,虽然while语句不起作用,但无法找到基于整数的文件。 If I use the second ls command the sed will not modify the hh:mm based date and the difference won't work. 如果我使用第二个ls命令,则sed将不会修改基于hh:mm的日期,并且两者之间的区别将不起作用。 Any ideas? 有任何想法吗?

Few points to your query: 要点您的查询:
1. File creation timestamp is not stored in linux. 1.文件创建时间戳未存储在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.] [请参阅https://stackoverflow.com/a/79824/2331887了解它们之间的区别。]
2. Now, reconsider and decide which timestamp difference you need to find. 2.现在,重新考虑并确定您需要查找哪个时间戳差异。 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 1. 长清单文件说明的第8个参数将输出
a) hh:mm for the files that are modified in current year and a) hh:mm用于当年修改的文件,以及
b) yyyy instead of hh:mm for the files that were modified in previous years. b)对于前几年修改过的文件,使用yyyy代替hh:mm
Hence, ambiguity would occur in your calculation. 因此,在您的计算中会出现歧义。

Solution : You can use ls --time-style='+%d-%m-%Y %H:%M' -l *.log | awk '{print $7}' 解决方案 :您可以使用ls --time-style='+%d-%m-%Y %H:%M' -l *.log | awk '{print $7}' ls --time-style='+%d-%m-%Y %H:%M' -l *.log | awk '{print $7}' for the case you want calculation based on hh*60+mm . ls --time-style='+%d-%m-%Y %H:%M' -l *.log | awk '{print $7}'对于要基于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) ] 相反,为了计算时差,我建议使用stat -c %Y *.log [它提供自Epoch (1970年1月1日午夜 )以来的最后修改时间( 以秒为单位 ]

2. The bug in your code is very small. 2.代码中的错误很小。 Change current=$(fname | sed s/:/*60+/g | bc); 更改current=$(fname | sed s/:/*60+/g | bc); with current=$(echo $fname | sed s/:/*60+/g | bc); 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. 请注意,仅在文件的修改日期相同的情况下,这才提供正确的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM