linux 文件的时间 以及find命令的相关应用

580阅读 0评论2014-08-20 fathermotherson
分类:系统运维

一、用stat命令查看文件的几个时间

点击(此处)折叠或打开

  1. # stat test
  2.   File: `test'
  3.   Size: 4 Blocks: 8 IO Block: 4096 regular file
  4. Device: fd00h/64768d    Inode: 783490 Links: 1
  5. Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
  6. Access: 2014-08-20 16:39:15.360223332 +0800
  7. Modify: 2014-08-20 16:40:04.771252003 +0800
  8. Change: 2014-08-20 16:40:18.071245939 +0800

三个时间:
atime  访问时间(读取
mtime 修改时间(内容data)ls -l看到的就是这个时间
ctime   状态改动时间(属性inode

在linux中stat函数中,用st_atime表示文件数据最近的存取时间(last accessed time);用st_mtime表示文件数据最近的修改时间(last modified time);使用st_ctime表示文件i节点数据最近的修改时间(last i-node's status changed time)。
 
 字段           说明                  例子           ls(-l)
 st_atime  文件数据的最后存取时间       read            -u
 st_mtime  文件数据的最后修改时间       write           缺省
 st_ctime  文件数据的最后更改时间       chown,chmod     -c

二、用find命令找出并删除一段时间前的文件
linux中的可以帮助我们找出某一时间点或内的文件,然后进行相关操作。

$man find 可以查看find命令的使用方法。
1. 找出 n 天前的文件
$find /temp/ -type f -mtime +n -print
注:/temp/ 指出寻找/temp/目录下的文件
-type f 指出找系统普通文件,不包含目录文件
-mtime +n 指出找 n*24 小时前的文件
-print 将找出的文件打印出来

例如:
找出 7 天前的文件

点击(此处)折叠或打开

  1. $find /temp/ -type f -mtime +7 -print

找出并删除7天前的文件

点击(此处)折叠或打开

  1. $find /temp/ -type f -mtime +7 -print -exec rm -f {} \;
注:-exec 指出要执行后面的系统命令
rm -f 删除找出的文件
{} 只有该符号能跟在命令后面
\ 结束符

也可以用xargs代替-exec

点击(此处)折叠或打开

  1. $find /temp/ -type f -mtime +7 -print | xargs rm -f

find 命令的用途举例

点击(此处)折叠或打开

  1. * 查找/var下最大的前10个文件:
  2. $ find /var -type f -ls | sort -k 7 -r -n | head -10

  3. * 查找/var/log/下大于5GB的文件:
  4. $ find /var/log/ -type f -size +5120M -exec ls -lh {} \;

  5. * 找出今天的所有文件并将它们拷贝到另一个目录:
  6. $ find /home/me/files -ctime 0 -print -exec cp {} /mnt/backup/{} \;

  7. * 找出所有一周前的临时文件并删除:
  8. $ find /temp/ -mtime +7-type f | xargs /bin/rm -f

  9. * 查找所有的mp3文件,并修改所有的大写字母为小写字母:
  10. $ find /home/me/music/ -type f -name *.mp3 -exec rename 'y/[A-Z]/[a-z]/' '{}' \;





参考:http://blog.csdn.net/rocky2com/article/details/5644096
上一篇:rpm包版本说明
下一篇:MySQL 独立表空间 VS 共享表空间