In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "linux find command format and find command how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "linux find command format and find command how to use" this article.
1. The general form of the find command is
Find pathname-options [- print-exec-ok.]
2. Parameters of the find command
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.
3. Find command options
-name
Find the file by file name.
-perm
Find files according to file permissions.
-prune
Use this option to prevent the find command from looking in the currently specified directory, and if you use the-depth option at the same time, the-prune will be ignored by the find command.
-user
Find the file according to the owner of the file.
-group
Find the file by the group to which the file belongs.
-mtime-n + n
Find the file according to the change time of the file,-n means that the file change time is within n days from now, and + n means 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
Look 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
Find a file that does not have a valid owner, that is, the owner of the file does not exist in / etc/passwd.
-newer file1! File2
Find files whose change time is newer than the file file1 but older than the file file2.
-type
Find 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
Find the files accessed in * * N minutes in the system
-atime n
Find files in the system accessed by * 24 hours a day
-cmin n
Find the files in the system that have been changed in * * N minutes.
-ctime n
Find the files in the system that have been changed in 24 hours.
-mmin n
Find the files in the system where the file data has been changed in * * N minutes.
-mtime n
Find the files in the system that have been changed in 24 hours.
4. Use exec or ok to execute shell commands
When using the find command, as long as the desired operation is written in a file, you can use exec to cooperate with the find command to find it, which is very convenient
In some operating systems, only the-exec option is allowed to execute commands such as ls or ls-l. Most users use this option to find old files and delete them. It is recommended that before actually executing the rm command to delete files, * use the ls command to take a look at them and make sure that they are the files to be deleted.
The exec option is followed by the command or script to be executed, followed by a pair of {}, a space, and a semicolon. In order to use the exec option, you must also use the print option. If you verify the find command, you will find that it only prints the relative path and file name from the current path.
For example, to list matching files with the ls-l command, you can put the ls-l command in the-exec option of the find command
# 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
In the above example, the find command matches all the normal files in the current directory and uses the ls-l command in the-exec option to list them.
Look for files that were changed before 5 days in the / logs directory and delete them:
$find logs-type f-mtime + 5-exec rm {}
Remember: before deleting files in any way in shell, you should check the corresponding files first, and be careful! When using commands such as mv or rm, you can use the safe mode of the-exec option. It will prompt you before operating on each matched file.
In the following example, the find command looks in the current directory for all files whose names end with .log and changes more than 5 days, and deletes them, but prompts you before deleting them.
$find. -name "* .conf"-mtime + 5-ok rm {}
< rm ... ./conf/httpd.conf >? N
Press y key to delete the file, press n key not to delete.
Any form of command can be used in the-exec option.
In the following example, we use the grep command. The find command first matches all files with the file name "passwd*", such as passwd, passwd.old, passwd.bak, and then executes the grep command to see if a sam user exists in these files.
# find / etc-name "passwd*"-exec grep "sam" {}
Sam:x:501:501::/usr/sam:/bin/bash
Example of the find command
1. Find all the files in the current user's home directory:
The following two methods can be used
$find $HOME-print
$find ~-print
2. Let the master of the file in the current directory have read and write permissions, and the users of the group to which the file belongs and other users have read permission.
$find. -type f-perm 644-exec ls-l {}
3. In order to find all the ordinary files with zero file length in the system, and list their full paths
$find /-type f-size 0-exec ls-l {}
4. Look for ordinary files in the / var/logs directory that were changed before 7 days, and ask them before deleting them
$find / var/logs-type f-mtime + 7-ok rm {}
5. To find all the files in the system that belong to the root group
$find. -group root-exec ls-l {}
-rw-r--r-- 1 root root 595 October 31 01:09. / fie1
6. The find command will delete admin.log files with numeric suffixes when the access time in the directory has been 7 days.
This command only checks for three digits, so the suffix of the corresponding file should not exceed 999. Create several admin.log* files before you can use the following command
$find. -name "admin.log [0-9] [0-9] [0-9]"-atime-7-ok
Rm {}
< rm ... ./admin.log001 >? N
< rm ... ./admin.log002 >? N
< rm ... ./admin.log042 >? N
< rm ... ./admin.log942 >? N
7. To find and sort all directories in the current file system
$find. -type d | sort
8. To find all the rmt tape devices in the system
$find / dev/rmt-print
Xargs
Xargs-build and execute command lines from standard input
When processing matched files using the-exec option of the find command, the find command passes all matched files together to exec for execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error occurs a few minutes after the find command is run. The error message is usually "Parameter column too long" or "Parameter column overflow". This is what the xargs command is for, especially when used with the find command.
The find command passes the matching files to the xargs command, while the xargs command fetches only some files at a time, not all of them, unlike the-exec option. In this way, it can first process some of the files obtained by *, then the next batch, and so on.
In some systems, using the-exec option will initiate a corresponding process to process each matched file, instead of executing all the matched files as parameters at once; in some cases, there will be too many processes and poor system performance, so the efficiency is not high.
There is only one process using the xargs command. In addition, when using the xargs command, whether to get all the parameters at once or in batches, and the number of parameters each time will be determined according to the options of the command and the corresponding adjustable parameters in the system kernel.
Take a look at how the xargs command works with the find command, and give some examples.
The following example finds each ordinary file in the system, and then uses the xargs command to test which type of file they belong to.
# find. -type f-print | xargs file
. / .kde / Autostart/Autorun.desktop: UTF-8 Unicode English text
. / .kde / Autostart/.directory: ISO-8859 text
.
Look for the memory information dump file (core dump) throughout the system, and save the results to the / tmp/core.log file:
$find /-name "core"-print | xargs echo "" > / tmp/core.log
The execution of the above is too slow. I will look for it in the current directory instead.
# find. -name "file*"-print | xargs echo "" > / temp/core.log
# cat / temp/core.log
. / file6
Find all files in the current directory for which users have read, write, and execute permissions, and take back the corresponding write permissions:
# ls-l
Drwxrwxrwx 2 sam adm 4096 October 30 20:14 file6
-rwxrwxrwx 2 sam adm 0 October 31 01:01 http3.conf
-rwxrwxrwx 2 sam adm 0 October 31 01:01 httpd.conf
# find. -perm-7-print | xargs chmod Omurw
# ls-l
Drwxrwxr-x 2 sam adm 4096 October 30 20:14 file6
-rwxrwxr-x 2 sam adm 0 October 31 01:01 http3.conf
-rwxrwxr-x 2 sam adm 0 October 31 01:01 httpd.conf
Use the grep command to search for the word hostname in all ordinary files:
# find. -type f-print | xargs grep "hostname"
. / httpd1.conf:# different IP addresses or hostnames and have them handled by the
. / httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
On your
Use the grep command to search for the word hostnames in all ordinary files in the current directory:
# find. -name *-type f-print | xargs grep "hostnames"
. / httpd1.conf:# different IP addresses or hostnames and have them handled by the
. / httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
On your
Note that in the above example, it is used to cancel the special meaning of * in shell in the find command.
The find command in conjunction with exec and xargs enables the user to execute almost all commands on the matched file.
Parameters of the find command
The following is an example of some commonly used parameters of the find command, which can be checked when useful. Like the previous posts above, some of these parameters are used. You can also use man or check other posts in the forum to have the find command manual.
1. Use the name option
The file name option is the most commonly used option for the find command, either alone or in conjunction with other options.
You can use some kind of file name pattern to match files, and remember to enclose the file name pattern in quotation marks.
No matter what the current path is, if you want to look in your root directory $HOME for files whose names match * .txt, use ~ as the 'pathname' parameter, and the tilde ~ represents your $HOME directory.
$find ~-name "* .txt"-print
To find all the'* .txt 'files in the current directory and subdirectories, you can use:
$find. -name "* .txt"-print
To find files whose names begin with an uppercase letter in the current directory and subdirectories you want, you can use:
$find. -name "[Amurz] *"-print
To find files whose names begin with host in the / etc directory, you can use:
$find / etc-name "host*"-print
To find the files in the $HOME directory, you can use:
$find ~-name "*"-print or find. -print
If you want the system to run under heavy load, look for all the files from the root directory.
$find /-name "*"-print
If you want to find a file name in the current directory that starts with two lowercase letters, followed by two numbers, and * * is a .txt file, the following command returns a file named ax37.txt:
$find. -name "[a murz] [a murz] [0MULY9] [0MULFE9] .txt"-print
2. Use the perm option
According to the file permission mode, use the-perm option to find files by the file permission mode. * use octal permission representation.
If you look for files with a file permission bit of 755 in the current directory, that is, files that the file owner can read, write and execute, and other users can read and execute, you can use:
$find. -perm 755-print
There is another way of expression: put a horizontal bar in front of the octal number to indicate that they all match. For example,-007 is equivalent to 777, mai Yu 006 is equivalent to 666.
# ls-l
-rwxrwxr-x 2 sam adm 0 October 31 01:01 http3.conf
-rw-rw-rw- 1 sam adm 34890 October 31 00:57 httpd1.conf
-rwxrwxr-x 2 sam adm 0 October 31 01:01 httpd.conf
Drw-rw-rw- 2 gem group 4096 October 26 19:48 sam
-rw-rw-rw- 1 root root 2792 October 31 20:19 temp
# find. -perm 006
# find. -perm-006
. / sam
. / httpd1.conf
. / temp
-perm mode: file license is exactly in line with mode
-perm + mode: the license part of the file conforms to mode
-perm-mode: file license is fully compliant with mode
3. Ignore a directory
If you want to ignore a directory when looking for files, because you know that there are no files you are looking for, you can use the-prune option to indicate the directory you need to ignore. Be careful when using the-prune option, because if you also use the-depth option, the-prune option will be ignored by the find command.
If you want to look for files in the / apps directory, but not in the / apps/bin directory, you can use:
$find / apps-path "/ apps/bin"-prune-o-print
4. How to avoid a file directory when using find to find files
For example, to find all the files in the / usr/sam directory that are not in the Dir1 subdirectory
Find / usr/sam-path "/ usr/sam/dir1"-prune-o-print
Find [- path..] [expression] after the path list is the expression
-path "/ usr/sam"-prune-o-print is-path "/ usr/sam"-a-prune-o
The abbreviated expressions of-print are evaluated sequentially, and both-an and-o are short-circuited, similar to the & & and | of shell. If-path "/ usr/sam" is true, the evaluation-prune,-prune returns true, and the logical expression is true; otherwise, it does not evaluate-prune, and the logical expression is false. If-path "/ usr/sam"-a-prune is false, the evaluation-print,-print returns true, or the logical expression is true; otherwise, it does not evaluate-print, or the logical expression is true.
The special case of this expression combination can be written in pseudo code as
If-path "/ usr/sam" then
-prune
Else
Avoid multiple folders
Find / usr/sam (- path / usr/sam/dir1-o-path / usr/sam/file1)-prune-o-print
Parentheses indicate the combination of expressions.
Indicates a reference, which instructs shell not to make a special interpretation of the following characters, but leaves it to the find command to explain its meaning.
Find a certain file, and add options such as-name after-o
# find / usr/sam (- path / usr/sam/dir1-o-path / usr/sam/file1)-prune-o-name "temp"-print
5. Use the user and nouser options
To find a file by file owner, such as a file whose owner is sam in the $HOME directory, you can use:
$find ~-user sam-print
Look for the file whose owner is uucp in the / etc directory:
$find / etc-user uucp-print
To find files where the master account has been deleted, you can use the-nouser option. This allows you to find files where the owner does not have a valid account in the / etc/passwd file. When using the-nouser option, you don't have to give a user name; the find command can do the job for you.
For example, if you want to find all such files in the / home directory, you can use:
$find / home-nouser-print
6. Use the group and nogroup options
Just like the user and nouser options, the find command has the same option for the user group to which the file belongs. To find files that belong to the gem user group in the / apps directory, you can use:
$find / apps-group gem-print
To find all files that do not have a valid user group, you can use the nogroup option. The following find command looks for such a file from the root directory of the file system
$find /-nogroup-print
7. Find files according to the change time or access time
If you want to find the file by when it was changed, you can use the mtime,atime or ctime option. If the system suddenly runs out of free space, there is a good chance that the length of a file will grow rapidly during this period, and you can use the mtime option to find such a file.
Use the minus sign to limit files whose change time is less than n days, and the plus sign to limit files whose change time is less than n days.
If you want to find files with a change time of less than 5 days in the system root directory, you can use:
$find /-mtime-5-print
To find files with a change time before 3 days in the / var/adm directory, you can use:
$find / var/adm-mtime + 3-print
8. Find a file that is newer or older than a file
If you want to find all files whose change time is newer than one file but older than another, you can use the-newer option. Its general form is:
Newest_file_name! Oldest_file_name
Among them,! It's logical, not symbolic.
Find files whose change time is newer than the file sam but older than the file temp:
There are two files
-rw-r--r-- 1 sam adm 0 October 31 01:07 fiel
-rw-rw-rw- 1 sam adm 34890 October 31 00:57 httpd1.conf
-rwxrwxr-x 2 sam adm 0 October 31 01:01 httpd.conf
Drw-rw-rw- 2 gem group 4096 October 26 19:48 sam
-rw-rw-rw- 1 root root 2792 October 31 20:19 temp
# find-newer httpd1.conf!-newer temp-ls
1077669 0-rwxrwxr-x 2 sam adm 0 October 31 01:01. / httpd.conf
1077671 4-rw-rw-rw- 1 root root 2792 October 31 20:19. / temp
1077673 0-rw-r--r-- 1 sam adm 0 October 31 01:07. / fiel
Find files whose change time is newer than temp files:
$find. -newer temp-print
9. Use the type option
To find all directories under the / etc directory, you can use:
$find / etc-type d-print
To find all types of files except the directory under the current directory, you can use:
$find. !-type d-print
Find all the symbolic link files in the / etc directory, you can use the
$find / etc-type l-print
10. Use the size option
Files can be found by file length, which can be measured either in blocks (block) or in bytes. The expression of measuring file length in bytes is NC; measuring file length in blocks can only be expressed in numbers.
When looking up files by file length, you generally use this file length in bytes to look at the size of the file system, because it is easier to convert when using blocks to measure.
Look for files with a length greater than 1 M bytes in the current directory:
$find. -size + 1000000c-print
Look in the / home/apache directory for files that are exactly 100 bytes long:
$find / home/apache-size 100c-print
Look for files longer than 10 blocks in the current directory (one block equals 512 bytes):
$find. -size + 10-print
11. Use the depth option
When using the find command, you may want to match all the files before looking in subdirectories. Using the depth option, you can make the find command do this. One reason for this is that when you use the find command to back up a file system on tape, you want to back up all the files first, and then back up the files in the subdirectory.
In the following example, the find command starts at the root of the file system and looks for a file named CON.FILE.
It will first match all the files and then go into the subdirectory to look for it.
$find /-name "CON.FILE"-depth-print
12. Use mount option
To find files in the current file system (without entering other file systems), you can use the mount option of the find command.
Start from the current directory to find files in this file system whose filenames end with XC:
$find. -name "* .XC"-mount-print
The above is all the contents of this article entitled "linux's find command format and how to use find commands". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.