Linux under the Find command

Source: Internet
Author: User

Turn from: http://www.cnblogs.com/skynet/archive/2010/12/25/1916873.html1.1, find command general form

The general form of the find command given in the man document is:

Find [-h] [-l] [-p] [-D debugopts] [-olevel] [path ...] [Expression]

In fact [-h] [-l] [-p] [-D debugopts] [-olevel] These options are not commonly used (at least in my daily work, not used), the above the common form of the Find command can be simplified to:

Find [path ...] [Expression]

    • The directory path that the Path:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory
    • Expression:expression can be divided into--"-options [-print-exec-ok ...]"
    • -options, specify common options for the Find command, and the following section details
    • The-print,find command outputs the matched file to the standard output
    • The-exec,find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \;
      Find./-size 0-exec rm {} \; Delete files with a file size of zero (you can also do this: Rm-i ' find./-size 0 ' or find./-size 0 | Xargs Rm-f &)
      In order to list the matching files with the Ls-l command, you can place the Ls-l command in the-exec option of the Find command: Find. -type f-exec ls-l {} \;
      In the/logs directory, look for files that change time before 5th and delete them: Find/logs-type f-mtime +5-exec rm {} \;
    • -ok, which is the same as-exec, simply executes the shell command given by the parameter in a more secure mode, prompting the user to determine whether to execute before executing each command.
      Find. -name "*.conf"-mtime +5-ok rm {} \; Finds all filenames in the current directory. Log end, change files over 5th, and delete them, just give a hint before deleting

Someone also summarizes the structure of the Find command:




Action_to_perform_on_results
1.2. Common options and examples of the Find command
  • -name
    Find files by file name.
    Find/dir-name FileName Locate the file named filename under the/dir directory and its subdirectories
    Find. -name "*.C" in the current directory and its subdirectories (with "." To find any file with the extension "C" in the
  • -perm
    Follow the file permissions to find the file.
    Find. -perm 755–print in the current directory to find files with permission bit 755, that is, the file owner can read, write, execute, other users can read, execute files
  • -prune
    Use this option to make the Find command not look in the currently specified directory, and if you use the-depth option at the same time,-prune will be ignored by the Find command.
    Find/apps-path "/apps/bin"-prune-o–print find files in the/apps directory, but do not want to find them in the/apps/bin directory
    Find/usr/sam-path "/usr/sam/dir1"-prune-o–print Find all files in the/usr/sam directory that are not within the DIR1 subdirectory
  • -user
    Locate the file according to the owner of the file.
    Find ~-user sam–print look up files in the $home directory that belong to the main Sam file
  • -group
    Locate the file according to the group to which it belongs.
    Find/apps-group Gem–print Find files belonging to the Gem User group in the/apps directory
  • -mtime-n +n
    The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago.
    Find/-mtime-5–print in the system root for files with a change of time within 5th
    Find/var/adm-mtime +3–print in/var/adm directory to find the files that were modified before 3rd
  • -nogroup
    Finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups.
    Find/–nogroup-print
  • -nouser
    Find a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd.
    Find/home-nouser–print
  • -newer file1! File2
    Look for changes that are newer than the file file1 but file2 older than the file.
  • -type
    Find a file of a certain type, such as:
    B-block device files.
    D-Directory.
    C-character device file.
    P-Pipeline file.
    L-Symbolic link file.
    F-Normal file.
    Find/etc-type D–print Find all directories in/etc directory
    Find. ! -type D–print to find all types of files except directories in the current directory
    Find/etc-type L–print Find all the symbolic link files in the/etc directory
  • -size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes.
    Find. -size +1000000c–print Find files with file lengths greater than 1 m in the current directory
    Find/home/apache-size 100c–print looking for files with a file length of exactly 100 bytes in the/home/apache directory
    Find. -size +10–print Find files with a length of more than 10 blocks in the current directory (a chunk equals 512 bytes)
  • -depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories.
    Find/-name "CON. File "-depth–print It will first match all the files and then go to the subdirectory to find
  • -mount: Does not cross the file system mount point when locating files.
    Find. -name "*. XC "-mount–print from the current directory to find files in the file system with the file name ending in XC (no other file system)
  • -follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to.
1.3. Find and Xargs

When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.

The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.

In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there will be too many processes and degraded system performance, so the efficiency is not high;

With the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.

Take a look at how the Xargs command is used with the Find command, and give some examples.

Find. -type F-print | Xargs file looks up every normal document in the system, and then uses the Xargs command to test what type of file they belong to

Find/-name "core"-print | Xargs echo "" >/tmp/core.log find the Memory information dump file (core dump) throughout the system and save the results to the/tmp/core.log file:

Find. -type F-print | Xargs grep "hostname" uses the grep command to search for the word hostname in all common files.

Find/-mtime +3-print|xargs rm-f–r Delete everything from 3 days ago (find. -ctime +3-exec rm-rf {} \;)

Find./-size 0 | Xargs Rm-f & Delete files with a file size of zero

The Find command, with exec and Xargs, allows the user to execute almost all commands against the matching file.

Linux under the Find command

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.