Function Description: find (find) along the main file-level (directory) structure, traversing down, matching a matching document, can be attached to the appropriate action option, the default action is to print out the results meet the requirements of files and directories ( can also be specified by using -print, stressed that the command result is printed out, without this option may be).
The main functions are as follows (all tests have begun to match in the current directory (test), Search):
⓪: basic usage: Lists all files and subdirectories in a given directory (base_path) under: find base_path -print
supplement:
①: based on the file name and regular expression search, use the option -name or -iname (ignore case):
find base_path -name ‘xxx’ -print
find base_path -iname ’xxx‘ -print
②: negative parameters, you can use! To exclude the specified pattern.
Here you will print out all the files except txt text file.
③ depth directory-based search, find command specifies finished traversing all subdirectories. Use -maxdepth and -mindefth can limit the depth of the find command directory traversal, and find the default command does not search for symbolic links, you can change this behavior with the -L option.
-Maxdepth parameters for example 1, only the current directory matching.
-mindepth parameter represents the directory to start matching the shortest distance base_path.
④: search based on file type: You can specify a search using -type file types, linux / unix all that are treated as files (file types: regular files f, directory d, a symbolic link l, character device c, block equipment b, socket s, FIFO-p), we can use the -type option to filter the file type.
Here it will only match all the ordinary files in a particular item, and directories.
⑤: search based on the timestamp file: each file Linux / Unix file system there are three timestamps, access time (-atime), modification time (-mtime), change time (-ctime), the unit is the number of days , specified as an integer plus + before the number, that is greater than this time; plus -, this means less than a few days; without this represents just a few days.
此处的文件是我在进行截图之前才创建的,访问,修改,变化时间均小于一天。
当然相应的用分钟作为单位就可以用选项(-amin)(-mmin)(-cmin),如下我们测试修改时间
⑥:基于文件大小的搜索:find提供了指定文件大小的单位选项进而搜索符合大小文件的功能,这个搜索也常常会让用户感到非常舒服(b:块, c:字节, w:字, k:千字节, M:兆字节, G:吉字节)。
在搜索之前我们先用ls(list)指令来查看下当前目录下的文件信息:
信息的第五列就是各文件目录的大小(字节),我们通过指定匹配条件来搜索:
经过测试,在开始目录下,文件类型为普通目录,文件大小大于30个字节的文件就是zl.txt了
⑦:基于文件权限和所有权的匹配,-perm选项指定了find指匹配指定权限的文件,参数为文件对应的权限码。
我们仍然可参考⑥中的所有文件信息的第一列,此处需要掌握一定关于文件权限的知识。如下我们查找权限为644的普通文件,即用户可读写,组用户可读,其他可读。
也可以用选项-user,匹配指定用户所拥有的文件,参数为用户名或者UID
⑧利用find执行相应操作,比如删除文件,使用-delete选项;删除测试目录下所有的.txt普通文件
还可以利用-exec选项结合其他命令对文件进行更高效的操作,更改文件的所属权,复制文件等,find命令使用一对花括号{}代表文件名,对于每一个匹配到的文件,find命令会将{}替换成相应的文件名; 如果-exec的命令有多个参数时,需要注意结尾使用 " \; " 或者 "+",前者表示进行转义,不然系统会以为是find命令的结尾。
我们将测试目录下的所有的.txt文件由用户lihongbo转换到用户litao999,我们必须以root用户进行此操作,chown用于更改权限:
⑨:指定find跳过特定的目录:使用-prune选项可以跳过我们在搜寻的的一些明显我们不需要的目录
跳过了./test1目录
需要指出的是:选项出现的先后次序我们也应该考虑到内,因为它会影响到整条命令的执行效率。
提示:本文需要掌握关于ls(list), chown(changeowner)等命令,以及关于Linux/Unix文件系统文件类型和文件权限等知识。