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

What are the examples of using the find command in Linux

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

Share

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

Editor to share with you what are the examples of the use of the find command in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. main contents

1. Find a file with a file name

two。 Find a file with a file name, ignoring case

3. Use mindepth and maxdepth to limit the depth of the search for the specified directory

4. Execute the command on the file found by the find command

5. Opposite matching

6. Use inode numbers to find files

7. Find files based on file permissions

8. Find all empty files (0-byte files) in the home directory and subdirectories

9. Find 5 * * files

10. Find the 5 smallest files

11. Use-type to find files of the specified file type

twelve。 Find the file by comparing the modification time with other files

13. Find files by file size

14. Alias common find operations

15. Delete large package files with the find command

16. Find files that were changed within 1 hour

17. Find files that have been accessed within 1 hour

18. Find files whose status has been changed within an hour

19. Search is limited to files only and folders are not displayed

20. Find only non-hidden files (do not show hidden files)

21. Find a file whose modification time is after a file has been modified

twenty-two。 Find files whose access time is after the modification time of a file

23. Find a file whose status change time is after the modification time of a file:

24. Use ls-l on the output of the find command to list the details of the files that have been edited within 1 hour

25. Search only in the current file system

twenty-six。 Use multiple {} in the same command

twenty-seven。 Use multiple {} instances

twenty-eight。 Redirect errors to / dev/nul

twenty-nine。 Replace the spaces in the file name with an underscore

thirty。 Execute two commands simultaneously in the find result

31. Common instructions

II. Summary of practical examples of find command

(1) query files based on name

# find. -name tecmint.txt

# find / home-name tecmint.txt

# find / home-iname tecmint.txt

# find /-type d-name Tecmint

# find. -type f-name tecmint.php

# find. -type f-name "* .php"

(2) query files based on permissions

# find. -type f-perm 0777-print

# find /-type f!-perm 777

# find /-perm 2644

# find /-perm 1551

# find /-perm / UBSs

# find /-perm / gems

# find /-perm / Ubunr

# find /-perm / astatx

# find /-type f-perm 0777-print-exec chmod 644 {}\

# find /-type d-perm 777-print-exec chmod 755 {}\

# find. -type f-name "tecmint.txt"-execrm-f {}\

# find. -type f-name "* .txt"-exec rm-f {}\

# find. -type f-name "* .mp3"-exec rm-f {}\

# find / tmp-type f-empty

# find / tmp-type d-empty

# find / tmp-type f-name ". *"

(3) query files based on users and groups

# find /-user root-name tecmint.txt

# find / home-user tecmint

# find / home-group developer

# find / home-user tecmint-iname "* .txt"

(4) query files or directories based on time

# find /-mtime 50

# find /-atime 50

# find /-mtime + 50-mtime-100

# find /-cmin-60

# find /-mmin-60

# find /-amin-60

(5) query files or directories based on size

# find /-size 50m

# find /-size + 50m-size-100m

# find /-size + 100m-exec rm-rf {}\

# find /-type f-name * .mp3-size + 10m-exec rm {}\

III. Detailed information

In addition to the basic operation of finding files under a directory structure, you can also use the find command to implement some practical operations to make your command line journey easier. This article introduces 31 Linux find commands that are useful for both beginners and veterans.

First, test the following find command example by creating the following empty file under your home directory.

# vimcreate_sample_files.sh

TouchMybashProgram.sh

Touch mycprogram.c

Touch MyCProgram.c

Touch Program.c

Mkdir backup

Cd backup

TouchMybashProgram.sh

Touch mycprogram.c

Touch MyCProgram.c

Touch Program.c

# chmod + xcreate_sample_files.sh

#. / create_sample_files.sh

# ls-R

.:

Backup MybashProgram.sh MyCProgram.c

Create_sample_files.sh mycprogram.c Program.c

. / backup:

MybashProgram.sh mycprogram.c MyCProgram.c Program.c

1. Find a file with a file name

This is a basic use of the find command. The following example shows how to use MyCProgram.c as the lookup name to find files in the current directory and its subdirectories.

# find-name "MyCProgram.c"

. / backup/MyCProgram.c

. / MyCProgram.c

two。 Find a file with a file name, ignoring case

This is a basic use of the find command. The following example shows how to use MyCProgram.c as the lookup name to find files in the current directory and its subdirectories, ignoring case.

# find-iname "MyCProgram.c"

. / mycprogram.c

. / backup/mycprogram.c

. / backup/MyCProgram.c

. / MyCProgram.c

3. Use mindepth and maxdepth to limit the depth of the search for the specified directory

Look for the passwd file in the root directory and its subdirectories.

# find /-namepasswd

. / usr/share/doc/nss_ldap-253/pam.d/passwd

. / usr/bin/passwd

. / etc/pam.d/passwd

. / etc/passwd

Look for passwd in the root directory and its 1-tier deep subdirectory. (for example, root-level 1, and one sub-directory-level 2)

# find-maxdepth 2-name passwd

. / etc/passwd

Look for the passwd file in the root directory and its two-tier deep subdirectory. (e.g. root-level 1, and two sub-directories-level 2 and 3)

# find /-maxdepth 3-name passwd

. / usr/bin/passwd

. / etc/pam.d/passwd

. / etc/passwd

Look for the passwd file between the layer 2 subdirectory and the layer 4 subdirectory.

# find-mindepth 3-maxdepth 5-name passwd

. / usr/bin/passwd

. / etc/pam.d/passwd

4. Execute the command on the file found by the find command

The following example shows the find command to calculate the MD5 validation and for all case-insensitive files named "MyCProgram.c". {} will be replaced by the current file name.

Find-iname "MyCProgram.c"-exec md5sum {}\

D41d8cd98f00b204e9800998ecf8427e. / mycprogram.c

D41d8cd98f00b204e9800998ecf8427e. / backup/mycprogram.c

D41d8cd98f00b204e9800998ecf8427e. / backup/MyCProgram.c

D41d8cd98f00b204e9800998ecf8427e. / MyCProgram.c

5. Opposite matching

Displays all files or directories whose names are not MyCProgram.c. Because maxdepth is 1, only the files and directories in the current directory are displayed.

Find-maxdepth 1-not-iname "MyCProgram.c"

.

. / MybashProgram.sh

. / create_sample_files.sh

. / backup

. / Program.c

6. Use inode numbers to find files

Any file has a * inode number, so we can distinguish between files. Create two files with similar names, such as one with a space ending and one without.

Touch "test-file-name"

# touch "test-file-name"

[Note: There is aspace at the end]

# ls-1 test*

Test-file-name

Test-file-name

The output from ls cannot tell which file has a space ending. Using the option-I, you can see the inode number of the file, thus distinguishing the two files.

Ls-i1 test*

16187429test-file-name

16187430test-file-name

You can specify the inode number in the find command as shown below. Here, the find command renames a file with an inode number.

Find-inum 16187430-exec mv {} new-test-file-name\

# ls-i1 * test*

16187430new-test-file-name

16187429test-file-name

You can use this technique when you want to do something with poorly named files like the one above. For example, a file name named file?.txt has a special character in it. If you want to execute "rm file?.txt", all three files shown below will be deleted. So, use the following steps to delete the "file?.txt" file.

Ls

File1.txt file2.txt file?.txt

Find the inode number of each file.

Ls-i1

804178 file1.txt

804179 file2.txt

804180 file?.txt

As follows:? Use inode numbers to delete file names with special symbols.

Find-inum 804180-exec rm {}\

# ls

File1.txt file2.txt

[Note: The file withname "file?.txt" is now removed]

7. Find files based on file permissions

The following operations are reasonable:

Find a file with the specified permissions

Ignore other permission bits and check whether they match the specified permissions

Search based on the permissions of a given octal / symbolic expression

In this example, assume that the directory contains the following files. Note that the permissions of these files are different.

Ls-l

Total 0

-rwxrwxrwx 1 rootroot 0 2009-02-19 20:31 all_for_all

-rw-r--r-- 1 rootroot 0 2009-02-19 20:30 everybody_read

-1 rootroot 0 2009-02-19 20:31 no_for_all

-rw- 1 rootroot 0 2009-02-19 20:29 ordinary_file

-rw-r- 1 rootroot 0 2009-02-19 20:27 others_can_also_read

-r-1 rootroot 0 2009-02-19 20:27 others_can_only_read

Find the file with group read permissions. Use the following command to find a file in the current directory that has read permissions for the same group of users, ignoring other permissions for that file.

Find. -perm-g=r-type f-exec ls-l {}\

-rw-r--r-- 1 rootroot 0 2009-02-19 20:30. / everybody_read

-rwxrwxrwx 1 rootroot 0 2009-02-19 20:31. / all_for_all

-r-1 rootroot 0 2009-02-19 20:27. / others_can_only_read

-rw-r- 1 rootroot 0 2009-02-19 20:27. / others_can_also_read

Locate the file that has read-only access to the group user.

Find. -perm g=r-type f-exec ls-l {}\

-r-1 rootroot 0 2009-02-19 20:27. / others_can_only_read

Locate the file that has read-only access to the group users (in the form of octal permissions).

Find. -perm 040-type f-exec ls-l {}\

-r-1 rootroot 0 2009-02-19 20:27. / others_can_only_read

8. Find all empty files (0-byte files) in the home directory and subdirectories

Most of the output files of the following command are place hoders created by other programs in the locked file box.

Find ~-empty

List only the empty files in your home directory.

Find. -maxdepth 1-empty

Only non-hidden empty files in the current year's directory are listed.

Find. -maxdepth 1-empty-not-name ".

9. Find 5 * * files

The following command lists 5 * * files in the current directory and subdirectories. This will take some time, depending on the number of files that the command needs to process.

Find. -type f-execls-s {}\; | sort-n-r | head-5

10. Find the 5 smallest files

The method is similar to finding five * * files, except that the order of the sort is descending.

Find. -type f-execls-s {}\; | sort-n | head-5

In the above command, it is likely that all you see is an empty file (0-byte file). In this way, you can use the following command to list the smallest files instead of 0 bytes.

Find. -not-empty-type f-exec ls-s {}\; | sort-n | head-5

11. Use-type to find files of the specified file type

Find only socket files

Find. -type s

Find all the directories

Find. -type d

Find all the general files

Find. -type f

Find all hidden files

Find. -type f-name ". *"

Find all hidden directories

Find-type d-name ".

twelve。 Find the file by comparing the modification time with other files

Displays the files that made changes after the file was specified. The following find command displays all files that were created and modified after ordinary_file.

Ls-lrt

Total 0

-rw-r- 1 rootroot 0 2009-02-19 20:27 others_can_also_read

-r-1 rootroot 0 2009-02-19 20:27 others_can_only_read

-rw- 1 rootroot 0 2009-02-19 20:29 ordinary_file

-rw-r--r-- 1 rootroot 0 2009-02-19 20:30 everybody_read

-rwxrwxrwx 1 rootroot 0 2009-02-19 20:31 all_for_all

-1 rootroot 0 2009-02-19 20:31 no_for_all

# find-newerordinary_file

.

. / everybody_read

. / all_for_all

. / no_for_all

13. Find files by file size

Use the-size option to find files by file size.

Find a file larger than the specified file

Find ~-size + 100m

Find a file smaller than the specified file

Find ~-size-100m

Find files that match a given size

Find ~-size 100m

Note:-the finger is smaller than the given size and the + finger is larger than the given size. No symbol represents exactly the same size as a given size.

14. Alias common find operations

If you find something useful, you can give it an alias. And do it wherever you want.

Commonly used to delete a.out files.

Aliasrmao= "find.-iname a.out-exec rm {}\;"

# rmao

Delete the core file generated by the c program.

Alias rmc= "find.-iname core-exec rm {}\;"

# rmc

15. Delete large package files with the find command

The following command deletes * .zip files greater than 100m.

Find /-type f-name*.zip-size + 100m-exec rm-I {}\; "

Delete all heavy rain 100m * .tar files with alias rm100m. Using the same idea, you can create a class of aliases for rm1g,rm2g,rm5g to delete all files larger than 1G, 2G, and 5G.

Aliasrm100m= "find /-type f-name * .tar-size + 100m-exec rm-I {}\;"

# aliasrm1g= "find /-type f-name * .tar-size + 1G-exec rm-I {}\;"

# aliasrm2g= "find /-type f-name * .tar-size + 2G-exec rm-I {}\;"

# aliasrm5g= "find /-type f-name * .tar-size + 5G-exec rm-I {}\;"

# rm100m

# rm1g

# rm2g

# rm5g

Find files based on access / modify / change Tim

You can find files based on the time attributes of the following three files.

Access time file. The access time is updated when the file is accessed.

The time when the file was modified. When the contents of the file are modified, the modification time is updated.

The time when the file was changed. The change in the inode data that is updated when the time is changed.

In the following example, the difference between the min option and the time option is a parameter.

The argument takes its parameter to minutes. For example, 60 minutes (1 hour) = 60 minutes. Time parameter, set its parameter to 24 hours. For example, time 2 = 2 * 24 hours (2 days). Although the 24-hour calculation of doing so, the decimal part will be ignored, so 25 hours is 24 hours, and 47 hours is 24 hours, and only 48 hours is 48 hours. For a clearer reference to the man pages of some of the find commands of atime.

16. Find files that were changed within 1 hour

To find a file by its modification time, you can use the parameter-mmin-mtime. Here are the definitions of mmin and mtime in the man manual.

-mmin n file * once modified within n minutes

-mtime n file * once modified within 24 hours.

Execute the command in the following example, and you will find files or directories in the current directory and its subdirectories that were last modified within 1 hour (60 minutes).

# find. -amin-60

In the same way, execute the command in the following example, and you will find files that have been accessed within 24 hours (1 day) (file system root / under)

# find /-atime-1

17. Find files that have been accessed within 1 hour

To find a file by file access time, you can use the parameter-amin-atime. Here are the definitions of amin and atime in the man manual.

-amin n file * one visit is within n minutes

-atime n file * one visit is within 24 hours

Execute the command in the following example, and you will find files or directories in the current directory and its subdirectories that were last accessed within 1 hour (60 minutes).

# find. -amin-60

In the same way, execute the command in the following example, and you will find files that have been accessed within 24 hours (1 day) (file system root / under)

# find /-atime-1

18. Find files whose status has been changed within an hour

(translator's note: the change here more than the first example of changing the file content time is a different concept, here is to change the file inode data, such as file permissions, ownership, and so on.)

To find the change time of the file's inode, use the-cmin and-ctime options

The state of the-cmin n file is changed within n minutes

-ctime n file status is changed within 24 hours (that is, within n days)

(translator's note: if the above n is in the form of-n, it means within n minutes / day, and if n is + n, it means before n minutes / day)

The following example looks under the current directory and its subdirectories for files whose state has changed within an hour (that is, within 60 minutes):

# find. -cmin-60

By the same token, the following example is a list of files whose state has been changed within the next day (within 24 hours) of the root directory / and its subdirectories:

# find /-ctime-1

19. Search is limited to files only and folders are not displayed

The above example searches for not only files, but also folders. Because when a file is accessed, its folder will also be accessed, if you are not interested in the folder, you can use the-type f option

The following example shows files that have been modified within 30 minutes, and folders are not displayed:

# find/etc/sysconfig-amin-30

.

. / console

. / network-scripts

. / i18n

. / rhn

. / rhn/clientCaps.d

. / networking

. / networking/profiles

. / networking/profiles/default

. / networking/profiles/default/resolv.conf

. / networking/profiles/default/hosts

. / networking/devices

. / apm-scripts

[note: the output above contains files and folders]

# find/etc/sysconfig-amin-30-type f

. / i18n

. / networking/profiles/default/resolv.conf

. / networking/profiles/default/hosts

[note: the output above contains only files]

20. Find only non-hidden files (do not show hidden files):

If we don't want to hide the file and show it when we look for it, we can use the following regular formula to find it:

The following command displays files whose contents have been modified within 15 minutes under the current directory and its subdirectories, and lists only non-hidden files. In other words, in order to. The beginning of the file will not be displayed

# find. -mmin-15\ (!-regex ". * /\.. *"\)

Find command based on file comparison

It is easier for us to remember some things by comparing with other things. For example, I want to find out the files I edited after I edited the test file. You can use the editing time of the test file as a baseline to find later edited files:

21. Find a file whose modification time is after a file has been modified:

Syntax: find-newerFILE

The following example shows a file that has been modified after the / etc/passwd change. For the system administrator, it is helpful to know that you have added a user to track the activity of the system (in case the new user is dishonest and cheats up, you will soon know ^ _ ^):

# find-newer/etc/passwd

twenty-two。 Find a file whose access time is after the modification time of a file:

# find-newer/etc/passwd

The following example shows all the files accessed after the / etc/hosts file was modified. If you add a new host / port to the / etc/hosts file, you'll probably want to know what files have been accessed after that. Here's the command:

# find-anewer/etc/hosts

23. Find a file whose status change time is after the modification time of a file:

Syntax: find-cnewerFILE

The following example shows all files whose state has changed after modifying the file / etc/fstab. If you add a mount point to / etc/fstab, you probably want to know which files have changed since then, you can use the following command:

# find-cnewer/etc/fstab

Execute the command directly on the results of the list of files found:

You have seen before that if you use the find command to find a list of files for various conditions. If you are not familiar with these find commands, I suggest you finish reading the * * section above

In the next section, we will show you how to execute different commands on the find command, that is, how to manipulate the list of files found by the find command.

We can specify any action on the list of file names found by the find command:

# find-exec\

The OPERATION can be any command. Here is a list of the more commonly used commands:

The rm command, which is used to delete files found by find

Mv command to rename the found file

The ls-l command, which displays the details of the files found

Md5sum, a string can be obtained by performing md5sum operation on the found file, which is used to detect the validity of the file content.

Wc command, which is used to count the number of words in a file, file size, etc.

Execute any Unix Shell command

Execute the shell script you wrote yourself, and the parameter is the name of each file you find.

24. Use ls-l on the output of the find command to list the details of the files that have been edited within 1 hour

# find-mmin-60

. / cron

. / secure

# find-mmin-60-exec ls-l {}\

-rw- 1 root root 1028 Jun 21 15:01. / cron

-rw- 1 root root 831752 Jun 21 15:42. / secure

25. Search only in the current file system

Sometimes system administrators just want to search on / mounted file system partitions rather than other mounted partitions, such as / home/ mounted partitions. If you have multiple partitions mounted and you want to search under /, you can generally do this as follows

The following command searches for the file names at the end of .log under the root directory / and its subdirectories. If you have multiple partitions below /, this search will search for all mounted partitions:

# find /-name "* .log"

If we use the-xdev option, it will only be searched in the current file system. Here is the definition of-xdev found on the manpage of xdev:

-xdev Don't descenddirectories on other filesystems.

The following command searches the / directory and its subdirectories for all files ending in .log in the current file system (that is, the mounted file system), that is, if you have multiple partitions mounted under /, the following search will not search for other partitions (for example, / home/)

# find /-xdev-name "* .log"

twenty-six。 Use multiple {} in the same command

The linux manual says that only one {} can be used in a command, but you can use multiple {} in the same command as below.

# find-name "* .txt" cp {}. Bkup\

Note that it's okay to use this {} in the same command, but not in different commands, that is, it doesn't work if you imagine renaming the file as follows

Find-name "* .txt"-exec mv {} `basename {} .htm`.html\

twenty-seven。 Use multiple {} instances

You can write a shell script like this to simulate the renaming example above

# mv "$1"`basename" $1 ".htm`.html"

The double quotation marks above are used to prevent spaces in the file name, which will be problematic if you don't add them. Then you save the shell script as mv.sh, and you can use the find command as follows

Find-name "* .html"-exec. / mv.sh'{}'\

So, in any case, if you want to use the same file name multiple times in the execution of the find command, write a script first, and then execute the script through-exec in find, passing in the file name parameter. This is the easiest way.

twenty-eight。 Redirect errors to / dev/nul

Redirecting error output is generally not a good idea. An experienced programmer understands that it is important to display an error at the terminal and correct it in a timely manner.

In particular, redirecting errors in find commands is not a good practice. But if you really don't want to see those annoying mistakes, you want to redirect them to the null device (that is, the black hole device on linux, where anything thrown in disappears without a trace). You can do something like this.

Find-name "* .txt" 2 > > / dev/null

Sometimes this is very useful. For example, if you want to find all the * .conf files in the / directory through your own account, you will get a lot of "Permission denied" error messages, like this:

$find /-name "* .conf"

/ sbin/generate-modprobe.conf

Find:/tmp/orbit-root: Permission denied

Find:/tmp/ssh-gccBMp5019: Permission denied

Find:/tmp/keyring-5iqiGo: Permission denied

Find:/var/log/httpd: Permission denied

Find: / var/log/ppp:Permission denied

/ boot/grub/grub.conf

Find:/var/log/audit: Permission denied

Find:/var/log/squid: Permission denied

Find:/var/log/samba: Permission denied

Find:/var/cache/alchemist/printconf.rpm/wm: Permission denied

[Note: There are twovalid * .conf files burned in the "Permission denied" messages]

Do you say annoying? So, if you only want to see the actual search results of the find command instead of these "Permission denied" error messages, you can redirect these error messages to / dev/null

$find /-name "* .conf" 2 > > / dev/null

/ sbin/generate-modprobe.conf

/ boot/grub/grub.conf

[Note: All the "Permission denied" messages are not displayed]

twenty-nine。 Replace the spaces in the file name with an underscore

Many of the audio files you download from the Internet have spaces in their names. But file names with spaces are not good in linux (Unix-like) systems. You can use find and then add the replace function of the rename command to rename these files and convert spaces to underscores

The following shows how to replace the spaces in the filenames of all mp3 files with _

$find. -type f-iname "* .mp3"-exec rename "s / / _ / g" {}\

thirty。 Execute two commands simultaneously in the find result

On the man page page of find, the following is an example of a syntax for using two commands in a file lookup traversal

The following example of the find command, which traverses the file system once, lists the files and directories with the setuid attribute, writes to the / root/suid.txt file, and logs it to / root/big.txt if the file size is more than 100m

# find /\ (- perm-4000-fprintf / root/suid.txt'% # m% u% p\ n'\),\

\ (- size + 100M-fprintf / root/big.txt'%-10s% p\ n'\)

31. Common instructions

Find / backup/rman_backup/-mtime + 45-exec rm-rf {}\; delete files from 45 days ago

Find / tmp-mtime + 7-size + 1m-exec rm-rf {}\

Find-mtime + 7-nameabc*-exec rm-rf {}\

Find / tmp-mtime + 7-size + 1m-ok rm-rf {}\

These are all the contents of the article "what are the examples of the use of find commands in Linux?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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