find 命令
1.基于文件名或者正则表达式搜索
例子:匹配多个条件 find . \( -name "*.txt" -o -name "*.pdf" \) -print 找到当前目录中所有txt pdf文件输出
加-o 表示OR条件 \( \)将中间部分视为一个整体
find . -regex ".*\( \.py\|\.sh\)$“ -regex 基于正则表达式匹配
2.基于目录深度搜索
用 -maxzdepth指定最大深度
3.根据文件类型搜索
用 - type对文件搜索进行过滤 -type f表示只搜索普通文件 l表示符号链接 d 目录 s 套接字
4.利用find执行命令或者动作
加上-exec选项 例如 find . -type f -user root -exec chown slynux {} \ {}将会被替换为所有匹配的文件名
可以结合printf 输出有用信息 例如 find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} \
将find出来的东西拷到另一个地方
find *.c -exec cp ‘{}’ /tmp ‘;’
5.find和xargs的结合使用
重命名find出的文件
find / -name a | xargs -i mv {} b
# {} 代表找到的文件
# -i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替。
来自: http://man.linuxde.net/xargs
来自: http://man.linuxde.net/xargs
find . -type f -name "*.txt" -print0 | xargs -0 rm -f 这样删除所有.txt 文件 xargs -0 将\0作为输入定界符
也可以加 -delete选项来删除 find . -type f -name "*.txt" -delete