Linux Shell Basics (vii)

Source: Internet
Author: User
Tags file type file

16. File Lookup Command find:

Here's a sample of the main applications for the Find command:
/> Ls-l#列出当前目录下所包含的测试文件
-rw-r--r--. 1 root root 48217 Nov 00:57 Install.log
-rw-r--r--. 1 root root Panax notoginseng 00:56 testfile.dat
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rw-r--r--. 1 root root 183 Nov 08:02 users
-rw-r--r--. 1 root root 279 Nov 08:45 users2

1. Search by file name
-name:The file name is case sensitive when looking.
-iname:File name capitalization is not sensitive when looking up.
The #该命令为find命令中最为常用的命令, which is to look for files with a. log extension from the current directory. It is necessary to note that, by default, find searches from the specified directory and recursively searches its subdirectories.
/> Find-name "*.log"
./install.log
/> Find-iname u*#如果执行find. -name u* won't find a matching file
Users Users2


2. Search by file time attribute:
-atime-n[+n]:Find files where the file access time is within n days [outside].
-ctime-n[+n]:Find files where the file change time is within n days [outside].
-mtime-n[+n]:Find files that modify the data time within n days [].
-amin-n[+n]:Find files where the file access time is within n minutes [outside].
-cmin-n[+n]:Find files where the file change time is within n minutes [outside].
-mmin-n[+n]:Find files that modify the data time within n minutes [].
/> Find-ctime-2#找出距此时2天之内创建的文件
.
./users2
./install.log
./testfile.dat
./users
./test.tar.bz2
/> Find-ctime +2 #找出距此时2天之前创建的文件
No #因为当前目录下所有文件都是2天之内创建的 found
/> Touch Install.log#手工更新install the last access time for. Log so that the following find command can find the file
/> Find. -cmin-3 #找出修改状态时间在3分钟之内的文件.
Install.log

3. Perform the specified action based on the found file:
-exec: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 \; and there are no spaces between two {}
-ok:The main function and syntax format is exactly the same as the-exec, except that the option is more secure because it will be prompted every time the shell command executes, and the shell command will be executed only when the answer is Y. It is necessary to note that this option does not apply to automation scripts because the offer may suspend the entire automation process.
#找出距此时2天之内创建的文件, and based on the results of find, the command after-exec is applied, that is, ls-l, so that a clear list of find files can be displayed directly.
/> Find-ctime-2-exec ls-l {} \;
-rw-r--r--. 1 root root 279 Nov 08:45./users2
-rw-r--r--. 1 root root 48217 Nov 00:57./install.log
-rw-r--r--. 1 root root 00:56./testfile.dat
-rw-r--r--. 1 root root 183 Nov 08:02./users
-rw-r--r--. 1 root root 10530 Nov 23:08./test.tar.bz2
#找到文件名为 *.log, while the file data modification time is within 1 days of the file. If found, delete them. Sometimes, such a writing is deleted immediately after it is found, so there is a risk of accidental deletion.
/> ls
Install.log testfile.dat test.tar.bz2 Users Users2
/> Find-name "*.log"-mtime-1-exec rm-f {} \;
/> ls
Testfile.dat test.tar.bz2 Users Users2
Under the console, in order to make the above command more secure, we can replace-exec with-ok, as shown in the following example:
/> Find-name "*.dat"-mtime-1-ok rm-f {} \;
< RM .... /testfile.dat >?Y #对于该提示, if you answer Y, the found *.dat file will be deleted, which can be seen from the results of the LS command below.
/> ls
TEST.TAR.BZ2 Users Users2

4. Search by the owner and group to which the file belongs:
-user:Finds files for the specified user after the owner belongs to the-user option.
!-user:Finds files that owner does not belong to the specified user after the-user option.
-group:
Find the file that group belongs to after the-group option is specified.
!-group:Finds files that group does not belong to the specified groups after the-group option.
/> Ls-l#下面三个文件的owner均为root
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rw-r--r--. 1 root root 183 Nov 08:02 users
-rw-r--r--. 1 root root 279 Nov 08:45 users2
/> chown Stephen users#将users文件的owner从root改为stephen.
/> Ls-l
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rw-r--r--. 1StephenRoot 183 Nov 08:02 users
-rw-r--r--. 1 root root 279 Nov 08:45 users2
/> Find-user root#搜索owner是root的文件
.
./users2
./test.tar.bz2
/> Find!-user root#搜索owner不是root的文件, Attention! There should be a space between the-user and the.
./users
/> Ls-l#下面三个文件的所属组均为root
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rw-r--r--. 1 Stephen Root 183 Nov 08:02 users
-rw-r--r--. 1 root root 279 Nov 08:45 users2
/> chgrp Stephen users#将users文件的所属组从root改为stephen
/> Ls-l
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rw-r--r--. 1 StephenStephen183 Nov 08:02 Users
-rw-r--r--. 1 root root 279 Nov 08:45 users2
/> Find-group root#搜索所属组是root的文件
.
./users2
./test.tar.bz2
/> Find!-group root#搜索所属组不是root的文件, Attention! There should be a space between the-user and the.
./users

5. Search by the specified directory depth:
-maxdepth:The following parameters represent the depth specified from the current directory, where 1 represents the current directory, 2 represents a primary subdirectory, and so on. When this option is specified, find is not recursively its subdirectories until the specified depth is found. The depth in the following example is 1, which means that the search is only in the current subdirectory. If this option is not set, find will recursively return all subdirectories under the current directory.
/> mkdir subdir#创建一个子目录 and create a file within that subdirectory
/> CD subdir
/> Touch Testfile
/> CD.
#maxdepth后面的参数表示距当前目录指定的深度, where 1 represents the current directory, 2 represents a primary subdirectory, and so on. When this option is specified, find is not recursively its subdirectories until the specified depth is found. The depth in the following example is 1, which means that the search is only in the current subdirectory. If this option is not set, find will recursively return all subdirectories under the current directory.
/> Find. -maxdepth 1-name "*"
.
./users2
./subdir
./users
./test.tar.bz2
#搜索深度为子一级子目录, you can see that the testfile you just created under the subdirectory has been found
/> Find-maxdepth 2-name "*"
.
./users2
./subdir
./subdir/testfile
./users
./test.tar.bz2

6. Exclude the specified subdirectory lookup:
-path Pathname-prune:Avoid specifying subdirectories for pathname lookups.
-path Expression-prune:Avoids a set of pathname lookups specified in the expression.
It is necessary to note that if you use the-depth option at the same time, then-prune will be ignored by the Find command.
#为后面的示例创建需要避开的和不需要避开的子目录 and create files in these subdirectories that match the find rules.
/> mkdir Dontsearchpath

/> CD Dontsearchpath
/> Touch Datafile1
/> CD.
/> mkdir Dosearchpath
/> CD Dosearchpath
/> Touch Datafile2
/> CD.
/> Touch Datafile3
#当前目录下, avoid Dontsearchpath subdirectories and search for all files with file name datafile*.
/> Find-path "./dontsearchpath"-prune-o-name " datafile*"-print
./dosearchpath/datafile2
./datafile3
#当前目录下, while avoiding dontsearchpath and Dosearchpath two subdirectories, search for all files with file name datafile*.
/> Find \ (-path "./dontsearchpath"-o-path "./dosearchpath" \)-prune-o-name "datafile*"-print
./datafile3

7. Search by file permission property:
-perm mode:File permissions exactly match mode (mode is the octal representation of file permissions).
-perm +mode:The file Permissions section conforms to mode. If the command parameter is 644 (-rw-r--r--), then as long as there are any permissions in the file permission attribute and 644 overlap, such files can be selected.
-perm-mode:File permissions are fully compliant with mode. If the command parameter is 644 (-rw-r--r--), when the permission specified in 644 has been fully owned by the current file, and the file has additional permission attributes, such a file can be selected.
/> Ls-l
-rw-r--r--. 1 root root 0 Nov 10:02 datafile3
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rw-r--r--. 1 Stephen Stephen 183 Nov 08:02 users
-rw-r--r--. 1 root root 279 Nov 08:45 users2
/> Find-perm 644#查找所有文件权限正好为644 (-rw-r--r--) file.
./users2
./datafile3
./users
./test.tar.bz2
/> Find-perm 444#当前目录下没有文件的权限属于等于444 (All 644).
/> Find-perm-444#644所包含的权限完全覆盖444所表示的权限.
.
./users2
./datafile3
./users
./test.tar.bz2
/>find.-perm +111#查找所有可执行的文件, the command did not find any files.
/> chmod u+x users#改变users文件的权限, add the executable permission for owner so that the following command can be found.
/> Find-perm +111
.
./users

8. Search by file type:
-type:The type of the file that is specified later.
b-Block device files.
DDirectory
C-Character device files.
P-Piping files.
L-Symbolic link file.
F-Normal file.
/> mkdir subdir
/> Find-type D#在当前目录下, find files of the file type directory.
./subdir
  /> Find.!-type D#在当前目录下, find files of a file type that are not directories.
./users2
./datafile3
./users
./test.tar.bz2
/> Find-type f#在当前目录下, find files of file type file
./users2
./datafile3
./users
./test.tar.bz2

9. Search by file Size:
-size [+/-]100[c/k/m/g]:Represents a file with a length equal to [greater than/less than] 100 blocks [byte/k/m/g].
-empty:Find an empty file.
/> Find-size +4k-exec ls-l {} \;#查找文件大小大于4k的文件, print out the details of the found file at the same time
-rw-r--r--. 1 root root 10530 Nov 23:08./test.tar.bz2
/> Find-size-4k-exec ls-l {} \;#查找文件大小小于4k的文件.
-rw-r--r--. 1 root root 279 Nov 08:45./users2
-rw-r--r--. 1 root root 0 Nov 10:02./datafile3
-rwxr--r--. 1 Stephen Stephen 183 Nov 08:02/users
/> Find-size 183c-exec ls-l {} \;#查找文件大小等于183字节的文件.
-rwxr--r--. 1 Stephen Stephen 183 Nov 08:02/users
/> Find-empty-type f-exec ls-l {} \;
-rw-r--r--. 1 root root 0 Nov 10:02./datafile3

10. By changing the time than the specified file new or older than the way the file is looked up:
-newer file1! File2:Find files that change dates newer than file1, but older than file2 files.
/> LS-LRT#以时间顺序 (from morning to night) lists a list of all the files in the current directory for reference in the following example.
-rwxr--r--. 1 Stephen Stephen 183 Nov 08:02 Users1
-rw-r--r--. 1 root root 279 Nov 08:45 users2
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rw-r--r--. 1 root root 0 Nov 10:02 datafile3
/> Find-newer users1#查找文件更改日期比users1新的文件, as can be seen from the above results, the remaining documents meet the requirements.
./users2
./datafile3
./test.tar.bz2
/> Find!-newer users2#查找文件更改日期不比users1新的文件.
./users2
./users
#查找文件更改日期比users2新, but no more than test.tar.bz2 new files.
/> Find-newer users2!-newer test.tar.bz2
./test.tar.bz2

The attentive reader may find that the description of find, which I have already given in my previous blog, is the reason why the following three points are explained in a single section:
1. The find command plays a very important role in the Linux shell;
2. In order to ensure the completeness of this series;
3. The previous blog is a summary note that I left many years ago, somewhat rough, giving a more detailed example.

17. Xargs command :

The main function of this command is to build and execute shell commands from the input.
When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.
The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.
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.
/> Ls-l
-rw-r--r--. 1 root root 0 Nov 10:02 datafile3
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rwxr--r--. 1 root root 183 Nov 08:02 users
-rw-r--r--. 1 root root 279 Nov 08:45 users2
#查找当前目录下的每一个普通文件, and then use the Xargs command to test which type of file they belong to.
/> Find-type f-print | xargs file
./users2:ascii Text
./datafile3:empty
./users:ascii Text
./TEST.TAR.BZ2:BZIP2 compressed data, block size = 900k
#回收当前目录下所有普通文件的执行权限.
/> Find-type f-print | xargs chmod a-x
/> Ls-l
-rw-r--r--. 1 root root 0 Nov 10:02 datafile3
-rw-r--r--. 1 root root 10530 Nov 23:08 test.tar.bz2
-rw-r--r--. 1 root root 183 Nov 08:02 users
-rw-r--r--. 1 root root 279 Nov 08:45 users2
#在当面目录下查找所有普通文件 and use the grep command to find the word hostname in the searched file.
/> Find-type f-print | xargs grep "hostname"
#在整个系统中查找内存信息转储文件 (core dump), and then save the results to the/tmp/core.log file.
/> Find/-name "core"-print | xargs echo "" >/tmp/core.log

/> pgrep mysql | xargs kill-9 #直接杀掉mysql的进程
[1]+ killed MySQL

Linux Shell Basics (vii)

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.