Linuxskill

xiaoxiao2021-03-06  14

Using Find and Xargs may sometimes need to find files with a feature in the system (such as file permissions, file owners, file length, file type, etc.). There may be many reasons for this. It is possible for security considerations, or general system management, perhaps just to find a document that does not know where it is not known. F i N D is a very effective tool that traverses the current directory or even the entire file system to find some files or directories. In this chapter, we introduce the following: * Find Command Options. * Use f i n d command to different options. * Cooperate with F i n D using an example of the X a RG S command. Since F i n d has such a powerful function, it has a lot of options, most of whom is worth our time to find out. Even if the system contains a network file system (N f s), the F I n d command is equally valid in the file system, as long as you have the appropriate permissions. When running a very resource-consuming Find command, many people tend to perform it in the background, because it can spend a long time to traverse a large file system (here is the file system above 3 0 g bytes) ). F I n d Command general form is: Find pathname -Options [-print -exec -ok] Let's take a look at the parameter of the command: the Pathname Find command finds the directory path. For example, use. To represent the current directory, use / to represent the system root directory. The -print find command outputs the matching file to a standard output. The -EXEC Find command performs the S H E L command given by the matching file. The form of the corresponding command is 'c o m m m m m - and' {} /;, pay attention to the space between {} and /; The effect of -OK and - e x e c, but only the S H E L command given by this parameter in a more secure mode, gives a prompt before performing each command, allowing the user to determine if it is executed. The Find Command Options f i n d has a lot of options or expressions, each with a horizontal bar in front of each option. Let's first take a look at the main options of the command, and then give some examples. -name finds a file according to the file name. -Perm Find files in accordance with file permissions. -Prune Use this option to make F i n d in the currently specified directory, if you use the-D e P T H option, then the P R U n E option is ignored by the f i n d command. -user Find files according to the owner of the file. -group Find files in accordance with the group belonging to the file. -mtime -n n Find files according to the change time of the file, - n indicates that the file change time is now N days, n indicates that the file change time is now N days. F i n d or - a t i m e and - c t i m e option, but they are similar to-M t i m e option, so we only introduce the M T I m E option here. -NOGROUP looks out that the file where the college group is not, that is, the group to which the file belongs is not present in / e t C / G R O u P s. -nouser finds a file without a valid home, that is, the owner of the file does not exist in / e t C / P A S W. -newer file1! file2 lookup change time is new than file F i l E 1 new file File f i l e 2 old file. -type finds a type of file, such as: B - block device file. D - directory. C - Character device file. P - pipe file. l - symbolic link file. f - normal file. -size n [c] Find files with file length N block, with C indicates that the file length is byte.

-Depth When looking up a file, look for files in the current directory before looking for files in its subdirectory. -fstype Find files in a certain type of file system that can usually be found in configuration file / e t c / f s t a b, which contains information about file systems in this system. -mount does not cross the file system M O U n T point when looking for files. -follow If the F I n D command encounters a symbolic link file, track the file pointed to the link. -CPIO uses the C P i O command to match the match to the tape device. Using the Name Option File Name option is the most commonly used option of f i n d command, or use this option separately or with other options. You can use some filename mode to match the file, remember to cause the file name mode by quotation marks. Regardless of the current path, if you want to find the file name in your own root directory $ H O M e, you can use the file that meets *. T X T, use it as' p A t h n a m E parameters, the wavy number ~ represents your $ h o m e directory. $ find ~ -name "* .txt" -print wants to find all the '*. TX T' file in the current directory and subdirectory, you can use: $ find. -name "* .txt" -print you want In the current directory and subdirectories, the file name is found at the beginning of a capital letter, you can use: $ find. -Name "[az] *" -print wants to find file names in Host in the / etc directory, you can Use: $ find / etc -name "Host *" -print wants to find files in the home directory, you can use: $ find ~ -name "*" -print or find. -Print To allow system high load operation, Start find all files from the root directory. If you want to keep a good impression in the system administrator, it is best to think about it before you do it! $ FIND / -NAME "*" -print If you want to find the file name at the current directory to start with two lowercase letters, follow the two numbers, finally *. TXT file, the following command can return to AX 3 7 . TXT file: $ find. -name "[AZ] [AZ] [0--9] [0--9] .txt" -print Using the Perm Option If you want to find the file according to the file permission mode, you can use - PERM option. You may need to find all users with execution permission, or want to view file permissions in a user directory. When using this option, it is best to use an octal permission representation. In order to find files in the current directory, you can read, write, execute, other users can read, write, execute, other users can read, write, execute, if you want The current directory looks for all users readable, written, executes files (be careful of this), we can use the Find command - Perm option. Add a horizontal bar in front of the octal number. In the following command - P e R m represents the file permission looks, and '0 0 7' and the representation you employ in the absolute mode of the C H m o D command is identical.

$ find. -perm -007 -print ignores a directory if you want to ignore a directory when you find a file, because you know the file you want to find in that directory, you can use the P R U N E option to indicate the directory you need to ignore. Be careful when using - P r u n option, because if you use the - D e P T H option, then - P R u n e option is ignored by the f i n d command. If you want to find files in / apps directory, you don't want to find in / apps / bin directory, you can use: $ find / apps -name "/ apps / bin" -prune -o -print 2.1.4 User and Nouser Options If you want to find a file according to the file owner, you can give the corresponding username. For example, find the file owner's file for Dave, you can use: $ find ~ -user Dave -print Look for file owners to UUCP files: $ find / etc --user uucp -print To find a file that is already deleted, you can use the - Nouser option. This will find files that have no valid accounts in the / e T C / P A S w D file. When using the - N O U S ER option, you don't have to give the username; f i n d command can do the corresponding work for you. For example, I hope to find all such files in the / home directory, you can use: $ find / home -nouser -print using the group and nogroup options just like the user group, the user group belonging to the file, the Find command is also With the same option, in order to find files belonging to an Accts user group in / apps directory, you can use: $ find / apps -group acccts -print To find all files that do not have a valid assigned user group, you can use NOGROUP options. The following f i n D command finds this file from the root of the file system $ FINE / -NOGROUP-PRINT Follow the Change Time Find File If you want to find a file according to the change time, you can use the M t i m e option. If the system suddenly has no space, it is very likely that the length of a file is growing rapidly during this period. At this time, you can use the M t i m e option to find such a file. Use the minus-token-to limit the files within the distance from Today's N days, and use the plus number to qualify the file before the time N days. I hope to find changes within 5 days in the system root directory, you can use: $ find / -mtime -5 -print In order to find the file before the / var / adm directory, you can use: $ Find / var / adm -mtime 3 -print looks for all files than a file new or old file, if you want to find changes to all files than a file, you can use - NEWER options. Its general form is: newest_file_name! Oldest_file_name,! Is logical non-symbol. There are two files here, and their change time is about two days.

The find command given can find changes time than file age. Awk new but file BELTS. AWK Old file: If you want to use this option to use to find files within two hours, unless one The ready-made file has changed the time before two hours, otherwise it is not available to compare the files of the change time. To solve this problem, you can first create a file and set the date and timestamp to the time required. This can be implemented with the T O U c h command. Suppose the current time is 2 3: 4 0, I hope to find the file within two hours, you can first create such a file: a file that meets the requirements has been created; here we assume that today is May 4, and The change time of this file is 2 1: 4 0, which is just two hours earlier. Now we can use the Find command - the newer option to find all changes within two hours of the current directory: $ find. -Newer dstamp -print uses the Type option UNIX or Linux system There are several different file types This part of the content we have made in the previous chapter, here is not described again. If you want to find all directories in the / etc directory, you can use: $ find / etc --type d -print In order to find all types of files other than the directory in the current directory, you can use: $ find.! -Type d - Print In order to find all symbolic link files in the / etc directory, you can use: $ find / etc -type l -print 2.1.9 Use the size option to find the file according to the length of the file, this length refers to the length of the file can be used (Block) to count, or bytes can also be measured. The expression form of the byte metering file is n c; only the number is represented by the number of block meter files. In personally, I always use the way of bytes. When you find a file according to the file length, most people prefer the length of the file indicated by bytes, without using the number of blocks, unless It is the size of the file system because it is easier to convert when using blocks. In order to find files with a file length greater than 1 m bytes in the current directory, you can use: $ find. -Size 1000000c -print In order to find file lengths in the / home / apache directory, you can use a file with a file length of 1 0 0 bytes. Use: $ find / home / apache -size 100c -print In order to find a file with more than 1 0 block in the current directory (a piece equal to 5 1 2 bytes), you can use: $ find. -Size 10 -print Using Depth Options When you use the find command, you may want to match all files first, then look for in the subdirectory. Use the D e p T h option to make F I n d commands. One reason for this is that when you back up the file system on the tape using the F I n D command, you want to back up all files, followed by backup files in the subdirectory. In the following example, the F i n d starts from the root directory of the file system to find a file called C O n. F i l E. It will first match all files and then enter the subdirectory.

$ find / -name "con.file" -Depth -print Using the mount option to find the file in the current file system (do not enter other file systems), you can use the M O U N t option for the f i n d command. In the following example, we start looking up from the current directory in the file name in this file system with XC's file: $ find. -Name "* .xc" -mount -print Using the CPIO Option CPIO command to tape devices Backup files or recover files from it. You can use the f i n d command to look for the file throughout the file system (more in some cases), and then back up to the tape with the C P i o command. If you want to use the CPIO command backup / etc, / home, and / apps directory, you can use the commands given below, but you have to remember that you are in the root of the file system: (in the example above, The end of the end / tell the shell command has not ended, ignored / launched back.) In the above example, it should be noted that the path is missing /. This is called a relative path. The reason why the relative path is used because the path to restore the file can be selected when recovering these files from the tape. For example, these files can be restored to another directory, and they are restored to the original directory after some operations. If an absolute path is used when the backup, such as / e t c, then when recovery, you can only return to the / e t c directory, there is no other option. In the above example, I tell the find command first enter the / etc directory, then the / home and / apps directory, first match the files in these directory, then match the files in its subdirectory, all of these results will pass through pipe Back up the CPIO command. By the way, in the example above, the cpio command uses C 6 5 5 3 6 option, I can use B option, but this is only 5 1 2 bytes per piece, and C 6 5 5 3 6 After the option, the size of the block becomes 6 4 k bytes (6 5 5 3 6/1 0 2 4). Use Exec or OK to execute the shell command After matching some files, you may want to perform some operations, you can use the - e x e c option. Once the f i n d is equipped with the appropriate file, you can use the command in the-e x e c option to operate (only in some operating systems), such as commands such as L s or LS -L options). Most users use this option to find old files and delete them. Here I strongly recommend that before you really execute the R m command to delete the file, you should first use the L S command to confirm that they are the files to be deleted. E x e c Options followed by the command to be executed, then a pair of {}, a space and one /, and finally a semicolon. In order to use the E x E C option, you must use the P R I n t option. If you verify that the F I n d command will find that the command only outputs the relative path and file name from the current path. In order to list the matched files with the ls -l command, you can put the ls -l command in the exec option, for example: in the above example, the find command matches all ordinary files in the current directory, and Use the ls -l command to list them in the - EXEC option.

In order to find changes in the / logs directory, you can use: $ find logs -type f -mtime 5 -exec rm {} /; remember, use any way in any way in any way Before, you should first check the corresponding file, be careful! When using, for example, M v or R m commands, the security mode of the-e x e c option can be used. It will prompt you before performing each matching file. In the example below, the f i n d command looks for all file names in the current directory to end the file, change the time over 5 or more, and delete them, just give a prompt before deletion. Press the Y key to delete the file, press the n to not delete it. Any form of commands can be used in the - e x e c option. In the following example we use G R e p command. f i n D command first matches all files named "P A S W *" file, such as P A S W, P A S, P A S, P a S W. B Ak, then execute the G r e p command to see if there is a R O U N D e r user in these files. Examples of the Find command we have already introduced the basic options of the F i n d command, and some other examples of the F I n D command are given below. In order to match all files in the $ home directory, the following two methods can be used: $ find $ home -print $ find ~ -print In order to find Suid settings in the current directory, the file is owned by reading, writing, executing permission, And the files belonging to the files and other users with the permissions of read and execute, you can use: $ find. -Type f -perm 4755 -print In order to find a normal file with all the files in the system, you can list them The full path, you can use: $ find / -type f -size 0 -EXEC ls -l {} /; to find the normal file before the date of the / var / logs directory, and delete them, you can use: $ Find / var / logs -type f -mtime 7 -EXEC RM {} /; in order to find all the Audit groups belonging to the system, you can use: $ find / -name -group audit -print Our audit system creates every day An audit log file. The last containing numbers of the log file name, so that we can see which file is the latest, which is the oldest. A D m i n. L O g files: A D m i n. L o g. 0 0 1, A D m i n. L o g. 0 0 2, etc.. The following f i n d will delete the / l O g s directory before the 7th, contains a D m i n. L O g file of the digital suffix. This command only checks three digits, so the suffix of the corresponding log file should not exceed 9 99.

转载请注明原文地址:https://www.9cbs.com/read-48363.html

New Post(0)