Find command detailed

  
        

find is one of the best commands in linux, and its query function is very powerful. Most of the usage of find is detailed below: How it works: It traverses down the file hierarchy, matches the files that match the criteria, and performs the appropriate actions.


  1. 1.Printing files and directory listings
  2. $find . -print #Printing files and directory listings

    When using -print, ’\ ’ as a delimiter for separating files. -print0 indicates that ’\\0’ is used as a delimiter to print each file name that matches. This method is very effective for file names with ’\ ’. However, after the actual test on centos, find directly displays the matching file, without the -print option. 2. Match the search option according to the file name or regular expression -name specifies the string that the file name must match.


    1. $find . -name " *.txt" -print # Print files ending with .txt
    2. $find . -iname "*.txt" -print #-iname indicates that the matching file name ignores case

      3. Conditional constraints, matching one of several conditions -a: means and -o: means or -not: means not


      1. $find . (( -name "*.txt" -o -name "*.pdf" \\) -print #Print the file ending in .txt or .pdf in the current directory
      2. $find . \\( -name "*.txt" -a -name "a*" \\) -print #Print the file starting with a and ending with .txt in the current directory
      3. $find . -not \\( -name "*.txt" -a -name "a*" \\) -print #Print files that do not start with a or end with .txt in the current directory
      4. $find . -not \\( -name "*.txt" -o -name "a*" \\) -print #Print the file in the current directory that does not start with a and does not end with .txt

        -not usage is derived from De Morgan's law, see http://en.wikipedia.org/wiki/%E5%BE%B7%E6%91%A9%E6%A0%B9%E5 %AE%9A%E5%BE%8B

        -path: This parameter is used to match the file path as a whole.


        1. $find /home/user -path "*sh" -print
        2. /home/user/bin/1.sh< Br>
        3. /home/user/bash
        4. -regex : This parameter is similar to -path except that it matches the file path based on regular expressions.
        5. $find . -regex ".*\\( \\.py \\
Copyright © Windows knowledge All Rights Reserved