Linux find command usage Detailed

find command: Real-time finder, file system hierarchy is completed at the start by traversing the path of the file to find the specified
performance characteristics: 1, 2 look a bit slower, pinpoint 3, real-time look

用法:find [OPTIONS] [查找起始路径] [查找条件] -[处理动作]
    查找条件:
        根据文件从属关系查找:
            -user username:查找属主指定用户的所有文件
            -group groupname:查找属组指定组的所有文件
            -uid UID:搜索属主指定的UID的所有文件
            -group GID:查找属组指定的GID的所有文件
            -nouser:查找没有属主的文件
            -nogroup:查找没有属组的文件

        根据文件类型查找:
            -type TYPE
                f:普通文件
                d:目录文件
                l:符号链接文件
                b:块设备文件
                s:套接字文件
                c:字符设备文件
                p:管道文件

        组合测试:
            与:-a,默认组合逻辑
            或:-o
            非:-not ,  !

        根据文件的大小查找
            -size [+|-]num
            常用单位:k,m,G
                num:(num-1,num]
                -num:[0,num-1) 
                +num:(num,oo)
        注意:开闭开间

        根据时间戳查找:
            以“天”为单位
                -atime [+|-]num             访问时间
                -mtime [+|-]num               修改时间
                -ctime [+|-]num             改变时间
            以“分钟”为单位
                -amin [+|-]num
                -mmin [+|-]num
                -cmin [+|-]num

        根据权限查找
            -perm [+|/]mode
                mode:精确权限匹配
                /mode:任何一类用户(u,g,o)的权限中的任何一位(rwx)符合条件即满足
                    9位权限之间存在“或”关系
                -mode:每一类用户(u,g,o)的权限中每一位(r,w,x)符合条件即满足
                    9位权限之间存在“与”关系

    处理动作:
        -print:输出至标准输出,默认的动作
        -s:类似于对处理找到的文件执行“ls -l”命令,输出文件的详细信息             -fls /PATH/TO/SOMEFILE:把查找到的所有文件的长格式信息保存至指定文件中
        -ok COMMAND {} \; :对查找到的每个文件执行由COMMAND表示的命令
        -exec COMMAND {} \; :对查找到的每个文件执行由COMMAND表示的命令

            注意:find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,
                并一次性传递给后面的命令,但是有些命令不能接受过长的参数,此时命令执行会失败,
             另一种方式可规避此问题

Exercise:
1. Locate the / tmp directory under the main non-root all files

find /tmp/ ! -user root
find /tmp/ -not -user root

2. Identify the / tmp directory under the file name does not contain fstab file string

find /tmp/ ! -name 'fstab'
find /tmp/ -not -name 'fstab'

3. Locate the / tmp directory under the main non-root, and the file does not contain the string fstab

find /tmp/ ! -name 'fstab' -a ! -user root
find /tmp/ -not \( -name 'fstab' -o -user root \)

4. Find the / var directory subordinate to the main root, and is a group of all files or directories Python

find /var/ -user root -group python -ls

5. Find / all files or directories are not root, bin or under the usr directory Python, two ways

sudo find /tmp/recover/usr/ -not \( -user root -o -user bin -o -user python \)
sudo find /tmp/recover/usr/ -not -user root -o -user bin  -o -user python

6. Find inside / etc directory last week revised its content, and the owner is not the root user is not a Python users of all files or directories

sudo find /etc/ -mtime -7 -o -not \( -user root -o -user python \)

7. Look for the current owner, or belong to no group on the system, and was to be visited within the last week a file or directory

sudo find / -ctime -7 -a -nouser

All files larger than 1M 8. Look at the / etc directory and file type as an ordinary

sudo find /etc/ -size +1M  -type f

9. Find the file in the / etc directory of all users do not have write permission

sudo find /etc  -not -perm /222 -type f

10. Find / etc directory has at least file a class of user does not have permission to perform

sudo find /tmp/recover -perm /111 -a -not -perm -111 –type f

11. Find /etc/init.d directory, all users have execute permissions, and other users have write access to all files

sudo find /etc/init.d/ -perm -113  -type f

Guess you like

Origin blog.51cto.com/14192118/2415254