• home > OS > Linux > Devops >

    文件时间日期:–atime –ctime –mtime区别

    Author:zhoulujun Date:

    modification time(mtime):内容修改时间 status time(ctime):状态修改时间 access time(atime):最后访问时间,文件的 ctime、mtime、atime 之间有什么区别? ls显示出的time 是mtime,如何查看atime ctime?-mtime +n -n n参数区别?

    modification time(mtime):内容修改时间/status time(ctime):状态修改时间/access time(atime):最后访问时间。

    文件的 ctime、mtime、atime 之间有什么区别? 

    linux文件时间截点:atime、ctime与mtime

    • atime是指access time,即文件被读取或者执行的时间,修改文件是不会改变access time的

      Time whenfile data was last accessed. Changedby  the following  functions:  creat(),  mknod(),  pipe(),utime(2), and read(2).

    • mtime即modify time,指文件内容被修改的时间,是在写入文件时随文件内容的更改而更改的

      Time whendata was last modified. Changed bythe  fol- lowing  functions:  creat(),mknod(), pipe(), utime(), andwrite(2).

    • ctime即change time文件状态改变时间,是在写入文件、更改所有者、权限或链接设置时随 Inode 的内容更改而更改的

      Time whenfile status was last changed. Changed by the following  functions:  chmod(),  chown(),  creat(), link(2),  mknod(),  pipe(),  unlink(2),  utime(),  and write().

    各操作对三个时间参数的影响


    操作atimemtimectime
    mv没变没变变了
    cp变了没变没变
    touch

    变了

    变了变了
    cat/more/less

    变了

    没变没变
    ls没变没变没变
    chmod/chown没变没变变了
    ln没变没变变了
    echo 没变没变没变
    vi没变变了变了

    网上很多资料都声称cat、more等读取文件的命令会改变atime,但是我试验时却发现使用cat、more时atime没有被修改。这个问题需要另外做研究探讨。

    ls显示出的time 是mtime,如何查看atime ctime?

    linux查看文件ctime、atime、mtime命令

    • ls -lc test :查看test文件的ctime

    • ls -lu test :查看test文件的atime

    • ls -l test:查看test文件的mtime

    –mtime中的参数n

    –mtime n中的n指的是24*n(即n天), +n、-n、n分别表示:

    • +n:大于n,操作发在n+1天以前

    • -n:小于n,操作发生在n天以内

    •  n:等于n,操作刚好在n天时


    按日期批量移动文件 

    • find . –mtime n:  File waslast modified n*24 hours ago.

      最后一次修改发生在距离当前时间n*24小时至(n+1)*24 小时

    • find . –mtime +n:

    • 最后一次修改发生在n+1天以前,距离当前时间为(n+1)*24小时或者更早

    • find . –mtime –n:

    • 最后一次修改发生在n天以内,距离当前时间为n*24小时以内

    批量移动文件案例:

    find . -mtime -n 10  | xargs mv {} ../destinationPath

    Linux find按日期查找文件:如 查找可疑的木马文件

    • 查找:30天内被修改的文件

      find  ./  -mtime  -30  -type f  -exec ls -l  {} \;

    • 找到目录下所有的txt文件

      find ./ -name "*.txt" -print

    • 找到目录下所有的txt文件并删除

      find ./ -name "*.txt" -exec rm -rf {} \;

    • 找到目录下所有的php文件 并且在30天之类被修改的文件

      find  ./ -name "*.php" -mtime  -30  -typef  -exec  ls -l  {} \;

    • 找到目录下所有的php文件,同时,满足 30天以内,1天之前的

      find ./ -name "*.php" -mtime -30 -mtime +1 -type f -execls -l {} \;

    参考文章:

    Linux常用命令之 查找命令 find —— 细说 -atime,-mtime,-ctime https://www.cnblogs.com/qiaopei/p/5515189.html

    linux中ctime,mtime,atime的区别 https://www.cnblogs.com/PandoraX/p/4568879.html

    Find–atime –ctime –mtime的用法与区别总结 https://blog.csdn.net/abcdef0966/article/details/7607545




    转载本站文章《文件时间日期:–atime –ctime –mtime区别》,
    请注明出处:https://www.zhoulujun.cn/html/OS/Linux/LinuxDevops/8222.html