Use of the Linux command (a) find

Source: Internet
Author: User

Working under Linux, some commands can be much more efficient.

For example, the Find command, his brother can be considered a necessary Linux command, almost every day to use them.

    • Find command
      • General form of the Find command
      • Common options and examples for the Find command
      • Find and Xargs

A. Find command

The Find command is a ubiquitous command and is one of the most useful commands in Linux. The Find command is used to search for files in a directory (and subdirectories), and you can specify some matching criteria, such as locating files by file name, file type, user, or even time stamp. Here's an example of the power of the Find command.

Usage:find [-h] [-l] [-P] [-olevel] [-D help|tree|search|stat|rates|opt|exec] [path ...] [Expression]

Simplify to: Find [path ...] [Expression]

Options:

  • -amin< minutes;: Finds files or directories that have been accessed at a specified time, measured in minutes;
  • -anewer< reference file or directory;: Find its access time more closely to the current file or directory than the specified file or directory;
  • -atime<24 hours;: Finds files or directories that have been accessed at a specified time, in 24-hour terms;
  • -cmin< minutes;: Finds files or directories that have been changed at the specified time;
  • -cnewer< reference file or directory > find its change time more closely to the current file or directory than the specified file or directory;
  • -ctime<24 hours;: Finds files or directories that have been changed at a specified time, measured in 24 hours;
  • -daystart: Calculates the time from the current day;-depth: Searches from the deepest subdirectory under the specified directory;
  • -expty: Look for files with a file size of 0 byte, or empty directories without any subdirectories or files in the directory;
  • -exec< execution instruction;: If the return value of the Find command is true, execute the instruction;
  • -false: Sets the callback value of the Find command to false;
  • -fls< list file: The effect of this parameter is similar to specifying the "-ls" parameter, but saves the result as a specified list file;
  • -follow: Exclude symbolic connections;
  • -fprint< list file: The effect of this parameter is similar to specifying the "-print" parameter, but the result is saved to the specified list file;
  • -fprint0< list file: The effect of this parameter is similar to specifying the "-print0" parameter, but the result is saved to the specified list file;
  • -fprintf< list file >< output format: The effect of this parameter is similar to specifying the "-printf" parameter, but the result is saved to the specified list file;
  • -fstype< file system type;: Only look for files or directories under the file system type;
  • -gid< group identification Code;: Find files or directories that match the specified group ID;
  • -group< Group name: Find a file or directory that matches the specified group name;
  • -help or--help: online help;
  • -ilname< template style;: This parameter has the same effect as specifying the "-lname" parameter, but ignores the difference in the case of the character;
  • -iname< template style;: This parameter has the same effect as specifying the "-name" parameter, but ignores the difference in the case of the character;
  • -inum<inode: Find a file or directory that matches the specified inode number;
  • -ipath< template style;: This parameter has the same effect as specifying the "-path" parameter, but ignores the difference in the case of the character;
  • -iregex< template style;: This parameter has the same effect as specifying the "-regexe" parameter, but ignores the difference in the case of the character;
  • -links< number of connections;: Find files or directories that match the specified number of hard connections;
  • -iname< template style;: Specifies a string as the template style for searching for symbolic connections ;
  • -ls: Assuming that the return value of the Find command is ture, the file or directory name is listed to the standard output;
  • -maxdepth< directory hierarchy;: Set the maximum directory level;
  • -mindepth< directory hierarchy;: Set the minimum directory level;
  • -mmin< minutes;: Finds files or directories that have been changed at a specified time, measured in minutes;
  • -mount: The effect of this parameter is the same as specifying "-xdev";
  • -mtime<24 hours;: Find files or directories that have been changed at a specified time, measured in 24 hours;
  • -name< template style;: Specifies a string as a template style for searching for a file or directory;
  • -newer< reference file or directory;: Find changes time more closely to the current file or directory than the specified file or directory;
  • -nogroup: Find files or directories that do not belong to the local landlord group identification code;
  • -noleaf: Do not consider the directory must have at least two hard connections exist;
  • -nouser: Find files or directories that do not belong to the local host user identification code;
  • -ok< execution instructions; the effect of this parameter is similar to specifying "-exec", but the user is asked before executing the instruction, and if "Y" or "Y" is answered, the execution of the command is discarded;
  • -path< template style;: Specifies the string as the template style for the search directory;
  • -perm< permission value;: Find the file or directory that matches the specified permission value;
  • -print: Assuming that the return value of the Find directive is ture, the file or directory name is listed to standard output. The format is a name for each column, with a "./" string before each name;
  • -print0: Assuming that the return value of the Find directive is ture, the file or directory name is listed to standard output. All names are in the same line;
  • -printf< output format: Assuming that the return value of the Find directive is ture, the file or directory name is listed to standard output. The format can be specified by itself;
  • -prune: Do not look for a string as a template to find a file or directory style (skip the directory);
  • -regex< template style;: Specifies a string as a template style for searching for a file or directory;
  • -size< file size;: Find files that match the specified file size;
  • -true: Sets the callback value of the Find command to true;
  • -typ< file type;: Only files that match the specified file type are found;
  • -uid< user identification Code;: Find a file or directory that matches the specified user ID;
  • -used< days;: Find files or directories that have been accessed at a specified time after a file or directory has been changed, and the units are calculated on a daily basis;
  • -user< name of the owner;: a file or directory with a finder and a specified owner name;
  • -version or--version: Displays version information;
  • -xdev: Confine the scope to the first file system;
  • -xtype< file type;: The effect of this parameter is similar to specifying the "-type" parameter, except that it checks for symbolic connections.

Example:

Match according to a file or regular expression

List all files and folders in the current directory and sub-directories

Find.

Look for filenames ending in. txt under the/home directory

Find/home-name "*.txt"

Ditto, but ignores case

Find/home-iname "*.txt"

Find all files ending in. txt and. pdf under the current directory and subdirectories

Find. \ (-name "*.txt"-o-name "*.pdf" \)

Or

Find. -name "*.txt"-o-name "*.pdf"

Match file path or file

find/usr/-path "*local*"

Matching file paths based on regular expressions

Find. -regex ". *\ (\.txt\|\.pdf\) $"

Ditto, but ignores case

Find. -iregex ". *\ (\.txt\|\.pdf\) $"

Negative parameters

Find files that are not ending with. txt under/home

Find/home! -name "*.txt"

Search by File type

Find. -type

Type parameter type parameter list:

    • F Common Files
    • L Symbolic Connection
    • Catalog D
    • C-Character device
    • B-Block Equipment
    • s socket
    • P Fifo

Deep search based on directory

The down maximum depth limit is

3 Find. -maxdepth 3-type F

Search for all files with a depth distance of at least 2 subdirectories in the current directory

Find. -mindepth 2-type F

Search by file timestamp

Find. -type F time Stamp

The Unix/linux file system has three timestamps per file:

    • Access Time (-atime/days,-amin/minutes): The user's last access time.
    • Modification time (-mtime/days,-mmin/minutes): The last time the file was modified.
    • Change Time (-ctime/days,-cmin/minutes): File data elements (such as permissions, and so on) last modified time.

Search for all files that have been visited in the last seven days

Find. -type f-atime-7

Search all files that were visited exactly seven days ago

Find. -type F-atime 7

Search for all files that have been accessed for more than seven days

Find. -type F-atime +7

Search for all files with access time exceeding 10 minutes

Find. -type F-amin +10

Find all files that are longer than the File.log modification time

Find. -type F-newer File.log

Match based on file size

Find. -type f-size File Size unit

File size unit:

    • b--block (512 bytes)
    • c--bytes
    • w--Word (2 bytes)
    • k--kb
    • m--MBytes
    • g--gigabytes

Search for files larger than 10KB

Find. -type f-size +10k

Search for files less than 10KB

Find. -type f-size-10k

Search for files equal to 10KB

Find. -type F-size 10k

Delete a matching file

Delete all. txt files in the current directory

Find. -type f-name "*.txt"-delete

Match by file permissions/ownership

Search for files with permission 777 under current directory

Find. -type F-perm 777

Find php files with permissions not 644 in the current directory

Find. -type f-name "*.php"! -perm 644

Find all files that the current directory user Tom owns

Find. -type F-user Tom

Find all files owned by the current directory user group sunk

Find. -type F-group sunk

Use the-EXEC option in conjunction with other commands

Locate all root files in the current directory and change ownership to user Tom

Find.-type f-user root-exec chown Tom {} \;

In the example above, {} is used in conjunction with the-EXEC option to match all files and then be replaced with the corresponding file name.

Find all the. txt files in your home directory and delete them

Find $HOME/. -name "*.txt"-ok rm {} \;

In the example above,-ok and-exec behave the same, but it gives a hint as to whether to perform the appropriate action.

Find all. txt files in the current directory and stitch them together to write to the All.txt file

Find. -type f-name "*.txt"-exec Cat {} \;> All.txt

Move the. log file from 30 days ago to the old directory

Find. -type f-mtime +30-name "*.log"-exec cp {} old \;

Find all. txt files in the current directory and print them as "file: File name"

Find. -type f-name "*.txt"-exec printf "File:%s\n" {} \;

Because multiple commands cannot be used in the-exec parameter in a single-line command, the following methods can be implemented to accept multiple commands after-exec

-exec./text.sh {} \;

Search but jump out of the specified directory

Find all. txt files in the current directory or sub-directory, but Skip subdirectories

SK Find. -path "./sk"-prune-o-name "*.txt"-print

Find other tips for collecting

To list all files of zero length

Find. -empty

Use of the Linux command (a) find

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.