Linux command: find command Detailed

find command format

find path -option [-print] [-exec -ok |xargs |grep] [command {} \;]

# 参数说明
    path: find命令所查找的目录路径。
    		~ 表示$HOME目录;
    		.来表示当前目录;
    		/来表示系统根目录。
    		
  -print: find命令将匹配的文件输出到标准输出。
  
   -exec: find命令对匹配的文件执行该参数所给出的shell命令。
   			相应命令的形式为command {} ;,注意{};之间的空格。
   			
     -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

  |xargs: 与exec作用相同 ,起承接作用,区别在于|xargs 主要用于承接删除操作 ,而-exec都可用 如复制、移动、重命名等。

 options: 表示查找方式

find command parameters

path : Directory path you want to find.

  • ~ For $ HOME directory
  • It indicates the current directory
  • / Represents the root directory

print : Indicates the output to standard output.

exec : Shell command to execute the file matches the parameters given.

  • The space between; corresponding commands in the form of command {};, {} and attention.

ok : Exec action and the same, except that, before executing the command, will give prompt the user to confirm whether to execute.

|xargs : Exec the same effect, following a role play, except that | xargs mainly for receiving deletions, -exec are available such as copy, move, and rename.

options : Find a way to express.

options commonly used options:

-name filename        #查找名为filename的文件
-perm                 #按执行权限来查找
-user username        #按文件属主来查找
-group groupname      #按组来查找
-mtime -n +n          #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime -n +n          #按文件访问时间来查找文件,-n指n天以内,+n指n天以前
-ctime -n +n          #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-nogroup              #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser               #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-type b/d/c/p/l/f     #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size n[c]            #查长度为n块[或n字节]的文件
-mount                #查文件时不跨越文件系统mount点
-follow               #如果遇到符号链接文件,就跟踪链接所指的文件
-prune                #忽略某个目录

By following some simple examples to introduce under conventional usage of find:

1, Find by Name

	# 在当前目录及子目录中,查找大写字母开头的txt文件 
    $ find . -name '[A-Z]*.txt' -print   

    # 在/etc及其子目录中,查找host开头的文件 
    $ find /etc -name 'host*' -print   

    # 在$HOME目录及其子目录中,查找所有文件   
    $ find ~ -name '*' -print 

    # 在当前目录及子目录中,查找不是out开头的txt文件   
    $ find . -name "out*" -prune -o -name "*.txt" -print 

2, Search by directory

	# 在当前目录除aa之外的子目录内搜索 txt文件   
    $ find . -path "./aa" -prune -o -name "*.txt" -print   

    # 在当前目录及除aa和bb之外的子目录中查找txt文件   
    $ find . −path′./dir0′−o−path′./dir1′−path′./dir0′−o−path′./dir1′ -a -prune -o -name '*.txt' -print

	# 在当前目录,不再子目录中,查找txt文件 
    $ find . ! -name "." -type d -prune -o -type f -name "*.txt" -print 
	# 或者
	$ find . -name *.txt -type f -print

3. Find by permission

	# 在当前目录及子目录中,查找属主具有读写执行,其他具有读执行权限的文件   
    $ find . -perm 755 -print 

    # 查找用户有写权限或者组用户有写权限的文件或目录
    $ find ./ -perm /220
    $ find ./ -perm /u+w,g+w
    $ find ./ -perm /u=w,g=w

4. Find by type

	# 在当前目录及子目录下,查找符号链接文件   
    $ find . -type l -print

5, according to the owner and is a group

	# 查找属主是www的文件   
    $ find / -user www -type f -print   

    # 查找属主被删除的文件 
    $ find / -nouser -type f -print   

    # 查找属组 mysql 的文件 
    $ find / -group mysql -type f -print   

    # 查找用户组被删掉的文件 
    $ find / -nogroup -type f -print 

6. Find time

	# 查找2天内被更改过的文件 
    $ find . -mtime -2 -type f -print   

    # 查找2天前被更改过的文件 
    $ find . -mtime +2 -type f -print   

    # 查找一天内被访问的文件 
    $ find . -atime -1 -type f -print   

    # 查找一天前被访问的文件 
    $ find . -atime +1 -type f -print   

    # 查找一天内状态被改变的文件 
    $ find . -ctime -1 -type f -print   

    # 查找一天前状态被改变的文件 
    $ find . -ctime +1 -type f -print   

    # 查找10分钟以前状态被改变的文件 
    $ find . -cmin +10 -type f -print

7, according to documents old and new look

	# 查找比 aa.txt 新的文件 
    $ find . -newer "aa.txt" -type f -print   

    # 查找比 aa.txt 旧的文件 
    $ find . ! -newer "aa.txt" -type f -print   

    # 查找比aa.txt新,比bb.txt旧的文件 
    $ find . -newer 'aa.txt' ! -newer 'bb.txt' -type f -print

8. Find by size

	# 查找超过1M的文件 
    $ find / -size +1M -type f -print   

    # 查找等于6字节的文件 
    $ find . -size 6c -print   

    # 查找小于32k的文件 
    $ find . -size -32k -print
Published 100 original articles · won praise 45 · views 640 000 +

Guess you like

Origin blog.csdn.net/wangzhongshun/article/details/103197054