Find command
Used to find files in the file tree, and make the appropriate treatment.
Format:
find pathname -options [-print -exec -ok ...]
Command parameters:
pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。 -print: find命令将匹配的文件输出到标准输出。 -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。 -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
Command options:
-name 按照文件名查找文件 -perm 按文件权限查找文件 (如果想查找不是这个权限的 : !-perm ) -user 按文件属主查找文件 -group 按照文件所属的组来查找文件。 -type 查找某一类型的文件,诸如: b - 块设备文件 d - 目录 c - 字符设备文件 l - 符号链接文件 p - 管道文件 f - 普通文件 -size n :[c] 查找文件长度为n块文件,带有c时表文件字节大小 -amin n 查找系统中最后N分钟访问的文件 -atime n 查找系统中最后n*24小时访问的文件 -cmin n 查找系统中最后N分钟被改变文件状态的文件 -ctime n 查找系统中最后n*24小时被改变文件状态的文件 -mmin n 查找系统中最后N分钟被改变文件数据的文件 -mtime n 查找系统中最后n*24小时被改变文件数据的文件 (用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。 ) -maxdepth n 最大查找目录深度 -prune 选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略 -newer 如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项
Example:
One:
(1) Find a modified file within 48 hours
find -atime -2
(2) Find ending in .log files in the current directory. The representative of the current directory
find ./ -name '*.log'
(3) Find / opt directory permissions to 777 files
find /opt -type f -perm 777 (文件 :-type f)
(4) Find / opt directory under the file permissions are not 777
find /opt -type f !-perm 777
(5) look for files larger than 1K of
find -size +1000c
Find the file is equal to 1000 characters
find -size 1000c
Find the file is equal to 1000 characters
find -size 1000c
two:
-exec behind parameters with a command command, its termination is based; to end the flags, so the sentence following the command semicolon is indispensable, taking into account the various systems semicolon have different meanings, it is preceded by backslash. {} Braces front of representatives find the file name to find out.
Example:
(5) Find change the time in the current directory before the 10th of files and delete them (No reminder)
find . -type f -mtime +10 -exec rm -f {} \;
(6) the current directory to find all files ending in .log, change file time in more than five days, and delete them, but to prompt before deleting. Press y to delete the file, press the delete key does n
find . -name '*.log' mtime +5 -ok -exec rm {} \;
Under (7) of the current directory to find the file name begins with passwd, content that contains "pkg" character file
find . -f -name 'passwd*' -exec grep "pkg" {} \;
(8) with the option to execute the cp command exec
find . -name '*.log' -exec cp {} test3 \;
three:
-xargs find the command to match the file transfer command to the xargs, and xargs command each get only part of the file, but not all, unlike the -exec option that. So that it can first deal with the first part of the file retrieved, and then the next batch, and so continue.
Example:
(9) common to find each file in the current directory, and the file type is determined to xargs
find . -type f -print | xargs file
(10) Find all of the current common js files ending and contains the 'editor' character directory
find . -type f -name "*.js" -exec grep -lF 'ueditor' {} \;
find -type f -name '*.js' | xargs grep -lF 'editor'
(11) use xargs execute the command mv
find . -name "*.log" | xargs -i mv {} test4
(12) Search hostnames word in all the ordinary files in the current directory with the grep command, and mark where the lines:
find . -name \*(转义) -type f -print | xargs grep -n 'hostnames'
(13) Find the current directory with a lowercase letter, and finally ended 4-9 plus .log file:
find . -name '[a-z]*[4-9].log' -print
(14) Find test4 not find a subdirectory in the test directory
find test -path 'test/test4' -prune -o -print
(15) Example 1: Change the time to find new but the old than the file log2012.log than the file log2017.log file
find -newer log2012.log ! -newer log2017.log