linux finde exec 命令使用总结

find 命令是linux中使用频率很高的一个命令,用来查找文件。功能非常强大,同时参数也众多。比如可以按照名字、大小、权限、属于那个用户、那个组进行查找。这里给出一些示例。

  1. 直接查找当前目录的文件
#
# 查找当前目录的的 *.c 文件, 如果没有提示 No such file or directory
find *.c 
  1. 命令格式
    find pathname -options [-print -exec -ok]

  2. 常用的选项

    选项 含义
    -name 安装名字进行查找
    -perm 按照权限进行查找
    -prune 排除要查找的目录
    -user 属于指定用户的文件
    -group 属于指定组的文件
    -mtime -n (n那天内) +n(那天之前)
    -atime -n (n那天内) +n(那天之前)
    -ctime -n (n那天内) +n(那天之前)
    -nogorup 文件所属的组不在 /etc/groups 中
    -nouser 文件所属的组不在 /etc/passwd 中
    -newer file1 ! file2 更改的时间比 fiel1新,file2老
    -type [bdcplf] 指定类型
    -size n[cwkMG] 文件大小满足一定条件
    -depth 查找的深度
    -fstype 在指定的文件类中的系统中(/etc/fstab)
    -mount 查找的文件不跨越mount点

查找示例

#
# 根目录除去 /proc 目录,查找大于100M的文件
# ! -path 必须紧跟要查找的目录 
#  ! -print 必须使用,否则输出 proc  
find / -path /proc -prune -o   -size +100M  -print

# 同时忽略两个目录
find / -path /proc -prune -o -path /var -prune -o   -size +100M  -print
find / \( -path /proc -prune -o -path /var -prune \) -o   -size +100M  -print

# 使用-exec 
# 查找并移动文件, 当前目录下的c文件,移动到当前的c目录下
# 以下三条目录等价,注意对于特殊的符号 * 的处理
# {} 表示文件的名字 \; -exec 的结束
find *.c -exec mv {} c \;
find . -name "*.c" -exec mv {} c \;
find . -name \*.c -exec mv {} c -f \;

发表评论

邮箱地址不会被公开。