shell脚本中特殊筛选文件

问题描述:在写shell中,总会遇到一些各式各样筛选文件的需求,整理了一些特殊情况

 

1.查找目标文件下大于100Mb的文件

find $target_dir -type f -size +70M

 

2.查找目标文件下大于100Mb的文件,并显示详细信息

find $target_dir -type f -size +300M  -print0 | xargs -0 ls -ltrh

 

3.查找文件不缺分大小写

find $target_dir -iname "$file*.zip"

 

4.查找目标文件下所有的*.zip文件,并且排除掉几个目标*.zip文件

find $target_dir -iname "$file*.zip" -type f -mtime +0 -mtime -2 -not -name '*-test.zip' -not -name '*-mysql.zip' -not -name '*-information_schema.zip' -not -name '*-sys.zip'

 

5.查找目标文件下最新的目录

find $target_dir -type d -printf '%T@ %p\n' | sort -n | tail -n 1 | awk '{print $2}'

 

6.获取目标文件下最新的一个*.zip文件

ls -ltd $target_dir/* | grep -E all-database-.*zip | head -n1 | awk '{print $9}'

 

7.查找目标文件下7天前的*.zip文件

find $target_dir -iname "all-database-$file*.zip" -type f -mtime +0 -mtime -7

 

8.查找目标文件下的所有文件,目标层级为0,不向下递归

find $target_dir -path test -prune -o -maxdepth 0 -type d

 

posted @ 2023-05-31 17:12  我爱睡莲  阅读(120)  评论(0编辑  收藏  举报