打赏

星辰大海ゞ

That which does not kill us makes us stronger!

导航

find命令之时间戳使用示例

查看当前目录以及子目录下哪些文件占用的空间最大:

find  ./  -type  f  -exec du -m {} \; | sort -nr | head

find  ./  -type  f  -size  +1G    -exec  du -m {} \;  | sort -nr

find  ./  -type  f  -size +500M -exec  du -m {} \;  | sort -nr

------------------------------------------------------------------------------

# find /root -mindepth 1 

使用mindepth和maxdepth限定搜索指定目录的深度

mindepth 是指定最小的遍历深度. /root 本身的深度是0, 所以当最小遍历深度为1的时候正好排除 /root.

 使用 find /root/* 可以达到同样效果, 不过其实不是通过 find 本身, 而是由 bash 先把通配符展开为 /root 下所有的文件和文件夹再传给 find.

例如:在第二层子目录和第四层子目录之间查找passwd文件。

# find -mindepth 3 -maxdepth 5 -name passwd

-----------------------------------------------------------------------------

多参数时则要使用 -I

find ./ -mmin +60 -type f |xargs -i cp {} /root/test

------------------------------------------------------------------------------

 设计一个shell程序,在每天凌晨3点备份并压缩前一天/svn目录的所有内容,存放在/root/bak目录里,且文件名为如下形式: svn.2020.06.06.tar.gz,试写脚本。

 

 1 #!/bin/bash
 2 createtime=`date -d "1 day ago" +"%Y.%m.%d"`
 3 mytime1=`date -d "1 day ago" +"%Y%m%d0000"`
 4 mytime2=`date -d "1 day ago" +"%Y%m%d2359"`
 5 touch -t $mytime1 /root/bak/timestamp1
 6 touch -t $mytime2 /root/bak/timestamp2
 7 cd /svn ; find  -newer /root/bak/timestamp1 ! -newer /root/bak/timestamp2 |xargs tar -zcvf svn.$createtime.tar.gz
 8 mv /svn/svn.$createtime.tar.gz /root/bak/
 9 rm -f /root/bak/timestamp1
10 rm -f /root/bak/timestamp2

 

touch命令可以重新设定一个已存在的文件的时间    

-t  STAMP        使用[[CC]YY]MMDDhhmm[.ss] 格式的时间而非当前时间

例如:touch -t 201606060606 /tmp/haha.txt

 

参考资料:http://www.oschina.net/translate/15-practical-linux-find-command-examples

 

posted on 2016-01-12 17:21  星辰大海ゞ  阅读(1037)  评论(0编辑  收藏  举报