Linux find-locating files under the specified directory

Source: Internet
Author: User
Tags file permissions

recommended: More Linux file Lookup and comparison commands attention:Linux commands Daquan

The Find command is used to locate files under the specified directory. Any string that precedes the parameter will be treated as the name of the directory you want to find. If you use this command without setting any parameters, the Find command looks for subdirectories and files under the current directory. and displays all the subdirectories and files that are found.

Grammar
Find (options) (parameters)
Options
-amin< minutes;: Finds files or directories that have been accessed at a specified time, in minutes;-anewer< reference files or directories;: Finds access times closer to the current file or directory than the specified file or directory;-atime< 24 hours number;: Finds files or directories that have been accessed at a specified time, and the unit calculates;-cmin< minutes in 24 hours;: Find files or directories that have been changed at the specified time;-cnewer< reference files or directories > Find the change time of the specified file or directory is closer to the current file or directory;-ctime<24: finds the file or directory that was changed at the specified time, measured in 24 hours;-daystart: Calculates the time from the current day -depth: Starting from the deepest subdirectory of the specified directory;-expty: Looking for a file with a file size of 0 byte, or an empty directory without any subdirectories or files in the directory;-exec< execution Instructions: Assuming that the return value of the Find command is true, execute the instruction;-false: Sets the callback value of the Find command to the false;-fls< list file;: The effect of this parameter and the specified "-ls "parameter is similar, but will save the result as the specified list file;-follow: Exclude Symbolic Connection;-fprint< list file; the effect of this parameter is similar to specifying the"-print "parameter, but saves the result 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 and the specified "-printf "parameter is similar, but will save the result 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 a file or directory that matches the specified group ID ;-group< Group name: Find a file or directory that matches the specified group name;Help or--help: Online helper;-ilname< template style;: The effect of this parameter is similar to specifying the "-lname" parameter, but ignores the difference in the case of the character;-iname< the template style; the effect of this parameter is similar to specifying the "-name" parameter , but ignores the difference in the case of characters;-inum<inode: finds files or directories that match the specified inode number;-ipath< template style; the effect of this parameter is similar to specifying the "-path" parameter, but ignores the difference in case of characters ;-iregex< template style;: The effect of this parameter is similar to specifying the "-regexe" parameter, but ignores the difference in the case of characters;-links< the number of connections;: Finds files or directories that match the specified number of hard connections;-iname< template style : Specifies a string as a template style for finding symbolic joins;-ls: Assuming that the return value of the Find directive is ture, the file or directory name is listed at the standard output;-maxdepth< directory level;: Set the maximum directory level;-mindepth< Directory level: 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, in 24-hour format;-name< template style > : Specifies a string as the template style for searching for a file or directory;-newer< reference file or directory;: Find the change time of the file or directory that you specified is closer to the current file or directory;-nogroup: Find a file or directory that does not belong to the local landlord cluster Group identification code ;-noleaf: Do not consider the directory must have at least two hard connections exist;-nouser: Find a file or directory that does not belong to the local host user identification code;-ok< execution instructions;: This parameter has the same effect as specifying "-exec", but asks the user before executing the instruction, if the answer "Y" or "y", discard the Execute command;-path< template style;: Specify a string as the template style for the search directory;-perm< permission value;: Find a file or directory that matches the specified number of permissions -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: If 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 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;-regex< template style;: Specify a string as the template style for finding a file or directory;-size< file size;: Find files that match the specified file size -true: The return value of the Find command is set to the true;-typ< file type;: Only look for files matching the specified file type;-uid< user identification code;: Find the number of files or directories that match the specified user ID;-used< days To find files or directories that have been accessed at a specified time after a file or directory has been changed, the unit calculates the;-user< owner name by day;: A file or directory with the specified owner name;-version or--version: Display version information -xdev: Confine the scope to the first file system;-xtype< file type;: The effect of this parameter and the specified "-Type "parameter is similar, except that it checks for symbolic connections. 
Parameters

Start directory: Find the starting directory for the file.

Instances are matched according to a file or regular expression

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

Find.

/homefind filenames ending in. txt under 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

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

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 --byte
    • W --word (2 bytes)
    • k --Kbytes
    • M --MBytes
    • G --gigabyte

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 -execOptions are used in conjunction with other commands

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

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

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

Cat {} \;> All.txt

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

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

Linux find-locating files under the specified directory

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.