file search command find

16. File search command find :

    The main application example of the find command is given below:
    /> ls -l #List      the test files contained in the current directory
    -rw-r--r--. 1 root root 48217 Nov 12 00:57 install.log
    -rw-r--r--. 1 root root 37 Nov 12 00:56 testfile.dat
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar .bz2
    -rw-r--r--. 1 root root 183 Nov 11 08:02 users
    -rw-r--r--. 1 root root 279 Nov 11 08:45 users2
    
    1. Find by filename :
    - name:   The file name is case sensitive when searching.
    -iname: The  filename is case-insensitive when searching.
    #This command is the most commonly used command in the find command, that is, it searches the file with the extension .log from the current directory. It should be noted that by default, find will search from the specified directory and recursively search its subdirectories.
    /> find . -name "*.log"
     ./install.log
    /> find . -iname U*          #If you execute find . -name U* will not find matching files
    users users2


    2. Search by file time attribute:
    -atime -n[+n]:  Find files whose access time is within [outside] of n days.
    -ctime -n[+n]:  Find files whose file change time is within [outside] n days.
    -mtime -n[+n]:  Find files with modified data time within [outside] n days.
    -amin -n[+n]:  Find files with file access times within [outside] n minutes.
    -cmin -n[+n]:  Find files whose file change time is within [outside] n minutes.
    -mmin -n[+n]:  Find files with modified data within [outside] n minutes.
    /> find -ctime -2 #Find         files created within 2 days from this time
    .
    ./users2
    ./install.log
    ./testfile.dat
    ./users ./test.tar.bz2
    /
    > find -ctime + 2 #Find out that the files created 2 days ago are
    not found #Because all files in the current directory were created within 2 days
    /> touch install.log     #Manually update the last access time of install.log, so that the following find command can find the file
    /> find . -cmin -3 #Find files whose modification status time is within 3 minutes.
    install.log

    3. Perform the specified action based on the files found:
    -exec:  Execute the shell command given by this parameter on the matching files. The form of the corresponding command is 'command' {} \;, pay attention to the space between {} and \;, and there is no space between the two {}
    -ok:    its main function and syntax format are exactly the same as -exec, the only The difference is that this option is more secure, because it prompts before each shell command is executed, and only if the answer is y, the subsequent shell command will be continued. To be clear, this option does not apply to automation scripts, as this offer may hang the entire automation process.
    #Find files created within 2 days from this time, and based on the results of find, apply the command after -exec, that is, ls -l, so that an obvious list of files found by find can be directly displayed.
    /> find . -ctime -2 -exec ls -l {} \;
    -rw-r--r--. 1 root root 279 Nov 11 08:45 ./users2
    -rw-r--r--. 1 root root 48217 Nov 12 00:57 ./install.log
    -rw-r--r--. 1 root root 37 Nov 12 00:56 ./testfile.dat
    -rw-r--r--. 1 root root 183 Nov 11 08:02 ./users
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 ./test.tar.bz2
    #Found The file name is *.log, and the file data modification time is within 1 day. Delete them if found. Sometimes, this way of writing is deleted immediately after it is found, so there is a certain danger of accidental deletion.
    /> ls
    install.log testfile.dat test.tar.bz2 users users2
    /> find . -name "*.log" -mtime -1 -exec rm -f {} \; 
    /> ls
    testfile.dat test.tar. bz2 users users2
    is under the console, in order to make the above command more secure, we can replace -exec with -ok, see the following example:
    /> find . -name "*.dat" -mtime -1 -ok rm -f { } \;
    < rm ... ./testfile.dat > ?  y #To this prompt, if you answer y, the found *.dat files will be deleted, as can be seen from the results of the ls command below.
    /> ls
    test.tar.bz2 users users2

    4. Search by owner and group to which the file belongs:
    -user:      Find files whose owner belongs to the user specified after the -user option.
    ! -user:     Find files whose owner does not belong to the user specified after the -user option.
    -group:   
Find files whose group belongs to the group specified after the -group option.
    ! -group:  Find files whose group does not belong to the group specified after the -group option.
    /> ls -l        #The                      owners of the following three files are root
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
    -rw-r--r--. 1 root root 183 Nov 11 08:02 users
    -rw-r--r--. 1 root root 279 Nov 11 08:45 users2
    /> chown stephen users #Change    the owner of the users file from root to stephen.
    /> ls -l
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
    -rw-r--r--. 1  stephen  root 183 Nov 11 08:02 users
    -rw-r--r--. 1 root root 279 Nov 11 08:45 users2
    /> find . -user root #Search           for files whose owner is root
    .
    ./users2 ./test.tar.bz2
    /
    > find . ! -user root #Search         for files whose owner is not root. Note that there should be a space between ! and -user.
    ./users
    /> ls -l          #The                    following three files belong to root
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
    -rw-r--r --. 1 stephen root 183 Nov 11 08:02 users
    -rw-r--r--. 1 root root 279 Nov 11 08:45 users2
    /> chgrp stephen users #Change     the group of users file from root to stephen
    /> ls -l
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
    -rw-r--r--. 1 stephen stephen       183 Nov 11 08:02 users
    -rw-r--r--. 1 root root 279 Nov 11 08:45 users2
    /> find . -group root  #Search        files whose group is root
    .
    ./users2
    ./test .tar.bz2
    /> find . ! -group root #Search       for files whose group is not root. Note that there should be a space between ! and -user.    
    ./users

    5. Search according to the specified directory depth:
    -maxdepth:  The following parameters indicate the specified depth from the current directory, where 1 indicates the current directory, 2 indicates a first-level subdirectory, and so on. When this option is specified, find simply does not recurse its subdirectories after finding the specified depth. The depth in the example below is 1, which means only the current subdirectory is searched. If this option is not set, find will recurse all subdirectories under the current directory.
    /> mkdir subdir #Create                a subdirectory and create a file in that subdirectory
    /> cd subdir
    /> touch testfile
    /> cd ..
    The parameter after #maxdepth represents the specified depth from the current directory, where 1 represents the current directory, 2 represents the first-level subdirectory, and so on. When this option is specified, find simply does not recurse its subdirectories after finding the specified depth. The depth in the example below is 1, which means only the current subdirectory is searched. If this option is not set, find will recurse all subdirectories under the current directory.
    /> find . -maxdepth 1 -name "*"
    .
    ./users2
    ./subdir ./users ./test.tar.bz2
    #Search
    depth
    is sub-level sub-directory, here you can see the testfile just created in the sub-directory already found
    /> find . -maxdepth 2 -name "*"  
    .
    ./users2
    ./subdir
    ./subdir/testfile ./users
    ./test.tar.bz2
    6.
    
    Exclude the specified subdirectory search:
    -path pathname -prune :    Avoid the specified subdirectory pathname search.
    -path expression -prune:   Avoid lookups for a set of pathnames specified in the expression.
    It should be noted that if the -depth option is also used, then -prune will be ignored by the find command.
    #Create subdirectories that need to be avoided and subdirectories that do not need to be avoided for the following examples, and create files that meet the search rules in these subdirectories.
    /> mkdir DontSearchPath  

    /> cd DontSearchPath
    /> touch datafile1
    /> cd ..
    /> mkdir DoSearchPath
    /> cd DoSearchPath
    /> touch datafile2
    /> cd ..
    /> touch datafile3
    #In the current directory, avoid the DontSearchPath subdirectory, search All files named datafile*.
    /> find . -path "./DontSearchPath" -prune -o -name "datafile*" -print
    ./DoSearchPath/datafile2
    ./datafile3 #In
    the current directory, while avoiding the two subdirectories of DontSearchPath and DoSearchPath, search for all file names A file for datafile*.
    /> find . \( -path "./DontSearchPath" -o -path "./DoSearchPath" \) -prune -o -name "datafile*" -print
    .
        
 
    -perm mode:    file permissions exactly match mode (mode is the octal representation of file permissions).
    -perm +mode: The  file permissions part conforms to mode. If the command parameter is 644 (-rw-r--r--), then as long as any permissions in the file permissions attribute overlap with 644, such files can be selected.
    -perm -mode: The   file permissions are exactly in mode. If the command parameter is 644 (-rw-r--r--), when the permission specified in 644 is already fully owned by the current file, and the file also has additional permission attributes, such a file can be selected.
    /> ls -l
    -rw-r--r--. 1 root root 0 Nov 12 10:02 datafile3
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
    - rw-r--r--. 1 stephen stephen 183 Nov 11 08:02 users
    -rw-r--r--. 1 root root 279 Nov 11 08:45 users2
    /> find . -perm 644 #Find       all files A file with permissions exactly 644 (-rw-r--r--).
    ./users2
    ./datafile3
    ./users
    ./test.tar.bz2
    /> find . -perm 444 #The       permissions of no files in the current directory belong to 444 (both are 644).    
    /> find . -perm -444      #644 contains permissions that completely override those represented by 444.
    .
    ./users2
    ./datafile3 ./users
    ./test.tar.bz2
    /
    >  find . -perm +111 #Find     all executable files, this command does not find any files.
    /> chmod u+x users      #Change the permissions of the users file and add the owner's executable permissions so that the following command can find it.
    /> find . -perm +111     
    .
    ./users    
    
    8. Find by file type:
    -type: Specify the file type later.
    b  - block device file.
    d  - directory.
    c  - character device file.
    p  - the pipe file.
    l   - Symbolically linked file.
    f   - normal file.
    /> mkdir subdir  
    /> find . -type d #In       the current directory, find the file whose file type is directory.
    ./subdir
  /> find . ! -type d  #In    the current directory, find files whose file type is not a directory.
    ./users2
    ./datafile3 ./users
    ./test.tar.bz2
    /
    > find . -type f   #In      the current directory, find the file whose file type is file
    ./users2
    ./datafile3
    ./users
    ./test. tar.bz2
    
    9. Find by file size:
    -size [+/-]100[c/k/M/G]:  Indicates that the length of the file is equal to [greater/less than] 100 blocks [byte/k/M/G] ]document.
    -empty:  Find empty files.
    /> find . -size +4k -exec ls -l {} \;   #Find files with a file size greater than 4k, and print out the details of the files found
    -rw-r--r--. 1 root root 10530 Nov 11 23 :08 ./test.tar.bz2
    /> find . -size -4k -exec ls -l {} \; #Find   files whose size is less than 4k.
    -rw-r--r--. 1 root root 279 Nov 11 08:45 ./users2
    -rw-r--r--. 1 root root 0 Nov 12 10:02 ./datafile3
    -rwxr--r- -. 1 stephen stephen 183 Nov 11 08:02 ./users
    /> find . -size 183c -exec ls -l {} \; #Find  files with a file size equal to 183 bytes.
    -rwxr--r--. 1 stephen stephen 183 Nov 11 08:02 ./users
    /> find . -empty -type f -exec ls -l {} \;
    -rw-r--r--. 1 root root 0 Nov 12 10:02 ./datafile3
    
    10. Find files whose change time is newer or older than the specified file:
    -newer file1 ! file2:  Find files whose change date is newer than file1 but older than file2.
    /> ls -lrt #Lists    a detailed list of all files in the current directory in chronological order (from early to late) for reference in later examples.
    -rwxr--r--. 1 stephen stephen 183 Nov 11 08:02 users1
    -rw-r--r--. 1 root root 279 Nov 11 08:45 users2
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
    -rw-r-- r--. 1 root root 0 Nov 12 10:02 datafile3
    /> find . -newer users1 #Find      files with a file change date newer than users1. As can be seen from the above results, the rest of the files meet the requirements.
    ./users2
    ./datafile3 ./test.tar.bz2
    /
    > find . ! -newer users2 #Find    files whose change date is not newer than users1.
    ./users2
    ./users #Find
    files whose change date is newer than users2, but not newer than test.tar.bz2.
    /> find . -newer users2 ! -newer test.tar.bz2
    ./test.tar.bz2    

 

17. xargs command :

    The main function of this command is to construct and execute shell commands from input.       
    When using the -exec option of the find command to process the matched files, the find command passes all the matched files to exec for execution. But some systems have a limit on the length of commands that can be passed to exec, so that after a few minutes of the find command running, an overflow error occurs. The error message is usually "parameter column too long" or "parameter column overflow". This is where the xargs command comes in, especially with the find command.  
    The find command passes the matched files to the xargs command, and the xargs command gets only some of the files at a time instead of all, unlike the -exec option. This way it can process the first batch of files it gets, then the next batch, and so on.  
    In some systems, using the -exec option will initiate a corresponding process for each matched file, instead of executing all the matched files as parameters at one time; in this way, there will be too many processes in some cases, and the system will The problem of performance degradation, so it is not efficient;  
    while using the xargs command, there is only one process. In addition, when using the xargs command, whether to obtain all parameters at one time or to obtain parameters in batches, and the number of parameters obtained each time will be determined according to the options of the command and the corresponding tunable parameters in the system kernel.
    /> ls -l
    -rw-r--r--. 1 root root 0 Nov 12 10:02 datafile3
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
    - rwxr--r--. 1 root root 183 Nov 11 08:02 users
    -rw-r--r--. 1 root root 279 Nov 11 08:45 users2 #Find
    every common file in the current directory, and then use the xargs command to test which type of file they belong to.
    /> find . -type f -print | xargs file 
    ./users2: ASCII text
    ./datafile3: empty
    ./users: ASCII text ./test.tar.bz2
    : bzip2 compressed data, block size = 900k #Recycle
    the current directory Execute permissions for all normal files.
    /> find . -type f -print | xargs chmod ax
    /> ls -l
    -rw-r--r--. 1 root root 0 Nov 12 10:02 datafile3
    -rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
    -rw-r--r--. 1 root root 183 Nov 11 08:02 users
    -rw-r--r--. 1 root root 279 Nov 11 08: 45 users2 #Find
    all ordinary files in the face directory, and use the grep command to find the word hostname in the searched files
    /> find . -type f -print | xargs grep "hostname" 
    #Find the memory information dump file (core dump) in the whole system, and then save the result to the /tmp/core.log file.
    /> find / -name "core" -print | xargs echo "" >/tmp/core.log  

    /> pgrep mysql | xargs kill -9 #Directly   kill the mysql process
    
[1]+ Killed mysql

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028478&siteId=291194637