One, find command (search file) 1, the main function of the Find command is to thoroughly check the tree directory hierarchy.
2. The general format of the Find command:
Find Pathname-expressions
Second, find common expressions 1, there are three expressions, option expressions (not commonly used), conditional expressions and action expressions
-mount |
option expression, The search scope to prevent the find command from exceeding the bounds of the current file system. The return value is often true. |
-group grp |
conditional expression, Checks whether the current file has the same GID or group name as the GRP. Returns true if the two are identical, otherwise false values are returned. |
-name pattern |
conditional expression, Check to see if the file name is the same as pattern. Pattern can be given using regular expressions. Use quotation marks when necessary. When the file name is consistent with pattern. Returns the truth value. Otherwise it is false. |
-type t |
conditional expression, check if the current file type is T. For the table of contents, the T value can be d. For ordinary files, the T value can be f, the connection is said, T value can be L, etc. Returns true if the current file type is T, otherwise false. |
-user usr |
conditional expression, Checks whether the owner or UID of the current file is USR. If the two are consistent, return the true value, otherwise false. |
-exec cmd |
action expression, used to execute the cmd command. If you want to pass the current file name to the command, you should add the {} token, which is used to denote the end of the CMD and separate from the expression that may appear later. If the cmd command is executed successfully, the true value is returned, otherwise false. |
-print |
An action expression that sends the current file name to the standard output device display, and the return value is often true. |
-mtime-n +n #按文件更改时间来查找文件,-n means n days or less, +n means n days ago-ctime-n +n #按文件创建时间来查找文件,-n refers to n days or less, +n means n days ago:-mmin-n #n天以内更改时间
2, example (1)/usr/lib # Find. -name ' bin* ' (equivalent to # Find. -name ' bin* '-print)
./initramfs-tools/bin
./guile-1.8/bin
./firefox/distribution/searchplugins/locale/en-us/bing.xml
./firefox/distribution/searchplugins/locale/zh-cn/bing.xml
./firefox/distribution/searchplugins/locale/en-gb/bing.xml
./firefox/distribution/searchplugins/locale/en-za/bing.xml
./pm-utils/bin
./klibc/bin
./ure/lib/binaryurp.uno.so
./ure/bin
./thunderbird/distribution/searchplugins/locale/en-us/bing.xml
./python2.7/binhex.pyc
./python2.7/binhex.py
(2) # Find. -name ' bin* '-type d-print (Filter by file name first, then filter by file type)
./initramfs-tools/bin
./guile-1.8/bin
./pm-utils/bin
./klibc/bin
./ure/bin
(3) # Find. -name ' *.bak '-type f-exec rm {} \; -print(The-exec executes the cmd command, {} means that the current file is used as the transfer command,\; represents RM'sEnd)Note: {} to "space" and enter "\;", otherwise an error
The following prints out the deleted information:./test3.bak./test.bak
./test2.bak
Third, find logical expressions
logical expression |
description |
e1-a E2 |
E1 evaluated only when E2 is true |
e1 E2 |
ibid. |
e1-o E2 |
E1 evaluated only when E2 is false |
e1,e2 |
evaluates two expressions, E1 first, E2 |
!e1 |
The result is true when E1 is false, and vice versa. |
(1) The current directory has A.bak b.bak# find. -name ' *.bak '-type f-exec echo {} \; -print./a.bak
./a.bak
./b.bak
./b.bak
Iv. Example of Find1. Find the Linux folder in the current directory that contains the named "a "The line number of the "B" character exists in the file# find/linux/-name A | Xargs grep-n "B"./linux/a:1:aaabbb
./linux/test/a:1:aaabbb
2. Otherfind.-user root-printfind.-type f-mtime-4–print (lists files that have been modified in the last four days)
find.-size +5000k-exec ls-lh {} \; (prints files larger than 5000k)
Find. \ (-name "[aa]*"-o-name "*[aa]" \)-print (prints files that start with AA or End with AA)
V. Thinking 1, statistics in the directory tree starting at/etc/, contains the string "Telnet", (case-insensitive) the number of files. Would you please write this order?
(1) Method one: With-exec first step: # find/etc/-type f-exec grep-il "telnet" {} \;
/etc/bash_completion
/etc/bash_completion.d/qemu
/etc/services Second Step: # find/etc/-type f-exec grep-il "telnet" {} \; | Wc-l
3
(2) Method two: with the pipeline# find/etc/-type F |grep-il "Telnet"(Error, indicates whether the file name contains Telnet)
# find/etc/-type F | Xargs grep-il "Telnet"
/etc/bash_completion
/etc/bash_completion.d/qemu
/etc/services
# find/etc/-type F | Xargs grep-il "Telnet" | Wc-l
3
2. Does the file contain a string? You can use the $ strings command
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Learn Linux_10_find from scratch