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 usage of find command in linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is about the use of the find command in linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In linux, the find command is used to find files in a specified directory, with the basic syntax "find path-option..". Any string that precedes a parameter is treated as the directory name you are looking for; if no parameters are set when using this command, the find command looks for subdirectories and files in the current directory.

The operating environment of this tutorial: Red Hat Enterprise Linux 6.1 system, Dell G3 computer.

The Linux find command is used to find files in a specified directory.

Find command format: find path-option [- print] [- exec-ok | xargs | grep] [command {}\;]

The parameters of the find command:

1) path: the directory path to find.

~ represents the $HOME directory

. Represents the current directory

/ indicates the root directory

2) print: means to output the result to standard output.

3) exec: execute the shell command given by this parameter on the matching file.

The form is command {}\;, notice that there is a space between {} and\;

4) ok: same as exec

The difference is that before the command is executed, a prompt is given to the user to confirm whether to execute it.

5) | xargs plays the same role as exec, so it plays an important role.

The difference is that | xargs is mainly used to undertake deletion operations, while-exec can be used such as copy, move, rename, etc.

6) options: indicates the search method

The following options are commonly used in options:

-name filename # find a file named filename-perm # find by execution permission-user username # find by file owner-group groupname # find by group-mtime-n + n # find files by file change time,-n means within n days + n refers to n days ago-atime-n + n # to find files by file access time,-n refers to within n days, + n refers to n days ago-ctime-n + n # to find files by file creation time,-n refers to within n days, + n refers to n days ago-nogroup # checks files that do not belong to a valid group That is, the subordinate group of the file does not exist in / etc/groups-nouser # check the file with no valid owner That is, the owner of the file is not stored in / etc/passwd-type b/d/c/p/l/f # looks up block devices, directories, character devices, pipes, symbolic links, Normal files-size n [c] # look for files of n blocks [or n bytes]-mount # look up files without crossing the file system mount point-follow # if you encounter a symbolic link file Just trace the file that the link refers to-prune # ignore a 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.

Here are some simple examples to introduce the general use of find:

1. Look up by name

In the current directory and subdirectories, find the txt file that begins with an uppercase letter

$find. -name'[Amurz] * .txt'- print

In / etc and its subdirectories, look for files that begin with host

$find / etc-name 'host*'-print

Find all files in the $HOME directory and its subdirectories

$find ~-name'*'- print

In the current directory and subdirectories, look for txt files that do not begin with out

$find. -name "out*"-prune-o-name "* .txt"-print

2. Search by catalog

Search for txt files in a subdirectory other than aa in the current directory

$find. -path ". / aa"-prune-o-name "* .txt"-print

Look for txt files in the current directory and subdirectories other than aa and bb

$find. \ (- path'. / dir0'-o-path'. / dir1'\)-a-prune-o-name'* .txt'- print

Note: you need to add spaces in 1 and 2, otherwise the error will appear as shown in the figure.

You can add-an in 3 places.

Look for the txt file in the current directory, no longer in the subdirectory

$find. !-name "."-type d-prune-o-type f-name "* .txt"-print

Or

Find. -name * .txt-type f-print

Links: detailed explanation of find command-path-prune usage in Linux

3. Search by permission

In the current directory and subdirectories, find the files for which the owner has read and write execution, and other files with read execution permission

$find. -perm 755-print

Find files or directories where the user has write permission or the group user has write permission

Find. /-perm / 220 find. /-perm / upriwjournal gendarmerw find. /-perm / upliwjingwjigwjingw

4. Search by type (b/d/c/p/l/f)

Look for symbolic link files in the current directory and subdirectories

$find. -type l-print

5. By owner and group

Find the file whose owner is www

$find /-user www-type f-print

Find the file whose owner has been deleted

$find /-nouser-type f-print

Find files that belong to group mysql

$find /-group mysql-type f-print

Find files whose user groups have been deleted

$find /-nogroup-type f-print

6. Search by time

Find files that have been changed within 2 days

$find. -mtime-2-type f-print

Find files that were changed 2 days ago

$find. -mtime + 2-type f-print

Find files accessed within a day

$find. -atime-1-type f-print

Find files that were accessed a day ago

$find. -atime + 1-type f-print

Find files whose status has been changed within a day

$find. -ctime-1-type f-print

Find files whose status was changed a day ago

$find. -ctime + 1-type f-print

Find files whose status was changed 10 minutes ago

$find. -cmin + 10-type f-print

7. Press the new and old documents

Find files newer than aa.txt

$find. -newer "aa.txt"-type f-print

Find files older than aa.txt

$find. !-newer "aa.txt"-type f-print

Find files newer than aa.txt and older than bb.txt

$find. -newer 'aa.txt'!-newer' bb.txt'-type f-print

8. Find by size

Find files that exceed 1m

$find /-size + 1m-type f-print

Find files equal to 6 bytes

$find. -size 6c-print

Find files less than 32k

$find. -size-32k-print

9. Execute orders

1) find del.txt and delete it. Prompt for confirmation before deletion.

$find. Name 'del.txt'-ok rm {}\;

2) find aa.txt and back it up as aa.txt.bak

$find. -name 'aa.txt'-exec cp {}. Bak\

3) check all 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 2003 2003-02-25. / conf.d/README

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

4) look for files that were changed before 5 days in the / logs directory and delete them

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

5) query the files modified on the same day

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

6) query the file and ask if you want to display

# 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 # find. /-mtime-1-type f-ok ls-l {}\

< ls … ./classDB.inc.php >

? N

About whether there is a difference between-print

Add-print

Find the directory and list the files under the directory (execute the ls command separately for each directory found, and the directory name will not be displayed on the first line of the file list without the option-print)

Find / home-type d-print-exec ls {}\

No-print

Thank you for reading! This is the end of the article on "the use of find commands in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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