Home  >  Article  >  System Tutorial  >  Parsing the options of the find command

Parsing the options of the find command

王林
王林Original
2024-02-18 21:51:21702browse

The find command is a commonly used file search command in Linux systems. This command can be used to find files that meet the conditions in the specified directory. The find command has many parameters and powerful functions. This article will analyze some commonly used parameters in detail and provide specific code examples.

1. Basic Usage

  1. The basic syntax of the find command is as follows:

    find [path...] [expression]

    Among them, path represents the directory to be searched, which can be one or more indivual. expression represents the operation to be performed, which can be to find file types, find files by size, find files by time, etc.

Example 1: Find a file named "file.txt" in the current directory

find . -name "file.txt"

Example 2: Recursively search the /home directory ending with ".txt" File

find /home -name "*.txt"

2. Commonly used parameters

  1. -name parameter: Search by file name, wildcards can be used for fuzzy matching.

Example 3: Find files starting with "file" in the current directory and its subdirectories

find . -name "file*"
  1. -type parameters: Search by file type, commonly used types There are f (ordinary file), d (directory), l (symbolic link), etc.

Example 4: Find all directories under the current directory and its subdirectories

find . -type d
  1. -size parameter: Search by file size, you can use K (kilobytes ), M (megabyte) and other units.

Example 5: Find files larger than 1MB in the current directory and its subdirectories

find . -size +1M
  1. -mtime Parameter: Search based on modification time, in days. It means greater than, - means less than, and no sign means exactly equal.

Example 6: Find files that have been modified in the last 30 days in the current directory and its subdirectories

find . -mtime -30

3. Advanced usage

  1. -exec Parameters: Execute the specified command.

Example 7: Find all files named "file.txt" in the current directory and its subdirectories and delete them

find . -name "file.txt" -exec rm {} ;
  1. -print parameters: Search results are output to standard output.

Example 8: Find files larger than 100KB in the current directory and its subdirectories, and save the results to the file

find . -size +100k -print > large_files.txt
  1. -prune parameter: exclude the specified The directory is not searched.

Example 9: Find all files named "file.txt" in the current directory and its subdirectories, but exclude the ./tmp directory

find . -name "file.txt" -prune -o -print

Summary:

This article explains in detail the common parameters of the find command and provides specific code examples. However, it should be noted that the find command has many parameters and is powerful. Readers can further learn and use it according to their own needs. At the same time, you need to be careful when operating commands to avoid accidentally deleting or modifying important files. It is recommended to test and confirm the accuracy of the operation before use.

The above is the detailed content of Parsing the options of the find command. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn