find命令
find语法:
常用参数:
-atime +n/-n:访问或执行时间大于/小于n天的文件
-ctime +n/-n:写入、更改inode属性(例如更改所有者、权限或者链接)时间大于/ 小于n天的文件
-mtime +n/-n:写入时间大于/小于n天的文件
-mmin +n/-n:更改时间大于/小于n分钟的文件
例如:
[root@localhost ~]# find /tmp/ -mtime -1
/tmp/
/tmp/yum.log
/tmp/.ICE-unix
/tmp/.Test-unix
/tmp/.X11-unix
/tmp/.XIM-unix
/tmp/.font-unix
/tmp/ks-script-JCNqAO
/tmp/4_6
# find /tmp/ -atime +10
# find /tmp/ -atime +1
-name filename 直接查找该文件名的文件
[root@Demo 4_6]# find . -name test2 //.表示当前目录,当前目录在/tmp/4_6下面
./dir2/test2
[root@Demo 4_6]# find . -name "test*" //支持通配符*,但是需要加“ ”
./test1
./dir2/test2
-type filetype 通过文件类型查找。文件类型有 f(普通文件), b(设备文件), c(字符设备文件), d(目录), l(符号链接文件), p(管道文件) 等
例如:
[root@localhost ~]# find /tmp/ -type d
/tmp/
/tmp/.ICE-unix
/tmp/.Test-unix
/tmp/.X11-unix
/tmp/.XIM-unix
/tmp/.font-unix
/tmp/4_6
[root@localhost ~]# find /tmp/ -type f
/tmp/yum.log
/tmp/ks-script-JCNqAO
-size [+n/-n] 根据大小查找(单位:c k M G),其中字节不是b,而是c
"+n"表示查找大小大于n块的文件
"-n"表示查找大小小于n块的文件
"nc"表示查找大小为n个字符的文件
例如:
[root@localhost ~]# find . size +10k
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
# find . -size -1M
# find . -size 4k
-perm mode 查找与给定权限匹配的文件,必须以八进制的形式给出访问权限
例如:
#find . -perm 777
-user username:查找指定用户名的文件
-group grpname:查找属于指定组的文件
-print:显示查找结果
-inum n:查找索引节点号为n的文件
其他用法:
1.忽略大小写文件名查找: # find /home -iname abc.txt
查找所有以.txt和.pdf结尾的文件 # find . ( -name “.txt" -o -name ".pdf” )
基于正则表达式匹配文件路径: # find . -regextype “posix-egrep” -regex “.*(.txt|.pdf)$”
基于正则表达式匹配文件路径(忽略大小写): # find . -regextype “posix-egrep” -iregex “.*(.txt|.pdf)$”
找出/home下不是以.txt结尾的文件:
# find /home ! -name "*.txt"
限定查找目录深度:
# find . -maxdepth 3 -type f
查找后再处理:
1)删除
find . -type f -name "*.gz" -mtime +30 -exec rm -f {} \;
find . -type f -name "*.gz" -mtime +30 -ok rm {} \;
find . -type f -name "*.gz" -mtime +30 |xargs rm -f
-ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。
2)改名
-exec command { } ;:对匹配指定条件的文件执行command命令
find . -name "*.txt" -exec mv {} {}.bak \;
find . -name "*.txt"|xargs -i mv {} {}.bak
# find .-type f -exec ls -l { } \;
//在当前目录下查找普通文件,并以长格式显示
# find /logs -type f -mtime 5 -exec rm { } \;
//在/logs目录中查找修改时间为5天以前的普通文件,并删除。保证/logs目录存在
# find /etc -name "*.conf"
//在/etc/目录下查找文件名以".conf"结尾的文件
find . -type f -perm 755 -exec ls { } \;
//在当前目录下查找权限为755的普通文件并显示