The find command is used to search for files under a specified directory that match the search options. The difference between awk, sed, and grep is that the objects it searches for are files, and the last three are characters or strings in files.

Introduction to the

The find command in Linux is used to find files in a specified directory. Any string before the parameter is treated as the name of the directory you are looking for. If you use this command without setting any parameters, the find command will find subdirectories and files in the current directory. All subdirectories and files found are displayed.

usage

  • The command format

    find path -option
    Copy the code
  • Parameters that

    • -name 'pattern'Search by name
    • -user 'pattern'By Owning user
    • -size nBased on file size
    • -type [fdbl]By file type
    • -emptySpacetime file
    • -exec command {} \;Further command processing of found files
    • -mtime nModify time according to file content (unit: day)
    • -ctime nChange according to file properties [chmod] Time (hour/day)
    • -atime nAccording to file access [cat] Time (in days)

    Note: -mmin n/-cmin n/-amin n and the above three differences are in minutes. For n,n refers to exactly n days ago,+n refers to all N days ago, and -n refers to all N days

case

  • Find files under the _posts/ directory whose names contain shell strings

    find _posts/ -name '*shell*'
    Copy the code

  • Look for files under the _posts/ directory whose names contain shell strings and count how many lines each file has

    find _posts/ -name '*shell*' -exec wc -l {} \;
    Copy the code

  • Query for files larger than 3k under _posts/ directory

    find _posts/ -size +3k
    Copy the code

  • The search result is based on the modification time (unit: minute) of the file content

    At present, the files in _posts are shown as follows in chronological order. Among them, the modification time of SWP file is 18:12 on July 1st, while the original file is 17:59. The other files are all files on June 30th.

    • Query which files were modified in the previous 19 minutes

      find _posts/ -mmin 19
      Copy the code

    • Query all files that were modified 19 minutes ago

      find _posts/ -mmin +19
      Copy the code

    • Query all files that are modified within the last 19 minutes

      find _posts/ -mmin -19
      Copy the code

reference

  1. Wikipedia find
  2. Find the source code

If this article is helpful to you, or you are interested in technical articles, you can follow the wechat official number: Technical Tea Party, and you can receive relevant technical articles in the first time. Thank you!

This article was automatically published by ArtiPub