更多 CentOS Linux 常用命令整理请点击这里。
查找:
从 ‘/’ 开始进入根文件系统查找文件和目录:
# find / -name file1
查找属于用户 ‘user1’ 的文件和目录:
# find / -user user1
在 /home 目录下查找以 .txt 结尾的文件:
# find /home -name "*.txt"
在 /home 目录下查找以 .txt 结尾的文件,但忽略大小写:
# find /home -iname "*.txt"
当前目录及子目录下查找所有以 .txt 和 .pdf 结尾的文件:
# find . \( -name "*.txt" -o -name "*.pdf" \) 或 # find . -name "*.txt" -o -name "*.pdf"
匹配文件路径或者文件:
# find /usr/ -path "*local*"
基于正则表达式匹配文件路径:
# find . -regex ".*\(\.txt\|\.pdf\)$"
基于正则表达式匹配文件路径,但忽略大小写:
# find . -iregex ".*\(\.txt\|\.pdf\)$"
否定参数:
找出 /home 下不是以 .txt 结尾的文件:
# find /home ! -name "*.txt"
根据文件类型进行搜索:
# find . -type 类型参数 类型参数列表: f 普通文件;l 符号连接;d 目录;c 字符设备;b 块设备;s 套接字;p Fifo 。
基于目录深度搜索:
向下最大深度限制为 3:
# find . -maxdepth 3 -type f
搜索出深度距离当前目录至少 2 个子目录的所有文件:
# find . -mindepth 2 -type f
根据文件时间戳进行搜索:
# find . -type f 时间戳 UNIX / Linux 文件系统每个文件都有三种时间戳: 1、访问时间(-atime/天,-amin/分钟):用户最近一次访问的时间; 2、修改时间(-mtime/天,-mmin/分钟):文件创建或者最后一次被修改的时间; 3、变化时间(-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改的时间。
查找当前目录下最近 7 天内被访问过的所有文件:
# find . -type f -atime -7
查找当前目录下恰好在 7 天前被访问过的所有文件:
# find . -type f -atime 7
查找当前目录下 7 天之前的被访问过的所有文件:
# find . -type f -atime +7
查找当前目录下访问时间超过 10 分钟的所有文件:
# find . -type f -amin +10
查找当前目录下比 file.log 修改时间更长的所有文件:
# find . -type f -newer file.log
查找当前目录下 14 天前创建或者被修改过的所有文件:
# find . -mtime +14
查找目录 /dir1/dir2 下 14 天前创建或者被修改过的所有文件:
# find /dir1/dir2 -mtime +14
查找目录 /dir1/dir2 下 14 天前创建或者被修改过的所有文件,但只列出文件个数:
# find /dir1/dir2 -mtime +14 | wc -l
查找目录 /usr/bin 下在 10 天内创建或者被修改过的所有文件:
# find /usr/bin -type f -mtime -10
根据时间范围进行搜索:
查找当前目录下 2018 年 7 月份的文件(201806302359 为 2018 年 6 月 30 日 23 点 59 分,201808010000 同理):
# touch -t 201806302359 ccie_lol_1 && touch -t 201808010000 ccie_lol_2 # find . -newer ccie_lol_1 ! -newer ccie_lol_2 | xargs ls -al # rm -f ccie_lol_1 ccie_lol_2
根据文件大小进行匹配:
# find . -type f -size 文件大小单元 文件大小单元: b —— 块(512字节); c —— 字节; w —— 字(2字节); k —— 千字节; M —— 兆字节; G —— 吉字节。
搜索大于 10 KB 的文件:
# find . -type f -size +10k
搜索小于 10 KB 的文件:
# find . -type f -size -10k
搜索等于 10 KB 的文件:
# find . -type f -size 10k
删除匹配文件:
删除当前目录下所有 .txt 文件:
# find . -type f -name "*.txt" -delete
(有关于文件和目录操作的更详细的内容请点击这里)
根据文件权限 / 所有权进行匹配:
当前目录下搜索出权限为 777 的文件:
# find . -type f -perm 777
找出当前目录下权限不是 644 的 php 文件:
# find . -type f -name "*.php" ! -perm 644
找出当前目录用户 tom 拥有的所有文件:
# find . -type f -user tom
找出当前目录用户组 sunk 拥有的所有文件:
# find . -type f -group sunk
借助 -exec 选项与其他命令结合使用:
找出当前目录下所有 root 的文件,并把所有权更改为用户 tom:
# find .-type f -user root -exec chown tom {} \;
上例中,{} 用于与 -exec 选项结合使用来匹配所有文件,然后会被替换为相应的文件名。
找出自己家目录下所有的 .txt 文件并删除:
find $HOME/. -name "*.txt" -ok rm {} \;
上例中,-ok 和 -exec 行为一样,不过它会给出提示,是否执行相应的操作。
查找当前目录下所有 .txt 文件并把他们拼接起来写入到 all.txt 文件中:
# find . -type f -name "*.txt" -exec cat {} \;> all.txt
在当前目录下将 30 天前创建或者被修改过的所有文件的详细信息列举出来:
# find . -mtime +30 -exec ls -lh {} \;
在当前目录下将 30 天前创建或者被修改过的 .log 文件复制到 old 目录中:
# find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
在当前目录下将 48 天前创建或者被修改过的所有文件移动到 /dir1/dir2 目录中:
# find . -mtime +48 -exec mv {} /dir1/dir2 \;
查找当前目录下 2018 年 7 月份的文件(201806302359 为 2018 年 6 月 30 日 23 点 59 分,201808010000 同理),并将找到的文件复制到当前目录的 old 目录下:
# touch -t 201806302359 ccie_lol_1 && touch -t 201808010000 ccie_lol_2 # mkdir old # find . -newer ccie_lol_1 ! -newer ccie_lol_2 -exec cp {} old \; # rm -f ccie_lol_1 ccie_lol_2 old/ccie_lol_2
(有关于文件和目录操作的更详细的内容请点击这里)
找出当前目录下所有 .txt 文件并以 “ File:文件名 ” 的形式打印出来:
# find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
因为单行命令中 -exec 参数中无法使用多个命令,以下方法可以实现在 -exec 之后接受多条命令:
# -exec ./text.sh {} \;
搜索但跳出指定的目录:
查找当前目录或者子目录下所有 .txt 文件,但是跳过子目录 sk:
# find . -path "./sk" -prune -o -name "*.txt" -print
在文件内容中查找:
在当前目录及其子目录所有 .c 和 .h 文件中查找 ‘expr’:
# find -name '*.[ch]' | xargs grep -E 'expr'
在当前目录及其子目录的常规文件中查找 ‘expr’:
# find -type f -print0 | xargs -r0 grep -F 'expr'
在当前目录中查找 ‘expr’:
# find -maxdepth 1 -type f | xargs grep -F 'expr'
其他:
在当前目录下列出所有长度为零的文件:
# find . -empty
寻找以 ‘.ps’ 结尾的文件,先运行 ‘updatedb’ 命令:
# locate \*.ps
发表评论?