Execute shell commands using exec or OK When using find, you only need to write the desired operation in a file, you can use exec to search with find, which is very convenient.(In some operating systems, only the-e x e c option is allowed to execute commands such as l s or LS-l ). Most users use this option to find and delete old files. Before executing the r m command to delete files, we recommend that you use the l s command to check whether they are the files to be deleted. The E x e c option is followed by the command or script to be executed, followed by a pair of characters {}, a space, a/, and a semicolon. To use the e x e c option, you must use the p r I n t option at the same time. If you verify the f I n d command, you will find that this command only outputs the relative path and file name starting from the current path. For example, to use the LS-l command to list the matched files, you can place the LS-l command in the-e x e c option of the f I n d command. # Find.-Type F-exec LS-l {}/; -RW-r -- 1 Root 34928 2003-02-25./CONF/httpd. conf -RW-r -- 1 Root 12959 2003-02-25./CONF/magic -RW-r -- 1 Root 180 2003-02-25./CONF. d/readmeIn the above example, the f I n d command matches all common files in the current directory and uses the LS-l command in the-e x e c option to list them. In the/l o g s directory, find the files whose changes were earlier than 5 days and delete them: $ Find logs-type F-mtime + 5-exec RM {}/;Remember, you should check the corresponding file before deleting the file in s h e l in any way. Be careful! When using commands such as m V or r m, you can use the safe mode of the-e x e c option. It will prompt you before performing operations on each matching file. In the following example, the f I n d command searches for all file names in the current directory. l files whose names end with o g and whose modification time is more than 5 days, and delete them, only a prompt is given before deletion. $ Find.-Name "*. conf"-mtime + 5-OK RM {}/; <RM.../CONF/httpd. conf>? NPress Y to delete the file, and press n to not delete the file. Any form of command can be used in the-e x e c option. In the following example, we use the g r e p command. The f I n d command First matches all files named "passwd *", such as passwd, passwd. Old, and passwd. Bak. Run the grep command to check whether a SAM user exists in these files. # Find/etc-name "passwd *"-exec grep "Sam "{}/; SAM: X: 501: 501:/usr/SAM:/bin/bash |
Example of executing the script in-exec in the Forum Use the find command to find the shell from to in a time period. Create a script judgetime with the following content: Ls-L $ * | awk '{split ($8, hour, ":"); If (hour [1]> 23 | hour [1] <1) & hour [1] <24) print }' Go to the directory to be searched and run Find./-name "*"-exec judgetime {}/; Note that the time format is in the 24-hour format. To be accurate to minutes: Touch-T 04241112 starttemp # accurate to 12 minutes Touch-T 04241220 endtemp # as Find [dir]-newer starttemp-! -Newer endtemp-exec LS-l {}/;
Example of the find command You can use either of the following methods to find all files in the current user's home directory: $ Find $ home-print $ Find ~ -PrintIn order to have the read and write permissions for the file owner in the current directory and the users in the file group and other users who have the read permission, you can use: $ Find.-Type F-Perm 644-exec LS-l {}/;To find all common files with a length of 0 in the system and list their full paths, you can use: $ Find/-type F-size 0-exec LS-l {}/;Search for Common files that were modified seven days ago in the/var/logs directory and ask them before they are deleted: $ Find/var/logs-type F-mtime + 7-OK RM {}/;To find all the files in the root group in the system, you can use: $ Find.-Group root-exec LS-l {}/; -RW-r -- 1 Root 595 October 31 01:09./fie1The following find command will delete the admin. log file that contains the numeric suffix since the access time in the directory is 7 days. This command only checks three digits, so the suffix of the corresponding file should not exceed 999. Create several Admin. log * files before you can use the following command: $ Find.-Name "Admin. log [0-9] [0-9] [0-9]"-atime-7-OK RM {}/; <RM.../admin. log001>? N <RM.../admin. log002>? N <RM.../admin. log042>? N <RM.../admin. log942>? NTo find and sort all directories in the current file system, you can use: $ Find.-type D | sortTo find all r m T tape devices in the system, you can use: $ Find/dev/RMT-print Original book: To find and sort all directories in the current file system, you can use: $ Find.-type D-loacl-mount | sort Corrected: $ Find.-type D | sort |
|
Xargs When you use the-e x e c option of the f I n d command to process matched files, the f I n d command passes all matching files to e x e c for execution. However, some systems have limits on the length of commands that can be passed to e x e c. In this way, an overflow error occurs after the f I n d command is run for several minutes. The error message is usually "the parameter column is too long" or "parameter column overflow ". This is the use of the x a rg s command, especially used with the f I n d command. The f I n d command passes the matched file to the X A RG s command, while the X A RG s command only obtains part of the file, not all of it at a time, unlike the-e x e c option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this. In some systems, using the-e x e c option will initiate a corresponding process for processing each matching file, rather than executing all the matching files as parameters once; in this way, in some cases, there may be too many processes and the system performance may decline, resulting in low efficiency; The command x a rg s has only one process. In addition, when using the x a rg s command, whether to obtain all parameters at a time or obtain parameters in batches, and the number of parameters obtained each time is determined based on the options of the command and the corresponding adjustable parameters in the system kernel. Let's take a look at how the x a rg s command is used together with the f I n d command and give some examples. The following example finds every common file in the system, and then uses the x a rg s command to test which files they belong. # Find.-Type F-print | xargs File ./. KDE/autostart/Autorun. Desktop: UTF-8 Unicode English text ../. KDE/autostart/. Directory: ISO-8859 text/ ...... Search for the memory dump file (core dump) in the system and save the result to the/tmp/CORE. log file: $ Find/-name "core"-print | xargs echo "">/tmp/CORE. LogThe above execution is too slow. I changed it to search in the current directory. # Find.-Name "file *"-print | xargs echo "">/temp/CORE. Log # Cat/temp/CORE. Log ./File6 Search for all files in the current directory with read, write, and execution permissions, and revoke the corresponding write permissions: # Ls-l Drwxrwxrwx 2 Sam ADM 4096 October 30 20:14 file6 -Rwxrwxrwx 2 Sam ADM 0 October 31 01:01 http3.conf -Rwxrwxrwx 2 Sam ADM 0 01:01 httpd. conf# Find.-Perm-7-print | xargs chmod o-w # Ls-l Drwxrwxr-x 2 Sam ADM 4096 October 30 20:14 file6 -Rwxrwxr-x 2 Sam ADM 0 October 31 01:01 http3.conf -Rwxrwxr-x 2 Sam ADM 0 October 31 01:01 httpd. conf Use the g r e p command to search for the hostname word in all common files: # Find.-Type F-print | xargs grep "hostname" ./Httpd1.conf: # different IP addresses or hostnames and have them handled by ./Httpd1.conf: # virtualhost: if you want to maintain multiple domains/hostnames On your Use the g r e p command to search for the hostnames word in all common files in the current directory: # Find.-Name/*-type F-print | xargs grep "hostnames" ./Httpd1.conf: # different IP addresses or hostnames and have them handled by ./Httpd1.conf: # virtualhost: if you want to maintain multiple domains/hostnames On yourNote: In the above example,/is used to cancel the special meaning of * in s h e l in the f I n d command. |
|
To find and sort all directories in the current file system, you can use: [Code] $ find.-type D-print-local-mount | sort# Find.-type D | sort . ./Dir1 ./File6 ./. KDE ./. KDE/autostart ./SAM ./. Xemacs # Ls-l Total usage 52 -RW-r -- 1 Root 0 October 31 18:06 Admin. log001 -RW-r -- 1 Root 0 October 31 18:06 Admin. log002 -RW-r -- 1 Root 0 October 31 18:06 Admin. log042 -RW-r -- 1 Root 0 October 31 18:07 Admin. log942 Drwxr-XR-x 2 root Root 4096 October 31 20:26 dir1 -RW-r -- 1 Sam ADM 0 October 31 01:07 fiel Drwxrwxr-x 2 Sam ADM 4096 October 31 20:25 file6 -Rwxrwxr-x 2 Sam ADM 0 October 31 01:01 http3.conf -RW-r -- 1 Sam ADM 34890 October 31 00:57 httpd1.conf -Rwxrwxr-x 2 Sam ADM 0 October 31 01:01 httpd. conf Drwxrwxr-x 2 gem group 4096 October 26 19:48 Sam -RW-r -- 1 Root 2792 October 31 20:19 temp |
|
The f I n d command works with e x e c and X A RG s to allow users to execute almost all the commands on the matched files. The following is an example of some common parameters to find. You can check the useful parameters. For example, some parameters are used in the previous posts, you can also use man or view the find command manual on other posts in the Forum. 1. Use the name Option The file name option is the most common option for the f I n d command. You can either use this option independently or use it with other options. You can use a certain file name pattern to match the file. Remember to use quotation marks to cause the file name pattern. No matter what the current path is,If you want to find a file named *. t x T in your root directory $ h o m e, Use ~ As the 'P a t h n a m e parameter, the Tilde ~ Represents your $ h o m e directory. $ Find ~ -Name "*. txt"-print To find all '*. t x t' files in the current directory and subdirectory, you can use: $ Find.-Name "*. txt"-print To search for a file whose name starts with an uppercase letter in the current directory and subdirectory, you can use: $ Find.-Name "[A-Z] *"-print To search for files whose names start with h o s t in the/e t c directory, you can use: $ Find/etc-name "Host *"-print To find the files in the $ h o m e directory, you can use: $ Find ~ -Name "*"-print or find.-Print To make the system run at a high load, search for all the files from the root directory. $ Find/-name "*"-print If you want to search for a file name starting with two lower-case letters in the current directory, there will be two numbers followed *. t x T file, the following command returns the name a x 3 7. t x T files:$ $ Find.-Name "commana-z?a-z=%0--9%%0--9%.txt"-print 2. Use the perm Option Use the-P e r m option in File Permission mode. Find the file in File Permission mode. It is best to use the octal permission notation. For example, in the current directory, find a file with a permission of, that is, the file owner can read, write, and execute the file. Other users can read and execute the file. You can use: $ Find.-Perm 755-print There is also a way to express: Add a horizontal bar before the octal number to indicate that all matches. For example,-007 is equivalent to 777, and-006 is equivalent to 666. # Ls-l -Rwxrwxr-x 2 Sam ADM 0 October 31 01:01 http3.conf -RW-1 Sam ADM 34890 00:57 httpd1.conf -Rwxrwxr-x 2 Sam ADM 0 October 31 01:01 httpd. conf Drw-RW-2 gem group 4096 October 26 19:48 Sam -RW-1 Root 2792 October 31 20:19 temp# Find.-Perm 006 # Find.-Perm-006 ./SAM ./Httpd1.conf ./Temp 3. Ignore a directory If you want to ignore a directory when searching for a file because you know that there is no file in the directory, you can use the-P r u n e option to specify the directory to be ignored. Be careful when using the-P r u n e option, because if you use the-d e p t h option at the same time, the-P r u n e option is ignored by the f I n d command. If you want to search for files in the/a p s directory, but do not want to search for files in the/a p s/B I n directory, you can use: $ Find/apps-path "/apps/bin"-prune-o-print-In the perm option, my Parsing There is also a way to express: Add a horizontal bar before the octal number to indicate that all matches. For example,-007 is equivalent to 777, and-006 is equivalent to 666. |
|
Add one: how to avoid a file directory when searching for files using find For example, you need to find all files not in the dir1 subdirectory under the/usr/SAM directory. Find/usr/SAM-path "/usr/SAM/dir1"-prune-o-print Find [path...] [expression] After the path list is an expression -Path "/usr/SAM"-prune-o-print is-path "/usr/SAM"-a-prune-o-print short expressions are evaluated in order, -both A and-O are short-circuit values. They are similar to shell's & | if-path "/usr/SAM" is true, the values are-Prune, -prune returns true, which is true to the logical expression. Otherwise, the Value-Prune is not required, and the logical expression is false. If-path "/usr/SAM"-a-Prune is false, evaluate-print,-print to return true, or the logical expression is true; otherwise, do not request a value-print, or the logical expression is true. The special expression combination can be written If-path "/usr/SAM" then -Prune Else -Print Avoid Multiple folders Find/usr/SAM/(-path/usr/SAM/dir1-o-path/usr/SAM/file1/)-prune-o-printParentheses indicate the combination of expressions. /Indicates the reference, that is, it indicates that shell does not give a special explanation for the subsequent characters, but leaves it to the find command to explain its meaning. Search for a specific file, and add-name and other options after-o # Find/usr/SAM/(-path/usr/SAM/dir1-o-path/usr/SAM/file1/)-prune-o-name "Temp"-print |
|
4. Use the user and nouser options Search for files by file owner. For example, in the $ h o m e directory, you can use: $ Find ~ -User Sam-printFind the file whose owner is u c p in the/e t c directory: $ Find/etc-user uucp-printYou can use the-N o u s e r option to find files that have been deleted by the owner account. In this way, you can find files with no valid account in the/e t C/p a s w d file of the owner. When you use the-N o u s e r option, you do not need to give a user name. The f I n d command can complete the corresponding work for you. For example, if you want to find all such files in the/h o m e directory, you can use: $ Find/home-nouser-print 5. Use the group and nogroup options Like the u s e r and n o u s e r options, the f I n d command also has the same options for the user group to which the file belongs, to search for files belonging to the Gem user group in the/a p s directory, you can use: |
|
|