Linux command (6/10): Find command

Source: Internet
Author: User
Tags readable stdin uppercase letter cpu usage file permissions

Linux divides clocks into system clocks (System clock) and hardware (Real time clock, referred to as RTC) clock two kinds . System time refers to the clock in the current Linux kernel,

The hardware clock is the battery-powered motherboard hardware clock on the motherboard, which can be set in the BIOS "standardBIOS feture" entry.

When Linux starts, the hardware clock reads the settings of the system clock, and the system clock is independent of the hardware.

Consider adding the following command to synchronize the hardware time with the software clock in the time synchronization script:

Hwclock--SYSTOHC (synchronized with system time)

Vmstat, frequently asked questions processing

If R is often greater than 4 and the ID is often less than 40, it indicates that the CPU is heavily loaded.

If the Pi,po is not equal to 0 for a long time, it indicates insufficient memory.

If disk is often not equal to 0, and the queue in B is greater than 3, the IO performance is poor.

1.) If the sequence running in processes (process R) is more contiguous than the number of CPUs in the system indicates that the system is now running slower, there are most processes waiting for the CPU.

2.) If the output number of R is more than 4 times times the number of available CPUs in the system, the system is facing a CPU shortage, or the CPU rate is too low, the system has a majority of processes waiting for the CPU, causing the system to run too slow process.

3.) If the idle time (CPU ID) lasts 0 and the system time (CPU Sy) is twice times the user time (CPU us) The system is facing a shortage of CPU resources.

Workaround:

When the above problems occur, please adjust the CPU usage of the application. This allows the application to use the CPU more efficiently. You can also consider adding more CPUs. The use of CPU can be combined with mpstat, PS aux top prstat– A and so on some of the corresponding commands to comprehensively consider the use of specific CPUs, and those processes that are taking up a lot of CPU time. In general, the problem with the application is larger. For example, some SQL statements are unreasonable and so on will cause this phenomenon.

Memory problem Phenomenon:

The bottleneck of memory is determined by scan rate (SR). Scan rate is scanned by the constant algorithm per second. If the scan rate (SR) A continuous greater than 200 pages per second indicates a possible memory defect. Similarly, if the PI and PO columns in the page item represent the number of pages per second that are paged in and the number of pages that are paged per second. If the value is often a non-0 value, there may be a memory bottleneck, of course, if the individual is not 0, is the normal page scheduling this is the main principle of virtual memory.

Workaround:
1. Adjust applications & servers to make memory and cache usage more efficient.

2. Increase the system's memory.

3. Implement priority paging in S-in pre-Solaris 8 versions by adding line ' Set priority Paging=1 ' In/etc/system. Remove the upgrading from the Solaris 7 to 8 & retaining Old/etc/system file.

About memory usage can also knot PS aux top prstat–a and so on some of the corresponding commands to comprehensively consider the use of specific memory, and those processes are consuming a lot of memory. In general, if the memory occupancy rate is high, but the CPU occupies a very low time, It can be considered that there are a lot of applications that occupy the memory that is not released, but that does not take up CPU time, you can consider the application, for not consuming CPU time and some background programs, freeing up memory consumption.

The Linux Find command searches the directory structure for files and performs the specified actions. Linux Find command provides quite a lot of search conditions, powerful

3. Command parameters:

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

The-print:find command outputs the matched file to standard output.

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

-ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.

4. Command options:

-name finds files by file name.

-perm to find files according to file permissions.

-prune Use this option to have the Find command not be found in the currently specified directory, and if you use the-depth option at the same time,-prune will be ignored by the Find command.

-user Search for files according to the owner of the file.

-group finds files according to the group to which the files belong.

-mtime-n +n The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but they both and the-m time option.

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

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

-newer file1! File2 look for a file that changes time than the file File1 new but older than the file file2.

-type find a file of a certain type, such as:

B-block device files.

D-Directory.

C-character device file.

P-Pipeline file.

L-Symbolic link file.

F-Normal file.

-size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes. -depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories.

-fstype: Find files located in a file system of a certain type, these file system types 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 locating 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 for matching files to back up these files to the tape device.

In addition, the following three differences:

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

-atime N Find the last n*24 hour Access file in the system

-cmin n Find files in the last n minutes of the system changed file status

-ctime n Find files that have changed file status in the last n*24 hours of the system

-mmin n Find files that have changed file data in the last N minutes of the system

-mtime n Find files that have changed file data for the last n*24 hours in the system

5. Usage examples:

Find Files
Find./-type F

Find a Directory
Find./-type D

Find a file or directory with the name test
Find./-name Test

Look for a file whose name matches the regular expression, and note the preceding '. * ' (found file with directory)
Find./-regex. *so.*\.gz

Find the directory and list the files under the directory (the LS command is executed separately for each directory found, no option-print the directory name is not displayed on the previous line of the file list)
Find./-type d-print-exec ls {} \;

Locate the directory and list the files in the directory (the LS command is executed separately for each directory found, and you need to confirm before executing the command)
Find./-type d-ok ls {} \;

Find the directory and list the files under the directory (add the found directory to the LS command one time after execution, the parameters will be executed multiple times when the parameter is too long)
Find./-type d-exec ls {} +

Find files with file names matching *.c
Find./-name \*.c

Prints the contents of the test file after printing the test filename
Find./-name test-print-exec cat {} \;

Do not print the test file name, only the contents of test files
Find./-name test-exec cat {} \;

Find files that are less than two days away from the current time of file update date
Find./-mtime-2

Files that are more than two days away from the current time when you find file update days
Find./-mtime +2

Find files that are less than two days away from the current time of day when files are updated
Find./-mtime 2

Find files that are less than two minutes from the current time when the file is updated
Find./-mmin-2

Find files that are more than two minutes from the current time when the file is updated
Find./-mmin +2

Find files that are less than two minutes away from the current time when a file is updated
Find./-mmin 2

Find file update time than file ABC content update time new file
Find./-newer ABC

Find file access times than file ABC content update time new files
Find./-anewer ABC

Find empty files or empty directories
Find./-empty

Find empty files and delete
Find./-empty-type F-print-delete

Find a file or directory with permission 644 (requires full compliance)
Find./-perm 664

Find files or directories where user/group permissions are read-write, other user rights are readable (other permissions are not limited)
Find./-perm-664

Find files or directories where the user has write permission or the group user has write permission
Find./-perm/220
Find./-perm/u+w,g+w
Find./-perm/u=w,g=w

Find directories or files with Read permission for owner permissions
Find./-perm-u=r

Find directories or files with Read permission for user group permissions
Find./-perm-g=r

Find directories or files with Read permission for other user rights
Find./-perm-o=r

Find files or directories that are owned by LZJ
Find./-user Lzj

Find a file or directory with a group named Gname
Find./-group Gname

Files that find the user ID of a file that does not exist
Find./-nouser

Find file with group ID that does not exist
Find./-nogroup

To find files with Execute permissions but no Read permission
Find./-executable \! -readable

Find file size smaller than 10 bytes of file or directory
Find./-size-10c

Find a file or directory with size equal to 10 bytes
Find./-size 10c

Find files or directories with a size greater than 10 bytes
Find./-size +10c

Find files or directories with size less than 10k
Find./-size-10k

Find files or directories with size less than 10M
Find./-size-10m

Find files or directories with size less than 10G
Find./-size-10g

EXEC explained:

The-exec parameter is followed by command, which terminates with a; for the end of the flag, so the semicolon behind this command is indispensable, considering that the semicolons in each system have different meanings, so precede the backslash.

{} curly braces represent the file name found in the previous find.

When using find, just write the desired action in a file, you can use the exec to match find find, very convenient. In some operating systems, only the-EXEC option is allowed to execute commands such as L s or ls-l. Most users use this option to find old files and delete them. It is recommended that you take a look at the LS command before you actually execute the RM command to delete files, confirming that they are the files you want to delete. The EXEC option is followed by the command or script that you want to execute, followed by a pair of {}, a space and a \, and finally a semicolon. In order to use the EXEC option, you must use the Print option at the same time. If you verify the Find command, you will see that the command outputs only the relative path and file name from the current path.

The instance 1:ls-l command is placed in the-exec option of the Find command

Command: Find. -type f-exec ls-l {} \;

Output: [[email protected] test]# find. -type f-exec ls-l {} \;

-rw-r--r--1 root root 127 10-28 16:51./log2014.log

Using the grep command in instance 4:-exec

Command: Find/etc-name "passwd*"-exec grep "root" {} \;

Output: [[email protected] test]# find/etc-name "passwd*"-exec grep "root" {} \;

Root:x:0:0:root:/root:/bin/bash

Root:x:0:0:root:/root:/bin/bash

[Email protected] test]#

Description: Any form of command can be used in the-EXEC option. In the example above we use the grep command. The find command first matches all files named "passwd*", such as passwd, Passwd.old, Passwd.bak, and then executes the grep command to see if there is a root user in these files.

Example 5: Find files to move to the specified directory

Command: Find. -name "*.log"-exec mv {}.. \;

Example 6: Executing the CP command with the EXEC option

Command: Find. -name "*.log"-exec cp {} test3 \;

Output:

[[email protected] test]# find. -name "*.log"-exec cp {} test3 \;

CP: "./test3/log2014.log" and "Test3/log2014.log" for the same file

In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there will be too many processes and degraded system performance, so the efficiency is not high; With the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.

The key to using this command is that many commands do not support | Pipelines to pass parameters, and in daily work there is a need for the Xargs command, for example:

This command is wrong.
Find/sbin-perm +700 |ls-l

This is the right thing.
Find/sbin-perm +700 |xargs ls-l
Xargs can read into the stdin data, and with blank characters or word-breaking characters as a resolution, the stdin data is separated into arguments. Because it is separated by a blank character, Xargs may be mistaken if there are some names or other meanings of nouns containing empty characters, and if a special character needs to be handled, it needs to be processed using the-0 parameter.

Example 1: Find every normal file in your system, and then use the Xargs command to test what type of file they belong to

Command: Find. -type F-print | Xargs file

Example 2: Find the Memory information dump file (core dump) throughout the system and save the results to the/tmp/core.log file

Command: Find/-name "core"-print | Xargs echo "" >/tmp/core.log

Example 3: Find files with read, write, and execute permissions for all users in the current directory and reclaim the appropriate write permissions

Command: Find. -perm-7-print | Xargs chmod o-w

Example 4: Use the grep command to search all common files for the word hostname

Command: Find. -type F-print | Xargs grep "hostname"

Example 5: Use the grep command to search all normal files in the current directory for the word hostnames

Command:

Find. -name \*-type F-print | Xargs grep "Hostnames"

Note: Notice that in the example above, \ is used to cancel the special meaning of the * in the shell in the Find command.

Example 6: Performing MV with Xargs

Command: Find. -name "*.log" | Xargs-i MV {} test4

Xargs prompt xargs:argument line too long solution after instance 7:find:

Command:

Find. -type F-atime +0-print0 | xargs-0-l1-t rm-f

Output: [[email protected] test4]# find. -type F-atime +0-print0 | xargs-0-l1-t rm-f

Description

-L1 is a processing of one;-T is the process of printing out a command before

Example 8: Use the-i parameter default front output with {} instead, the-i parameter can specify other substitution characters, as in the example []

Command:

[[email protected] test4]# find. -name "File" | Xargs-i [] CP []..

Note: Using the default front output of the-I parameter instead of {}, the-i parameter can specify other substitution characters, as in the example []

Use of the-p parameter for instance 9:xargs

Command: Find. -name "*.log" | Xargs-p-i mv {}.

  

1. Use the name option:

The file name option is the most common option for the Find command, either used alone or in conjunction with other options.  You can use a file name pattern to match files, remembering to enclose the filename pattern in quotation marks. No matter what the current path is, if you want to find the file name in your root $home that matches *.log, use ~ as the ' pathname ' parameter, and the tilde ~ represents your $home directory.

Find ~-name "*.log"-print

To find all the ' *.log ' files in the current directory and subdirectories, you can use:

Find. -name "*.log"-print

You want the current directory and subdirectories to find file names that begin with an uppercase letter, which can be used:

Find. -name "[a-z]*"-print

To find files with the file name beginning with host in the/etc directory, you can use:

Find/etc-name "host*"-print

To find files in the $home directory, you can use:

Find ~-name "*"-print or find. -print

To get the system running at a high load, start looking for all the files from the root directory.

Find/-name "*"-print

If you want to find the file name in the current directory starting with a lowercase letter, the last file that is 4 to 9 plus. Log Ends:

Command:

Find. -name "[A-z]*[4-9].log"-print

2. With the PERM option:

Follow the file permission mode with the-perm option to find files by file permission mode. It is best to use the octal permission notation.

For example, in the current directory to find file permission bit 755 file, that is, the file owner can read, write, execute, other users can read, execute files, can be used:

[[email protected] test]# find. -perm 755-print

There is also a way to express: in front of the octal number to add a horizontal bar-, the expression is matched, such as 007 is equivalent to 777,-005 equivalent to 555,

Command:

Find. -perm-005

3. Ignore a directory:

If you want to ignore a directory when you're looking for a file, because you know that directory doesn't have the file you're looking for, you can use the-prune option to indicate which directories you want to ignore. Be careful when using the-prune option, because if you use the-depth option at the same time, the-prune option is ignored by the Find command. If you want to find the file under the test directory, but do not want to find it in the Test/test3 directory, you can use:

Command: Find Test-path "Test/test3"-prune-o-print

4. How to avoid a file directory when finding files using find:

Example 1: Find all files in the test directory that are not within the TEST4 subdirectory

Command:

Find Test-path "Test/test4"-prune-o-print

Example 2: Avoid multiple folders:

Command:

Find test \ (-path test/test4-o-path test/test3 \)-prune-o-print

Description

Parentheses represent the combination of expressions. \ denotes a reference, which instructs the shell not to give a special explanation of the characters that follow, leaving the Find command to explain its meaning.

Example 3: Find a certain file,-name and other options after-O

Command:

Find test \ (-path test/test4-o-path test/test3 \)-prune-o-name "*.log"-print

5. Use the user and Nouser options:

Find files by file owner:

Example 1: Find files in the $home directory where the file belongs to the master Peida

Command: Find ~-user peida-print

Example 2: Look for files in the/etc directory where the file belongs to the main peida:

Command: Find/etc-user peida-print

Description: Example 3: In order to find files that are already deleted by the master account, you can use the-nouser option. Find all of these files in the/home directory

Command: Find/home-nouser-print

Description: This will enable you to find files that are not valid accounts in the/etc/passwd file. When using the-nouser option, you do not have to give the user name; the Find command can do the work for you.

6. Use the group and Nogroup options:

Just like the user and Nouser options, the Find command has the same options for the group of users that the file belongs to, in order to find files belonging 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 group of users, 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 by time of change or access time:

You can use the Mtime,atime or CTime option if you want to find the file by changing the time. If the system suddenly does not have free space, it is possible that the length of a file grows rapidly during this period, you can use the Mtime option to find such a file.

Use a minus sign-to limit the time to change the file within the current n days, and use the Plus + to limit the change time before the current n days of the file.

To find files that change within 5th of the system root directory, you can use:

Find/-mtime-5-print

In order to find files that change time before 3rd in the/var/adm directory, you can use:

Find/var/adm-mtime +3-print

8. To find new or older files than a file:

You can use the-newer option if you want to find all files that have changed time than one file but older than the other.

The general form of it is:

Newest_file_name! Oldest_file_name

Among them,! is a logical non-symbol.

Example 1: Find changed times than files Log2012.log new but older files than files Log2017.log

Command:

Find-newer Log2012.log! -newer Log2017.log

9. Use the type option:

Example 1: Find all directories in the/etc directory

Command: Find/etc-type d-print

Example 2: Find all types of files except directories in the current directory

Command: Find. ! -type D-print

Example 3: Find all the symbolic link files in the/etc directory

Command: Find/etc-type l-print

10. Use the size option:

files can be searched by file length, and the length of the file referred to here can be measured either in blocks or in bytes. The length of the measured file in bytes is expressed as n C, and the length of the block measurement file is only represented by a number.

When looking up files by file length, this is generally the size of the file in bytes, and it is easier to convert by using blocks to measure the file system.

Example 1: Find files with file lengths greater than 1 m bytes in the current directory

Command: Find. -size +1000000c-print

Example 2: Look for files with a file length of exactly 100 bytes in the/home/apache directory:

Command: Find/home/apache-size 100c-print

Example 3: Find a file with a length of more than 10 blocks in the current directory (a piece equals 512 bytes)

Command: Find. -size +10-print

11. Using the depth option:

When you use the Find command, you may want to match all the files and find them in the subdirectory. Use the depth option to enable the Find command to do so. One reason for this is that when you use the Find command to back up the file system to tape, you want to back up all the files first, and then back up the files in the subdirectories.

The instance 1:find command starts at the root of the file system and looks for a file named Con.file.

Command: Find/-name "CON." FILE "-depth-print

Description: It will first match all the files and then go to the subdirectory to find

12. Using the Mount option:

You can use the Mount option of the Find command to find a file in the current file system (without entering another file system).

Example 1: Starting from the current directory find files in the file system with the file name ending in XC

Command: Find. -name "*. XC "-mount-print

Linux command (6/10): Find command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.