Linux find命令10种使用方法技巧分享(2)
8、基于文件权限和所有权的匹配 代码如下: [root@localhost tmp]# find . -type f -perm 644 #查找当前目录权限为644的文件 [root@localhost tmp]# find . -type f -user reed #查找当
8、基于文件权限和所有权的匹配
代码如下:
[root@localhost tmp]# find . -type f -perm 644
#查找当前目录权限为644的文件
[root@localhost tmp]# find . -type f -user reed
#查找当前目录文件所有者为reed的文件
#查找当前目录权限为644的文件
[root@localhost tmp]# find . -type f -user reed
#查找当前目录文件所有者为reed的文件
9、结合find 执行命令或动作
find命令可以借助选项-exec与其他命令进行结合。
代码如下:
[root@localhost tmp]# find . -type f -user reed -exec chown cathy {} \;
#将当前目录文件拥有者为reed的文件改为cathy
{ }是一个特殊的字符串,对于每一个匹配的文件,{ }会被替换成相应的文件名。
代码如下:
[root@localhost test]# find . -type f -mtime +10 -name "*.log" -exec cp {} /data/bk_log \;
#将当前目录大于10天的log文件复制到/data/bk_log目录
[root@localhost test]# find /tmp/test/ -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;
Text file: /tmp/test/File_6_.txt
Text file: /tmp/test/file_4_.txt
Text file: /tmp/test/data_3_.txt
Text file: /tmp/test/data_1_.txt
#列出目录的所有txt文件
#将当前目录大于10天的log文件复制到/data/bk_log目录
[root@localhost test]# find /tmp/test/ -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;
Text file: /tmp/test/File_6_.txt
Text file: /tmp/test/file_4_.txt
Text file: /tmp/test/data_3_.txt
Text file: /tmp/test/data_1_.txt
#列出目录的所有txt文件
10、跳过指定的目录
有时间我们查找时需要跳过一些子目录
代码如下:
[root@localhost test]# find . \( -name "jump_dir" -prune \) -o \( -type f -print \)
# \( -name "jump_dir" -prune \) 指定要跳过的子目录的名字
# \( -name "jump_dir" -prune \) 指定要跳过的子目录的名字
精彩图集
精彩文章