Linux Find command detailed

Source: Internet
Author: User
Tags uppercase letter

one, find command format 1. The general form of the Find command is;find Pathname-options [-print-exec-ok ...] 2, the parameters of the Find command;
    • The directory path that the Pathname:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory.
    • The-print:find command outputs the matched file to standard output.
    • The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form "command {} \"; ", note" {} "and" \; " Space between the.
    • -ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.
3. Find command Options-name  finds files by file name. -perm  to find files according to file permissions. -prune  Use this option to have the Find command not be found in the currently specified directory, and if you use the-depth option at the same time,-prune will be ignored by the Find command. -user  Search for files according to the owner of the file. -group  finds files according to the group to which the files belong. -mtime-n +n  The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but they both and the-m time option. -nogroup  finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups. -nouser  finds a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd. -newer file1! File2   Find changed time than file File1 new but older file file2 than file. -type  find files of a certain type, such as: B-block device files. D-Directory. C-character device file. P-Pipeline file. L-Symbolic link file. F-Normal file. -size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes. -depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories. -fstype: Find files located in a file system of a certain type, these file system types can usually be found in the configuration file/etc/fstab, which contains information about the file system in this system. -mount: Does not cross the file system mount point when locating files. -follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to. -cpio: Use the cpio command for matching files to back up these files to the tape device.   In addition, the following three differences:    -amin n  Find the last n minutes of access to a file in the system-atime n  find the last n*24 hour Access file in the system-cmin n  Find files in the last n minutes of the system changed file status-ctime n  find system last n*24 hours file status changed file    -mmin n  Find the last n minutes of the system file data changed file-mtiMe n  find files in the system last n*24 hours changed file data   4. Use exec or OK to execute shell commands   When using find, just write the desired operation in a file, you can use the exec to match the Find lookup, very convenient   in some operating systems only allow the-EXEC option to execute commands such as L s or ls-l. Most users use this option to find old files and delete them. It is recommended that you take a look at the LS command before you actually execute the RM command to delete files, confirming that they are the files you want to delete. The &NBSP;EXEC option is followed by the command or script that you want to execute, followed by a pair of {}, a space and a \, and finally a semicolon. In order to use the EXEC option, you must use the Print option at the same time. If you verify the Find command, you will see that the command outputs only the relative path and file name from the current path.   For example: To list the matching files with the Ls-l command, you can place the Ls-l command in the-exec option of the Find command  # find. -type f-exec ls-l {} \;-rw-r--r--1 root root 34928 2003-02-25./conf/httpd.conf-rw-r--r--1 root root 12959 2003-02-25 ./conf/magic-rw-r--r--1 root root 2003-02-25 ./conf.d/readme  The above example, the Find command matches all the normal files in the current directory and uses LS in the-EXEC option: The l command lists them. In the/logs directory, look for files that change time before 5th and delete them:  $ find Logs-type f-mtime +5-exec rm {} \;  remember: Before the shell can delete files in any way, you should check the corresponding files, you must Be careful! You can use the Safe mode of the-EXEC option when using a command such as MV or RM. It will prompt you before you work on each file that is matched to it.   In the example below, the Find command finds all filenames in the current directory. Log end, change files over the 5th, and delete them, but give a hint before deleting them.  $ find. -name "*.conf"-mtime +5-ok rm {} \;< rm .... /conf/httpd.conf >? n  Press the Y key to delete the file, press N to not delete.   Any form of command can be used in the-EXEC option. &nbspIn the following example, we use the grep command. The find command first matches all files named "passwd*", such as passwd, Passwd.old, Passwd.bak, and then executes the grep command to see if there is a SAM user in these files.  # find/etc-name "passwd*"-exec grep "Sam" {} \;sam:x:501:501::/usr/sam:/bin/bash  Ii. Examples of the find command; 1. Find all files under the current user's home directory: The following two methods are available$ find $HOME-print$ find ~-print 2, let the current directory of the file owner has read, write permissions, and the file belongs to the group of users and other users have Read permission files;$ find.-type f-perm 644-exec ls-l {} \; 3, in order to find all the files in the system file length of 0 ordinary files, and list their full path; $ find/-type f-size 0-exec ls-l {} \; 4. Look for common files in the/var/logs directory that were changed before 7th, and ask them before deleting them;$ find/var/logs-type f-mtime +7-ok rm {} \; 5, in order to find all the files belonging to the root group in the system;$find. -group root-exec ls-l {} \;-rw-r--r--1 root root 595 October 01:09./fie1 6. The Find command will delete the Admin.log file that contains the digital suffix since the access time in the directory was 7th. This command checks only three digits, so the suffix of the corresponding file should not exceed 999. Build several admin.log* files before you can use the following command $ find. -name "admin.log[0-9][0-9][0-9]"-atime-7-okrm {} \;< rm .... /admin.log001 >? n< rm .... /admin.log002 >? n< rm .... /admin.log042 >? n< rm .... /admin.log942 >? N 7, in order to find all the directories in the current file system and sorting;$ find. -type D | Sort 8, in order to find all the RMT tape devices in the system;$ find/dev/rmt-print iii. parameters of the Find commandHere are some examples of find some common parameters, useful to the time to check on the line, like the previous posts above, have used some of the parameters, you can also use man or view other posts in the Forum have find command manual 1. Use the name optionThe file name option is the most common option for the Find command, either used alone or in conjunction with other options. You can use a file name pattern to match files, remembering to enclose the filename pattern in quotation marks. No matter what the current path is, if you want to find the file name in your root $home that matches *.txt, use ~ as the ' pathname ' parameter, and the tilde ~ represents your $home directory. $ find ~-name "*.txt"-print want to find all the ' *.txt ' files in the current directory and subdirectories, you can use: $ find. -name "*.txt"-print the current directory and subdirectories you want to find file names that begin with an uppercase letter can be used: $ find. -name "[a-z]*"-print want to find files with the file name beginning with host in the/etc directory, you can use: $ find/etc-name "host*"-print to find files in the $home directory, you can use: $ find ~-name "*"-print or find. -print to get the system running at a high load, start looking for all the files from the root directory. $ find/-name "*"-print if you want to find the file name in the current directory with two lowercase letters, followed by two digits, and finally the. txt file, the following command will be able to return a file named Ax37.txt: $find. -name "[A-z][a-z][0--9][0--9].txt"-print 2. With PERM optionFollow the file permission mode with the-perm option to find files by file permission mode. It is best to use the octal permission notation. For example, in the current directory to find file permissions bit 755 file, that is, the file owner can read, write, execute, other users can read, execute files, can be used: $ find. -perm 755-print also has a way of expression: in front of the octal number to add a bar-, the expression is matched, such as 007 is equivalent to 777,-006 equivalent to 666 # ls-l-rwxrwxr-x 2 Sam Adm 0 October 01:01 http3.c Onf-rw-rw-rw-1 Sam Adm 34890 October 00:57 httpd1.conf-rwxrwxr-x 2 Sam adm 0 October 01:01 httpd.confdrw-rw-rw-2 Gem Group 4096 October 19:48 sam-rw-rw-rw-1 root root 2792 October 20:19 temp# find. -perm 006# Find. -perm-006./sam./httpd1.conf./temp-perm mode: The file license is exactly in accordance with Mode-perm +mode: The file License section complies with Mode-perm-mode: The file license is fully compliant with mode 3. Ignore a directoryIf you want to ignore a directory when you're looking for a file, because you know that directory doesn't have the file you're looking for, you can use the-prune option to indicate which directories you want to ignore. Be careful when using the-prune option, because if you use the-depth option at the same time, the-prune option is ignored by the Find command. If you want to find the file under the/apps directory, but do not want to find it in the/apps/bin directory, you can use: $ find/apps-path "/apps/bin"-prune-o-print 4. How to avoid a file directory when finding files with findFor example, in the/usr/sam directory to find all files not within the Dir1 subdirectory Find/usr/sam-path "/usr/sam/dir1"-prune-o-print Find [-path ...] [expression] After the path list is the expression-path "/usr/sam"-prune-o-print is-path "/usr/sam" the shorthand expression of-a-prune-o-print is evaluated sequentially,-A and-O are Short-circuit evaluation, && with Shell | | Similarly, if-path "/usr/sam" is true, then the evaluation-prune,-prune returns True, and the logical expression is true; otherwise, the-prune is not evaluated, and the logical expression is false. If the-path "/usr/sam"-a-prune is false, the-print is evaluated, the-print returns True, or the logical expression is true, otherwise no value-print, or the logical expression is true. This combination of expressions can be written in pseudo-code as If-path "/usr/sam" then-pruneelse-print avoid multiple folders find/usr/sam \ (-path/usr/sam/di R1-o-path/usr/sam/file1 \)-prune-o-print parentheses represent the combination of expressions. \ denotes a reference, which instructs the shell not to give a special explanation of the characters that follow, leaving the Find command to explain its meaning. Find a certain file,-name and other options after-o #find/usr/sam \ (-path/usr/sam/dir1-o-path/usr/sam/file1 \)-prune-o-name "Temp"-print 5. Use the user and Nouser optionsFind files by file owner, such as in the $home directory to find files belonging to the main Sam file, you can use: Find ~-user sam-print in the/etc directory to find the file is the main UUCP file: $ find/etc-user uucp-print to check To find files that are already deleted from the master account, you can use the-nouser option. This will enable you to find files that are not valid accounts in the/etc/passwd file. When using the-nouser option, you do not have to give the user name; the Find command can do the work for you. For example, to find all such files in the/home directory, you can use: $ find/home-nouser-print 6. Use Group and Nogroup optionsJust like the user and Nouser options, the Find command has the same options for the group of users to which the file belongs, and in order to find files belonging to the Gem User group under the/apps directory, you can use: $ find/apps-group gem-print To find all files that do not have a valid group of users, you can use the Nogroup option. The following find command looks for such a file from the root of the file system $ find/-nogroup-print 7, according to change time or access time, etc. find filesYou can use the Mtime,atime or CTime option if you want to find the file by changing the time. If the system suddenly does not have free space, it is possible that the length of a file grows rapidly during this period, you can use the Mtime option to find such a file. Use a minus sign-to limit the time to change the file within the current n days, and use the Plus + to limit the change time before the current n days of the file. To find files that change within 5th of the system root, you can use: $ find/-mtime-5-print in order to find files in/var/adm directory that change time before 3rd, you can use: $ find/var/adm-mtime +3-print 8. Find newer or older files than a fileYou can use the-newer option if you want to find all files that have changed time than one file but older than the other. Its general form is: Newest_file_name! Oldest_file_name one of them,! is a logical non-symbol. Find changed time than file Sam new but older file than file Temp: example: There are two files-rw-r--r--1 Sam adm 0 October to 01:07 fiel-rw-rw-rw-1 Sam Adm 34890 October to 00:57 HTTPD1. Conf-rwxrwxr-x 2 Sam Adm 0 October 01:01 httpd.confdrw-rw-rw-2 gem Group 4096 October 19:48 sam-rw-rw-rw-1 root root 2792 October 20:19 temp# find-newer httpd1.conf! -newer temp-ls1077669 0-rwxrwxr-x 2 Sam adm 0 October 01:01./httpd.conf1077671 4-rw-rw-rw-1 root root 2792 October 31 20:1 9./temp1077673 0-rw-r--r--1 Sam adm 0 October 01:07./fiel look for a new file that changed time in the temp file: $ find. -newer Temp-print 9. Use the Type optionTo find all the directories in the/etc directory, you can use: $ find/etc-type d-print to find all types of files except directories in the current directory, which can be used: $ find. ! -type D-print in the/etc directory to find all the symbolic link files, you can use $ find/etc-type l-print 10. Use the size optionfiles can be searched by file length, and the length of the file referred to here can be measured either in blocks or in bytes. The length of the measured file in bytes is expressed as n C, and the length of the block measurement file is only represented by a number. When looking up files by file length, this is generally the size of the file in bytes, and it is easier to convert by using blocks to measure the file system. In the current directory, look for files with a file length greater than 1 m bytes: $ find. -size +1000000c-print find files with a file length of exactly 100 bytes in the/home/apache directory: $ find/home/apache-size 100c-print In the current directory, look for files with a length of more than 10 (one block equals 512 bytes): $ find. -size +10-print 11. Using the Depth optionWhen you use the Find command, you may want to match all the files and find them in the subdirectory. Use the depth option to enable the Find command to do so. One reason for this is that when you use the Find command to back up the file system to tape, you want to back up all the files first, and then back up the files in the subdirectories. In the following example, the Find command starts at the root of the file system and looks for a file named Con.file. It will first match all the files and then go to the subdirectory to find them. $ find/-name "CON. FILE "-depth-print 12. Using the Mount optionYou can use the Mount option of the Find command to find a file in the current file system (without entering another file system). From the current directory, look for files in the file system with the file name ending in XC: $ find. -name "*. XC "-mount-print from:http://coffeelet.blog.163.com/blog/static/13515745320114544432103/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.