Shell script find command

Source: Internet
Author: User

The find command is used for searching. It has powerful functions in the following format:

 
Find pathname-options [-print-Exec-OK...]
 
Pathname: directory path searched by the find command. For example, use "." To represent the current directory, and use "/" to represent the root directory of the system. -Print: The find command outputs matching files to the standard output. -Exec: The find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {}\;. Note the space between {} And. -OK: The Role of-exec is the same, but the shell command given by this parameter is executed in a safer mode. A prompt is displayed before each command is executed, let the user determine whether to execute.

There are many command options, which are commonly used as follows:

-Name: searches for files by file name. -Perm searches for files based on the file permissions. -Prune uses this option to make the find command not to be searched in the specified directory. If the-depth option is used at the same time,-prune will be ignored by the find command. -The user searches for files based on the file owner. -The group searches for files based on the group to which the files belong. -Mtime-N + N: Find the file based on the file change time.-N indicates that the file change time is earlier than N days, and + N indicates that the file change time is earlier than N days. The find command also has the-atime and-ctime options, but they both have the-M time options. -Nogroup: Find the file with no valid group, that is, the group to which the file belongs does not exist in/etc/groups. -Nouser: Find the file without a valid owner, that is, the owner of the file does not exist in/etc/passwd. -Newer file1! File2 searches for files whose change time is newer than file1 but older than file2. -Type: search for a certain type of files, such as B-block device files. D-directory. C-character device file. P-MPs queue file. L-Symbolic Link file. F-common file. -Size N: [c] searches for files with a length of N blocks. If a file contains C, the file length is measured in bytes. -Depth: when searching for a file, first find the file in the current directory and then find it in its subdirectory. -Fstype: searches for files in a certain type of file system. These file system types can usually be found in the configuration file/etc/fstab, this configuration file contains information about the file system in the system. -Mount: the mount point of the file system is not crossed during file search. -Follow: If the find command encounters a symbolic link file, it will trace the file to which the link points. -Cpio: Use the cpio command to back up the files to the tape device. -Amin n  Find the files accessed in the last n minutes of the system    -Atime n  Find the last n * 24 hours of files in the system    -Cmin n Find the file whose status is changed in the last n minutes in the system    -Ctime n  Find the file whose status is changed in the last n * 24 hours in the system.    -Mmin n  Find the file whose data is changed in the last n minutes in the system    -Mtime n  Find the file whose data has been changed for the last n * 24 hours in the system. 

A small example of the find command shell script is as follows ):

 
#! /Bin/shfor afile in 'Find. /-name "*" '; do # echo $ afile; if [-F "$ afile"]; then wordnum = 'cat $ afile | grep $1 | WC-l'; if ["$ wordnum"-ge 2]; Then ECHO $ afile; FI fidone

If the shell script is named find_word.sh and the current user is granted executable permissions, run the following command:

[@ 131.169 test] # find_word.sh Ni

Search for files containing Ni

Related Article

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.