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

Linux's method of finding files using the Find command

2024-07-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article takes a look at how Linux uses the Find command to find files. There is a certain reference value, friends in need can refer to, hope to help you.

The Linux Find command is one of the most powerful tools in the Linux system management staff library. It allows us to search for files and directories in the directory hierarchy according to the expressions given by the user, and apply user-specified actions to each matching file.

Linux Find command

Use the find command to search for files based on file permissions, type, date, ownership, size, etc. It can also be used in conjunction with other tools, such as grep or sed to perform operations on these files

Before we discuss how to use the find command, let's take a look at the basic syntax:

Find [options] [path...] [expression]

Description:

● options properties: controls the handling of symbolic links, debugging options, and optimization methods.

● path... Attribute: defines the starting directory where find will search for files.

The ● expression property: consists of "options", "search mode", and "operator delimited operations".

Let's look at the following example:

Find-L / home/projects/-name "* .js"-exec chmod 644 {}\

Description: this command contains a parameter-L (options), which allows the find command to follow the symbolic link, search the entire directory tree under / home/projects/ (path...), find all files ending in .js (expression), and set the permissions for all matching files to 644.

How do I find files by type?

To specify the file type to look for, you need to use the-type parameter.

You can specify the file type using the following descriptors:

● f: regular files

● d: directory

● l: symbolic links

● c: role device

● b: block Devic

● p: named pipe (FIFO)

● s: socket

Example:

To find all the directories in the current working directory, you can use:

Find. -type d

If you want to list all character devices on the system type:

Find /-type c

How do I find a file by name?

Finding files by name is probably the most common use of the find command. To find files by name, you need to pass the-name option along with the name of the file you want to search for.

Example:

To search for a file named document.pdf in the / home/linuxize directory, use the following command:

Sudo find / home/linuxize-type f-name document.pdf

"if you want to run a case-insensitive search, you can change the options with-name using the following command:"

Sudo find / home/linuxize-type f-iname document.pdf

Description: the above command will match Document.pdf,DOCUMENT.pdf.. Wait.

How do I find a file by extension?

Searching for files by extension is the same as searching for files by name.

For example:

To find all files that end in the. log.gzinside / var/log/nginx directory, you can use:

Find / var/log/nginx-type f-name'* .log.gz'

If you want to find all files that do not match the regular expression, * .log.gz can use this-not parameter. For example, to find all files that do not end with * .log.gz you, use:

Find / var/log/nginx-type f-not-name'* .log.gz'

How do I find files by size?

To find files based on file size, you need to pass the-size parameter along with the size condition. You can specify the file size using the following suffix:

● bvl 512 byte blocks (default)

● c: byte

● w: double-byte word

● k:Kilobytes

● M: megabytes

● G: gigabytes

For example:

To find all files with full 1024 bytes in the / tmp directory, run the following command:

Find / tmp-type f-size 1024c

The find command also allows us to search for files that are larger or smaller than the specified size. For example:

We can search for all files less than 1MB in the current working directory, which requires a minus sign before the size:

Find. -type f-size-1m

If you want to search for files larger than 1MB, you need to use the plus sign +:

Find. -type f-size + 1m

You can even search for files within the size range. For example, the following command finds all files between 1 and 2 MB:

Find. -type f-size + 1m-size 21m

How to find a file by the date it was modified?

The find command can also search for files based on when they were last modified, accessed, or changed.

As when searching by size, you can use the plus and minus signs to specify greater or less.

For example:

A few days ago you modified a dovecot configuration file, but you forgot the file name. We can easily filter all files in the / etc/dovecot/conf.d/ directory that ends in .conf, which have been modified in the past five days to:

Find / etc/dovecot/conf.d-name "* .conf"-mtime 5

We can also list all the files in the / home directory that we modified 30 days ago:

Find / home-mtime + 30-daystart

How do I find files by permissions?

To filter files based on file permissions, you need to use this-perm option.

For example:

To find all files with permission 775 in the / var/www/html directory:

Find / var/www/html-perm 644

You can also use a minus sign or a slash / to prefix a numeric pattern.

When slash / is used as a prefix, at least one category (user, group, or other category) must set at least the appropriate bit for file matching.

Find. -perm / 444

Description: search for all files that have read permissions set for users, groups, or others.

If you use a minus sign as a prefix, you must set at least the files to match.

Find. -perm-664

Description: search for files that have read and write access to owners and groups, and can be read by other users.

How do I find files by owner?

To find files owned by a specific user or group, you can use the-user and-group options.

For example, to search for all files and directories owned by the user linuxize, run:

Find /-user linuxize

This is a more advanced example, assuming that you want to find all the files owned by user www-data and change the ownership of matching files from www-data to nginx:

Find /-user www-data-type f-exec chown nginx {}\

About Linux uses the Find command to find the file method to share here, certainly not only the above and everybody analysis method, but the editor can guarantee its accuracy is absolutely no problem. I hope that the above content can have a certain reference value for everyone, and can be put into practice. If you like this article, you might as well 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