Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The most complex command in Linux-the find command

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Find

Find files in the specified directory

Supplementary explanation

The find command is used to find files in the specified directory. Any string that precedes a parameter is treated as the name of the directory you are looking for. If you use this command without setting any parameters, the find command looks for subdirectories and files under the current directory. And all the subdirectories and files found are displayed.

Grammar

Find (option) (parameter)

one

Option

-amin: find files or directories that have been accessed at a specified time (in minutes)

-anewer: find that the access time is closer to the current file or directory than the specified file or directory

-atime: find files or directories that have been accessed at a specified time, calculated in 24 hours

-cmin: find files or directories that have been changed at a specified time

-cnewer finds that the change time is closer to the current file or directory than that of the specified file or directory

-ctime: find files or directories that have been changed at a specified time, calculated in 24 hours

-daystart: calculate the time from today

-depth: start 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: if the return value of the find instruction is True, the instruction is executed

-false: set the return value of the find instruction to False

-fls: the effect of this parameter is similar to specifying the "- ls" parameter, but saves the result as the specified list file

-follow: excluding symbolic links

-fprint: the effect of this parameter is similar to that of specifying the "- print" parameter, but saves the result as the specified list file

-fprint0: the effect of this parameter is similar to that of specifying the "- print0" parameter, but saves the result as the specified list file

-fprintf: the effect of this parameter is similar to that of specifying the "- printf" parameter, but saves the result as the specified list file

-fstype: only look for files or directories under this file system type

-gid: find files or directories that match the specified group identification number

-group: find files or directories that match the specified group name

-help or-- help: online help

-ilname: the effect of this parameter is similar to specifying the "- lname" parameter, but ignores the difference in character case

-iname: the effect of this parameter is similar to specifying the "- name" parameter, but ignores the difference in character case

-inum: find files or directories that match the specified inode number

-ipath: the effect of this parameter is similar to specifying the "- path" parameter, but ignores the difference in character case

-iregex: the effect of this parameter is similar to specifying the "- regexe" parameter, but ignores the difference in character case

-links: find files or directories that match the specified number of hard connections

-iname: specifies a string as a template style for finding symbolic links

-ls: if the return value of the find instruction is Ture, the file or directory name is listed to standard output

-maxdepth: sets the maximum directory level

-mindepth: sets the minimum directory level

-mmin: find files or directories that have been changed at a specified time (in minutes)

-mount: the effect of this parameter is the same as the specified "- xdev"

-mtime: find files or directories that have been changed at a specified time, calculated in 24 hours

-name: specifies a string as a template style for finding files or directories

-newer: find that the change time is closer to the current file or directory than that of the specified file or directory

-nogroup: find files or directories that do not belong to the local host group identification code

-noleaf: do not consider that the directory needs to have at least two hard connections

-nouser: find files or directories that do not belong to the user identification code of the local host

-ok: the effect of this parameter is similar to that of specifying "- exec", but the user is asked before the instruction is executed. If you answer "y" or "Y", the command is abandoned.

-path: specifies the string as the template style for finding directories

-perm: find files or directories that match the specified permission values

-print: if the return value of the find instruction is Ture, the file or directory name is listed to standard output. The format is one name for each column, and each name is preceded by a ". /" string

-print0: if the return value of the find instruction is Ture, the file or directory name is listed to standard output. All names in the format are on the same line

-printf: if the return value of the find instruction is Ture, the file or directory name is listed to standard output. The format can be specified by yourself

-prune: do not look for strings as template styles for finding files or directories

-regex: specifies a string as a template style for finding files or directories

-size: find files that match the specified file size

-true: set the return value of the find instruction to True

-typ: only look for files that match the specified file type

-uid: find files or directories that match the specified user identification number

-used: find files or directories that have been accessed at a specified time after the file or directory has been changed, in daily terms

-user: the file or directory of the finder and the specified owner name

-version or-- version: displays version information

-xdev: limit the scope to the advance file system

-xtype: this parameter has a similar effect to specifying the "- type" parameter, except that it checks for symbolic connections.

Parameters.

Start directory: find the starting directory of the file.

Example

The current directory searches all files with contents of "140.206.111.111"

Find. -type f-name "*" | xargs grep "192.168.99.23"

one

two

Match based on file or regular expression

Lists all files and folders in the current directory and subdirectories

Find.

one

Look for file names ending in .txt in the / home directory

Find / home-name "* .txt"

one

Same as above, but ignore case

Find / home-iname "* .txt"

one

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

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

Or

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

one

two

three

Match file path or file

Find / usr/-path "local"

one

Matching file paths based on regular expressions

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

one

Same as above, but ignore case

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

one

Negative parameter

Find files under / home that do not end in .txt

Find / home!-name "* .txt"

one

Search by file type

Find. -type type parameter

one

Type parameter list:

F ordinary file

L symbolic connection

D directory

C character device

B block equipment

S socket

P Fifo

Search based on directory depth

The maximum downward depth is limited to 3

Find. -maxdepth 3-type f

one

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

Find. -mindepth 2-type f

one

Search based on file timestamp

Find. -type f timestamp

one

The UNIX/Linux file system has three timestamps for each file:

Access time (- atime/ days,-amin/ minutes): the last time the user visited.

Modification time (- mtime/ days,-mmin/ minutes): the last time the file was modified.

Change time (- ctime/ days,-cmin/ minutes): the last time the file data element (such as permissions, etc.) was modified.

Search for all files accessed in the last seven days

Find. -type f-atime-7

one

Search for all files that were accessed exactly seven days ago

Find. -type f-atime 7

one

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

Find. -type f-atime + 7

one

Search all files that have been accessed for more than 10 minutes

Find. -type f-amin + 10

one

Find all the files that take longer to modify than file.log

Find. -type f-newer file.log

one

Match based on file size

Find. -type f-size file size unit

one

File size unit:

B-block (512 bytes)

C-byte

W-word (2 bytes)

K-kilobytes

M-megabytes

G-gigabyte

Search for files greater than 10KB

Insert find here. -type f-size + 10k

one

Search for files less than 10KB

Find. -type f-size-10k

one

Search for files equal to 10KB

Find. -type f-size 10k

one

Delete matching file

Delete all .txt files in the current directory

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

one

Match based on file permissions / ownership

Search for files with permissions of 777 in the current directory

Find. -type f-perm 777

one

Find out the php files in the current directory whose permissions are not 644.

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

one

Find out all the files owned by the current directory user tom

Find. -type f-user tom

one

Find out all the files owned by the current directory user group sunk

Find. -type f-group sunk

one

Use the-exec option in conjunction with other commands

Find all the root files in the current directory and change the ownership to the user tom

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

one

In the above example, {} is used in conjunction with the-exec option to match all files, which is then replaced with the appropriate file name.

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

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

one

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

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

Find. -type f-name "* .txt"-exec cat {}\; > all.txt

one

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

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

one

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

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

one

Because multiple commands cannot be used in the-exec parameter of a single-line command, the following method allows you to accept multiple commands after-exec

-exec. / text.sh {}\

one

Search but jump out of the specified directory

Find all .txt files in the current directory or subdirectory, but skip the subdirectory sk

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

one

Find collection of other techniques

To list all files with zero length

Find. -empty

one

Other examples

Find all the jpg files in the find ~-name 'jpg' # home directory. The-name parameter allows you to restrict the results to files that match a given pattern.

Find ~-iname 'jpg' #-iname is like-name, but is not case-sensitive

Find ~ (- iname 'jpeg'-o-iname' jpg') # some pictures may be .jpeg extensions. Fortunately, we can combine patterns with "OR" (represented as-o).

Find ~ (- iname 'jpeg'-o-iname' jpg')-type f # what if you have some directories that end with jpg? Why you want to name a directory of bucketofjpg instead of pictures is beyond the scope of this article. ) We use the-type parameter to modify our command to find the file.

Find ~ (- iname 'jpeg'-o-iname' jpg')-type d # maybe you want to find those strangely named directories so that you can rename them later

We've taken a lot of pictures recently, so let's narrow it down to the file we changed last week.

Find ~ (- iname 'jpeg'-o-iname' jpg')-type f-mtime-7

one

You can perform time filtering based on file status change time (ctime), modification time (mtime), or access time (atime). These are in a few days, so if you want more fine-grained control, you can express it in a few minutes (cmin, mmin and amin, respectively). Unless you know exactly when you want, you may add a number after + (greater than) or-(less than).

But maybe you don't care about your picture. Maybe you don't have enough disk space, so you want to find all the huge (let's define as "greater than 1GB") files in the log directory:

Find / var/log-size + 1G

one

Or, maybe you want to find all the files owned by bcotton in / data:

Find / data-owner bcotton

one

You can also find files according to your permissions. Maybe you want to find files that are readable to everyone in your home directory to make sure you don't overshare.

Find ~-perm-otakr

one

Count the number of lines of code

Find. -name "* .java" | xargs cat | grep-v ^ $| count of wc-l # lines of code to exclude blank lines

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report