Linux Find

1. Find old files and delete

-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。

-mtime   -n +n                #按文件更改时间来查找文件,-n指n天以内,+n指n天以前

-ctime    -n +n              #按文件创建时间来查找文件,-n指n天以内,+n指n天以前

-type    b/d/c/p/l/f         #查是块设备、目录、字符设备、管道、符号链接、普通文件

"find $HOME -type f -mtime +365 -exec ls -lh {} +" will give you the size and path of every file over 365 days old in your home directory

2. Find big files and delete

-size      n[c]               #查长度为n块[或n字节]的文件

"find $HOME -type f -size +2G -exec ls -lh {} +" will give you the size and path of every file over 2 GB in your home directory

3. Combine 1 & 2

"find $HOME -type f -size +2G -mtime +365 -exec ls -lh {} +"

4. delete specific files under $HOME

find $HOME -name 'test-file-*' | xargs rm -rf (use rm directly would fail if the file number is more than 20,000)

-iname will case-insensitive

Always go with the choice that scares you the most, because that's the one that is going to require the most from you!
原文地址:https://www.cnblogs.com/sanquanfeng/p/3046010.html