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

How to use find Lookup Command in linux

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

Share

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

How to use the find search command in linux? for this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

1. Command format:

Find pathname-options [- print-exec-ok.]

2. Command function:

Used to find files in the file tree species and deal with them accordingly

3. Command parameters:

Pathname: the directory path that the find command looks for. For example, with. To represent the current directory and / to represent the system root directory.

The-print: find command outputs matching files to standard output.

The-exec: find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form 'command' {}\;, notice the space between {} and\;.

-ok: the function is the same as-exec, except that the shell command given by this parameter is executed in a more secure mode, and before each command is executed, a prompt is given to the user to determine whether to execute it.

4. Command options:

-name looks for files by file name.

-perm looks for files according to file permissions.

-prune uses this option to prevent the find command from looking in the currently specified directory, and if you also use the-depth option, the-prune will be ignored by the find command.

-user looks for files according to their owners.

-group looks for files by the group to which they belong.

-mtime-n + n finds the file according to the change time of the file,-n indicates that the file change time is within n days from now, and + n indicates that the file change time was n days ago. The find command also has the-atime and-ctime options, but they all have the-m time option.

-nogroup looks for a file that does not have a valid group, that is, the group to which the file belongs does not exist in / etc/groups.

-nouser looks for a file without a valid owner, that is, the owner of the file does not exist in / etc/passwd.

-newer file1! File2 looks for files whose change time is newer than the file file1 but older than the file file2.

-type looks for a certain type of file, such as:

B-block device file.

D-directory.

C-character device file.

P-pipe file.

L-symbolic link file.

F-ordinary file.

-size n: [C] looks for files with n blocks of file length, with c indicating that the file length is in bytes. -depth: when looking for files, first look for the files in the current directory, and then look in their subdirectories.

-fstype: look for files located in a certain type of file system, which can usually be found in the configuration file / etc/fstab, which contains information about the file system in this system.

-mount: does not cross the file system mount point when looking for files.

-follow: if the find command encounters a symbolic link file, it tracks to the file that the link points to.

-cpio: use the cpio command on matching files to back up the files to the tape device.

In addition, the differences between the following three:

-amin n to find the files accessed in the last N minutes of the system

-atime n to find the files accessed by the last 24 hours in the system

-cmin n finds the files in the system whose state has been changed in the last N minutes

-ctime n to find the files in the system whose state was changed in the last 24 hours

-mmin n finds the files in the system that were changed in the last N minutes.

-mtime n to find the files in the system that were changed in the last 24 hours.

5. Use examples:

Example 1: find files that have been modified within a specified time

Command:

Find-atime-2

Output:

The code is as follows:

[root@peidachang] # find-atime-2

.

. / logs/monitor

. / .bashrc

. / .bash _ profile

. / .bash _ history

Description:

Files that have been modified within 48 hours

Example 2: search according to keywords

Command:

Find. -name "* .log"

Output:

The code is as follows:

[root@localhost test] # find. -name "* .log"

. / log_link.log

. / log2014.log

. / test4/log3-2.log

. / test4/log3-3.log

. / test4/log3-1.log

. / log2013.log

. / log2012.log

. / log.log

. / test5/log5-2.log

. / test5/log5-3.log

. / test5/log.log

. / test5/log5-1.log

. / test5/test3/log3-2.log

. / test5/test3/log3-3.log

. / test5/test3/log3-1.log

. / test3/log3-2.log

. / test3/log3-3.log

. / test3/log3-1.log

Description:

Look for files that end in .log in the current directory. "." represents the current directory

Example 3: find a file according to the permissions of a directory or file

Command:

Find / opt/soft/test/-perm 777

Output:

The code is as follows:

[root@localhost test] # find / opt/soft/test/-perm 777

/ opt/soft/test/log_link.log

/ opt/soft/test/test4

/ opt/soft/test/test5/test3

/ opt/soft/test/test3

Description:

Find files with 777 permissions in the / opt/soft/test/ directory

Example 4: find by type

Command:

Find. -type f-name "* .log"

Output:

The code is as follows:

[root@localhost test] # find. -type f-name "* .log"

. / log2014.log

. / test4/log3-2.log

. / test4/log3-3.log

. / test4/log3-1.log

. / log2013.log

. / log2012.log

. / log.log

. / test5/log5-2.log

. / test5/log5-3.log

. / test5/log.log

. / test5/log5-1.log

. / test5/test3/log3-2.log

. / test5/test3/log3-3.log

. / test5/test3/log3-1.log

. / test3/log3-2.log

. / test3/log3-3.log

. / test3/log3-1.log

[root@localhost test] #

Description:

Find ordinary files that end in .log in the current directory

Example 5: find all current directories and sort them

Command:

Find. -type d | sort

Output:

The code is as follows:

[root@localhost test] # find. -type d | sort

.

. / scf

. / scf/bin

. / scf/doc

. / scf/lib

. / scf/service

. / scf/service/deploy

. / scf/service/deploy/info

. / scf/service/deploy/product

. / test3

. / test4

. / test5

. / test5/test3

[root@localhost test] #

Example 6: find files by size

Command:

Find. -size + 1000c-print

Output:

The code is as follows:

[root@localhost test] # find. -size + 1000c-print

.

. / test4

. / scf

. / scf/lib

. / scf/service

. / scf/service/deploy

. / scf/service/deploy/product

. / scf/service/deploy/info

. / scf/doc

. / scf/bin

. / log2012.log

. / test5

. / test5/test3

. / test3

[root@localhost test] #

Description:

Find files whose current directory is greater than 1K

Examples of common uses of find in Linux

Find path-option [- print] [- exec-ok command] {}\

#-print outputs the found files to standard output

#-exec command {}\;-command the checked file, with a space between {} and\;

#-ok and-exec are the same, except that you should ask the user = =-name filename # to find a file named filename before the operation

-perm # search by execute permission

-user username # search by file owner

-group groupname # find by group

-mtime-n + n # find files according to the time when the files are changed.-n refers to within n days, + n refers to n days ago.

-atime-n + n # look up GIN by file access time: 0px ">-perm # search by execution permission

-user username # search by file owner

-group groupname # find by group

-mtime-n + n # find files according to the time when the files are changed.-n refers to within n days, + n refers to n days ago.

-atime-n + n # look up files by file access time,-n refers to within n days, + n refers to n days ago

-ctime-n + n # find the file by the time it was created.-n refers to within n days, and + n refers to n days ago.

-nogroup # check that there is no valid group file, that is, the file group does not exist in / etc/groups

-nouser # check the file for which there is no valid owner, that is, the owner of the file is not saved in / etc/passwd

-newer F1! f2 look for files.-n refers to within n days, + n refers to n days ago.

-ctime-n + n # find the file by the time it was created.-n refers to within n days, and + n refers to n days ago.

-nogroup # check that there is no valid group file, that is, the file group does not exist in / etc/groups

-nouser # check the file for which there is no valid owner, that is, the owner of the file is not saved in / etc/passwd

-newer F1! f2 # check for files whose change time is newer than F1 but older than f2

-type b/d/c/p/l/f # check for block devices, directories, character devices, pipes, symbolic links, ordinary files

-size n [c] # look up files of length n blocks [or n bytes]

-depth # causes the lookup to find the local directory before entering the subdirectory

-fstype # check for files whose change time is newer than F1 but older than f2

-mount # do not cross the file system mount point when looking up files

-follow # if you encounter a symbolic link file, track the file that the link refers to

-cpio # uses the cpio command on matching files to back them up to the tape device

-prune # ignore a directory =

$find ~-name "* .txt"-print # look up the .txt file in $HOME and display

$find. -name "* .txt"-print

$find. -name "[A murz] *"-pri26nbsp; # uses the cpio command on matching files and backs them up to the tape device

-prune # ignores a directory $find. -name "[Amurz] *"-print # look for files that begin with uppercase letters

$find / etc-name "host*"-print # look for files that start with host

$find. -name "[a murz] [a Muoz] [0MULY9] [0MULFE9] .txt"-print # look for txt files that begin with two lowercase letters and two numbers

$find. -perm 755-print

$find. -perm-007-exec ls-l {}\; # check that all users can read and write the same files as-perm 777

$find. -type d-print print directory structure

$find. !-type d-print prints a non-directory file find / usr/include-name'* .h'- exec grep AF_INEF6 {}\; because grep cannot search subdirectories recursively, it can be used with find. Find the string AF_INEF6 in the .h file in all subdirectories of / usr/include

$find. -type l-print $find. -size + 1000000c-print # look for files whose length is greater than 1Mb

$find. -size 100c-print # look up files with length 100c

$find. -size + 10-print # check for overexpired files of 10 blocks (1 block = 512 bytes) $cd /

$find etc home apps-depth-print | cpio-ivcdC65536-o / dev/rmt0

$find / etc-name "passwd*"-exec grep "cnscn" {}\; # to see if there is a cnscn user

$find. -name "yao*" | xargs file

$find. -name "yao*" | xargs echo "" > / tmp/core.log

$find. -name "yao*" | xargs chmod Omurw = find-name april* looks for files starting with april in the current directory

Find-name april* fprint file looks for files starting with april in the current directory and outputs the results to file

Find-name ap*-o-name may* finds files that start with ap or may

Find / mnt-name tom.txt-ftype vfat looks for files with the name tom.txt and file system type vfat under / mnt

Find / mnt-name t.txt!-ftype vfat looks for files with the name tom.txt and file system type not vfat under / mnt

Find / tmp-name wa*-type l looks under / tmp for files whose name begins with wa and whose type is symbolic link

Find / home-mtime-2 check files that have been changed in the last two days under / home

Find / home-atime-1 check files that have been accessed within 1 day

Find / home-mmin + 60 check files that were changed 60 minutes ago under / home

Find / home-amin + 30 check files that have been accessed in the last 30 minutes

Find / home-newer tmp.txt looks under / home for files or directories that are updated more recently than tmp.txt

Find / home-anewer tmp.txt looks up files or directories with a shorter access time than tmp.txt under / home

Find / home-used-2 lists files or directories that have been accessed within 2 days after they have been changed

Find / home-user cnscn lists files or directories belonging to user cnscn in the / home directory

Find / home-uid + 501 lists the files or directories in the / home directory where the user's identification number is greater than 501

Find / home-group cnscn lists files or directories with group cnscn in / home

Find / home-gid 501lists files or directories with group id 501in / home

Find / home-nouser lists files or directories in / home that do not belong to local users

Find / home-nogroup lists files or directories within / home that do not belong to the local group

Find / home-name tmp.txt-maxdepth 4 list / home tmp.txt time check depth is up to 3 layers

Find / home-name tmp.txt-mindepth 3 starts from level 2.

Find / home-empty looks for files or empty directories of size 0

Find / home-size + 512k to check files larger than 512k

Find / home-size-512k check files less than 512k

Find / home-links + 2 check files or directories with more than 2 hard connections

Find / home-perm 0700 check files or directories with permissions of 700

Find / tmp-name tmp.txt-exec cat {}\

Find / tmp-name tmp.txt-ok rm {}\; find /-amin-10 # finds the files accessed in the last 10 minutes of the system

Find /-atime-2 # finds the files accessed in the last 48 hours of the system

Find /-empty # looks for files or folders that are empty in the system

Find /-groupcat # finds files that belong to groupcat in the system

Find /-mmin-5 # finds files that have been modified in the last 5 minutes of the system

Find /-mtime-1 # finds files that have been modified in the last 24 hours of the system

Find /-nouser # finds files that belong to invalid users in the system

Find /-user fred # finds files that belong to the user FRED in the system

Check all the ordinary files in the current directory

-# find. -type f-exec ls-l {}\

-rw-r--r-- 1 root root 34928 2003-02-25. / conf/httpd.conf

-rw-r--r-- 1 root root 12959 2003-02-25. / conf/magic

-rw-r--r-- 1 root root 180 2003-02-25. / conf.d/README

Check all the ordinary files in the current directory and use the ls-l command in the-e x e c option to list them

=

Look for files that were changed before 5 days in the / l o g s directory and delete them:

$find logs-type f-mtime + 5-exec-ok rm {}\

=

Query the files modified on the same day

[root@book class] # find. /-mtime-1-type f-exec ls-l {}\

=

Query the file and ask if you want to display

[root@book class] # find. /-mtime-1-type f-ok ls-l {}\

< ls ... ./classDB.inc.php >

? Y

-rw-r--r-- 1 cnscn cnscn 13709 January 12 12:22. / classDB.inc.php

[root@book class] # find. /-mtime-1-type f-ok ls-l {}\

< ls ... ./classDB.inc.php >

? N

[root@book class] # =

Query and give it to awk to handle

[root@book class] # who | awk'{print $1 "\ t" $2}'

Cnscn pts/0 =

Awk---grep---sed [root@book class] # df-k | awk'{print $1}'| grep- v 'none' | sed s "/\ / dev\ / g"

File system

Sda2

Sda1

[root@book class] # df-k | awk'{print $1}'| grep-v 'none'

File system

/ dev/sda2

/ dev/sda1

1) find all * .h in / tmp, look for "SYSCALL_VECTOR" in these files, and finally print out all file names containing "SYSCALL_VECTOR" A) find / tmp-name "* .h" | xargs-n50 grep SYSCALL_VECTOR

B) grep SYSCALL_VECTOR / tmp/*.h | cut-dazzlement'- F1 | uniq > filename

C) find / tmp-name "* .h"-exec grep "SYSCALL_VECTOR" {}\;-print

2) find /-name filename-exec rm-rf {}\

Find /-name filename-ok rm-rf {}\

3) for example, to find files larger than 3m on disk:

Find. -size + 3000k-exec ls-ld {}

4) copy what comes out of find to another place

Find * .c-exec cp'{}'/ tmp';'if you have a special file, you can use cpio or this syntax:

Find dir-name filename-print | cpio-pdv newdir

6) find files that were changed at 16:36:37 on 2004-11-30

# A = `find. /-name "* php" `| ls-1-- full-time $A2 > / dev/null | grep "2004-11-30 16:36:37 |

II. The usage of find command under linux

1. Basic usage:

Find /-name file name find ver1.d ver2.d-name'* .c'- print find the ver1.d,ver2.d * .c file and print find. -type d-print looks from the current directory, looks only for the directory, and prints the path name when found. Can be used to print the directory structure.

two。 No error lookup:

Find /-name access_log 2 > / dev/null

3. Find by size:

Find /-size 1500c (find files with a size of 1500 bytes, c for bytes)

Find /-size + 1500c (find files larger than 1500 bytes, + means greater)

Find /-size + 1500c (find files with a size less than 1500 bytes,-means less than)

4. By time:

Find /-amin n last n minutes

Find /-atime n last n days

Find /-cmin n changes the state in the last n minutes

Find /-ctime n changes status in the last n days

5. Other:

Find /-empty blank files, blank folders, folders without subdirectories

Find /-false finds files that are always wrong in the system

Find /-fstype type finds files that exist in the specified file system, such as ext2 for type

Find /-gid n group files with id n

Find /-group gname group file named gname

Find /-depth n gives priority to finding the contents of files in the specified directory of a certain layer

Find /-maxdepth levels looks up in a hierarchical directory in a decreasing manner

6. Logic

-and condition and-or condition or

7. Find string

Find. -name'* .html'- exec grep 'mailto:' {}

This is the answer to the question about how to use the find search command in linux. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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