The Find command in Linux

Source: Internet
Author: User
Tags uppercase letter

1.  Use the name option: The file name option is the most common option for the Find command, either by using the option alone or 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 *.log, use ~ as the ' pathname ' parameter, and the tilde ~ represents your $home directory. Find ~-name "*.log"-print want to find all the ' *.log ' files in the current directory and subdirectories, you can use: Find. -name "*.log"-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 in the/etc directory with the file name beginning with host, can be used: Find/etc-name "host*"-print want to find files in $home directory, can be used: 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 starting with a lowercase letter, the last is 4 to 9 plus. Log End File: command: Find. -name "[a-z]*[4-9].log"-print output: [[email protected] test]# ll total 316-rw-r--r--1 root root 302108 11-13 06:03 log2012. log-rw-r--r--1 root root 11-13 06:03 log2013.log-rw-r--r--1 root root 0 11-13 06:03 log2014.log-rw-r--r--1 Root root 0 11-13 06:06 log2015.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 0 6:08 test3drwxrwxr-x 2 root root 4096 11-13 05:50 test4[[email protected] test]# find. -name "[A-z]*[4-9].log"-print./log2014.log./log2015.log./test4/log2014.log[[email protected] Test] #2. With the PERM option: Follow the file permission mode with the-perm option, according to the file permission mode to find the file. It is best to use the octal permission notation. For example, in the current directory to find file permission bit 755 file, that is, the file owner can read, write, execute, other users can read, execute files, can be used: [[email protected] test]# find. -perm 755-print. /scf./scf/lib./scf/service./scf/service/deploy./scf/service/deploy/product./scf/service/deploy/info./scf/doc./ Scf/bin[[email protected] test]# 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,-005 equivalent to 555, command: Find. -perm-005 output: [[email protected] test]# ll total 316-rw-r--r--1 root root 302108 11-13 06:03 log2012.log-rw-r--r--1 root Root 11-13 06:03 log2013.log-rw-r--r--1 root root 0 11-13 06:03 log2014.log-rw-r--r--1 root root 0 11- 06:06 log2015.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 06:08 test3drwxrwxr-x 2 root root 4096 11-13 05:50 test4[[email protected] test]# find. -perm-005../test4./scf./scf/lib./scf/service./scf/service/deploy./scf/service/deploy/product./scf/service/ Deploy/info./scf/doc./scf/bin./tesT3[[email protected] Test] #3. Ignore a directory: If you want to ignore a directory when you are looking for a file, because you know that directory does not have the file you are looking for, you can use the-prune option to indicate which directories you need 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 test directory but do not want to find it in the Test/test3 directory, you can use: command: Find Test-path "test/test3"-prune-o-print output: [[email protected] soft]# find Test-path "Test/test3"-prune-o-printtesttest/log2014.logtest/log2015.logtest/test4test/test4/ log2014.logtest/test4/log2013.logtest/test4/log2012.logtest/scftest/scf/libtest/scf/servicetest/scf/service/ deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2013.logtest/ Log2012.log[[email protected] soft] #4. How to avoid a file directory when finding files using Find: Instance 1: Find all the files in the test directory that are not within the TEST4 subdirectory command: Find Test-path "test/test4"-prune-o-print output: [[email  protected] soft]# Find testtesttest/log2014.logtest/log2015.logtest/test4test/test4/log2014.logtest/test4/ log2013.logtest/test4/log2012.logtest/scftest/scf/libtest/scf/servicetest/scf/service/deploytest/scf/service/ deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2013.logtest/log2012.logtest/test3[[ Email&nbSp;protected] soft]# Find Test-path "Test/test4"-prune-o-PRINTTESTTEST/LOG2014.LOGTEST/LOG2015.LOGTEST/SCFTEST/SCF /libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf /doctest/scf/bintest/log2013.logtest/log2012.logtest/test3[[email protected] Soft] #说明: Find [-path ...] [expression] After the path list is the expression-path "test"-prune-o-print is-path "test"-a-prune-o-print shorthand expression evaluated sequentially,-A and-O are short-circuit evaluation, and Shell's && | | Similarly, if-path "test" is true, the-prune is evaluated,-prune returns TRUE, and the logical expression is true, otherwise no value-prune, and the logical expression is false. If the-path "test"-a-prune is false, the evaluation is-print,-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: If-path "Test" Then-prune Else-print Instance 2: Avoid multiple folders: command: Find test \ (-path test/test4-o-path test/te ST3 \)-prune-o-print output: [[email protected] soft]# find test \ (-path test/test4-o-path test/test3 \)-prune-o- printtesttest/log2014.logtest/log2015.logtest/scftest/scf/libtest/scf/servicetest/scf/service/deploytest/scf/ Service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2013.logtest/log2012.log[[email  protected] soft]# Description: 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. Example 3: Find a certain file,-name and other options added after-o command: Find test \ (-path test/test4-o-path test/test3 \)-prune-o-name "*.log"-print output: [[Emai L protected] soft]# find test \ (-path test/test4-o-path test/test3 \)-prune-o-name "*.log"-printtest/log2014.l Ogtest/log2015.logtest/log2013.logtest/log2012.log[[email protected] soft] #5. Using the user and Nouser options: Find files by file: Instance 1: Find files in the $home directory that belong to peida File command: Find ~-user Peida-print Instance 2: Find files in/etc directory where the file belongs to Peida: Command: Find/etc-user peida-print Description: Instance 3: In order to find files that are already deleted by the master account, you can use the-nouser option. Find all of these files in the/home directory command: Find/home-nouser-print Description: 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. 6. Use the group and Nogroup options: Just like the user and Nouser options, the Find command has the same options for the group of users that the file belongs to, in order to find files belonging to the Gem User group in 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 findcommand to find such a file from the root of the file system: Find/-NOGROUP-PRINT7. Find files by change time or access time: You can use the Mtime,atime or CTime option if you want to find files 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 the files in the/var/adm directory that change time before 3rd, you can use: Find/var/adm-mtime +3-print8. Find newer or older files than a file: You can use the-newer option if you want to find all files that are newer 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. Example 1: Find changed time than file Log2012.log new but older file log2017.log than file command: Find-newer log2012.log! -newer log2017.log output: [[email protected] test]# ll total 316-rw-r--r--1 root root 302108 11-13 06:03 log2012.log-rw-r--r --1 root root 11-13 06:03 log2013.log-rw-r--r--1 root root 0 11-13 06:03 log2014.log-rw-r--r--1 root root 0 11-13 06:06 log2015.log-rw-r--r--1 root root 0 11-16 14:41 log2016.log-rw-r--r--1 root root 0 11-16 14:4 3 log2017.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 06:08 test3drwxrwxr-x 2 rootRoot 4096 11-13 05:50 test4[[email protected] test]# find-newer log2012.log! -newer Log2017.log. /log2015.log./log2017.log./log2016.log./test3[[email protected] Test] #实例2: Find the change time in the new File command than Log2012.log file: find . -newer log2012.log-print output: [[email protected] test]# find-newer log2012.log. /log2015.log./log2017.log./log2016.log./test3[[email protected] Test] #9. Use the type option: Instance 1: Find all directory commands in the/etc directory: Find/etc-type d-print instance 2: Finds all types of files except directories in the current directory command: Find. ! -type D-print Instance 3: Find all the symbolic link file commands in the/etc directory: Find/etc-type l-print10. Use the size option: You can find files by file length, where the length of the file referred to 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. Example 1: Find files with file lengths greater than 1 m bytes in the current directory command: Find. -size +1000000c-print Example 2: Find files with a file length of exactly 100 bytes in the/home/apache directory: command: Find/home/apache-size 100c-print Example 3: Find a file with a length of more than 10 (a block equals 512 bytes) command in the current directory: Find. -size +10-print11. Use the depth option: When using the Find command, you might want to match all of the files and find them in the subdirectory. Use the depth option to enable the Find command to do so. One of the reasons for this is that when you use the Find command to prepare a tapeFile system, you want to back up all the files first, and then back up the files in the subdirectories. The instance 1:find command starts at the root of the file system and looks for a file named Con.file. Command: Find/-name "CON." File "-depth-print Description: It will first match all the files and then go to the subdirectory to find 12. Use the Mount option: to find a file in the current file system (without entering another file system), you can use the Mount option of the Find command. Example 1: Starting from the current directory, find the file in the file system that is located in the end of XC Files command: Find. -name "*. XC "-mount-print

The Find command in Linux

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.