A detailed look at the search command under Linux (find)

Source: Internet
Author: User
Tags auth svn uppercase letter sqoop

4.find

The Linux Find command searches the directory structure for files and performs the specified actions. The Linux Find command provides quite a lot of search criteria and is powerful. Because find has powerful features, it has a lot of options, most of which are worth taking the time to look at. Even if the system contains a network file system (NFS), the Find command works equally well in the file system, and you only have the appropriate permissions. When running a very resource-intensive find command, many people tend to put it in the background, because traversing a large file system can take a long time

4.1. Command format:

Find Pathname-options [-print-exec-ok ...]

4.2. Command function:

Used to locate files in the file tree and to make corresponding processing

4.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.

[[email protected] ~]$ cat 123.txt qwda[[email protected] ~]$ find 123*-ok cat {} \;< cat ... 123.txt >? Yesqwda[[email protected] ~]$ find 123*-exec cat {} \;qwda[[email protected] ~]$ find 123*| Xargs Catqwda

4.4. Command options:

-name filename #查找名为filename的文件-perm #按执行权限来查找-user username #按文件属主来查找               -group groupname #按组来查找-mtime-n +n #按文件更改时间来查找文件,-n means less than n days, +n refers to n days ago-atime-n +n #按文件访问时间来查GIN: 0px ">-ctime-n +n #按文件创建时间来查找文件,-n refers to n days, +n refers to n days ago-nogroup #查无有效属组的文件, That is, the file belongs to the group in the/etc/groups does not exist in the-nouser #查无有效属主的文件, that is, the owner of the file in/etc/passwd does not save-newer F1!f2 to find files,-n refers to less than n days , +n refers to n days ago-ctime-n +n #按文件创建时间来查找文件,-n refers to n days, +n refers to n days ago-nogroup #查无有效属组的文件, that is, the group of files in/etc/g There is no-nouser #查无有效属主的文件 in Roups, that is, the owner of the file does not exist in/etc/passwd-newer F1!f2 #查更改时间比f1新但比f2旧的文件-typ                       e b/d/c/p/l/f file for #查是块设备, directory, character device, pipe, symbolic link, normal file-size n[c] #查长度为n块 [or n bytes]-depth #使查找在进入子目录前先行查找完本目录-fstype #查更改时间比f1新但比f2旧的文件-type b/d/c/p/l/f #查是块设备, directories, character devices, pipelines, symbolic links,     Ordinary file-size N[c]          #查长度为n块 [or N bytes] of the file-depth #使查找在进入子目录前先行查找完本目录-fstype #查位于某一类型文件系统中的文件, these File system types can usually be found in/etc/fstab-mount #查文件时不跨越文件系统mount点-follow #如果遇到符号链接文件 to track the file that the link refers to      -cpio%; #查位于某一类型文件系统中的文件, these file system types can usually find-mount #查文件时不跨越文件系统mount点-follow in/etc/fstab #如果 If you encounter a symbolic link file,-cpio #对匹配的文件使用cpio命令 the file that the link refers to, and back them up to the tape device-prune #忽略某个目录

-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

4.5. Example

4.5.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  ------* means  to pass any character? Indicates  the wildcard of any single character

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  ---------[] means any one of the characters in the wildcard brackets

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

Find/etc-name "host*"-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:

[[email protected] ~]$ find. -name "[A-z]*[4-9].log"-print./script/hs_err_pid32186.log./script/hs_err_pid19725.log./script/hs_err_ Pid5736.log

4.5.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] ~]$ find. -perm 755-print

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

4.5.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

[[email protected] ~]$ find. -name *log./nby/dm_rpt_070001_rds_d.log./nby/rpt_sms_001_d_group.log./nby/rpt_170006_ams_d.log./nby/mls_epp_ Member_lab_info.log./nby/rpt_170009_ams_d.log./nby/bi_td.tsor_page_prmtr_enter_td.log./nby/tmp_act_acct_ info.log./nby/rpt_170010_ams_d.log./nby/bi_td.tsor_br_page_cate_td.log./nby/dpa_pty_onl_rgst_info.log./nby/ Finance.sor_mls_fnd_bill_order.log./.sqoop/shared-metastore.db.log./script/hs_err_pid30353.log./script/hs_err_ pid32186.log./script/hs_err_pid19725.log./script/log./script/log/hiveserver2.log./script/log/ restarthiveserver2.log./script/hs_err_pid5736.log./syf/dm_rpt_090005_pcs_m.log./syf/dm_rpt_090006_pcs_m.log./ Syf/dm_rpt_090004_pcs_m.log[[email protected] ~]$ find. -path "./syf"-prune-o-name *log-print./nby/dm_rpt_070001_rds_d.log./nby/rpt_sms_001_d_group.log./nby/rpt_170006_ ams_d.log./nby/mls_epp_member_lab_info.log./nby/rpt_170009_ams_d.log./nby/bi_td.tsor_page_prmtr_enter_td.log./ Nby/tmp_act_acct_info.log./nby/rpt_170010_ams_D.log./nby/bi_td.tsor_br_page_cate_td.log./nby/dpa_pty_onl_rgst_info.log./nby/finance.sor_mls_fnd_bill_ Order.log./.sqoop/shared-metastore.db.log./script/hs_err_pid30353.log./script/hs_err_pid32186.log./script/hs_ Err_pid19725.log./script/log./script/log/hiveserver2.log./script/log/restarthiveserver2.log./script/hs_err_ Pid5736.log

Example 2: Avoid multiple folders:

[[email protected] ~]$ find.  \ (  -path "./syf"-o-path "./script" \)  -prune-o-name *log-print./nby/dm_rpt_070001_rds_d.log./nby/rpt_sms_ 001_d_group.log./nby/rpt_170006_ams_d.log./nby/mls_epp_member_lab_info.log./nby/rpt_170009_ams_d.log./nby/bi_ Td.tsor_page_prmtr_enter_td.log./nby/tmp_act_acct_info.log./nby/rpt_170010_ams_d.log./nby/bi_td.tsor_br_page_ cate_td.log./nby/dpa_pty_onl_rgst_info.log./nby/finance.sor_mls_fnd_bill_order.log./.sqoop/ Shared-metastore.db.log

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.

4.5.4. 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 finance

Command:

Find ~-user finance-print

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

Command:

find/etc/-user Finance-print

Description

Example 3: 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.

4.5.5. 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/home/finance directory, you can use:

find/home/finance/-group Finance-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

4.5.6. 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.

If you want to find files that are within 5th of change time under System/home/finance/, you can use:

[Email protected] ~]$ find/home/finance/-mtime-5-print/home/finance//home/finance/123.txt/home/finance/.bash_ history/home/finance/data.txt/home/finance/.viminfo/home/finance/.subversion/home/finance/.subversion/ readme.txt/home/finance/.subversion/servers/home/finance/.subversion/auth/home/finance/.subversion/auth/ svn.simple/home/finance/.subversion/auth/svn.username/home/finance/.subversion/auth/svn.ssl.server/home/ finance/.subversion/auth/svn.ssl.client-passphrase/home/finance/.subversion/config/home/finance/script/home/ Finance/script/monitor_hiveserver2_leak.sh/home/finance/script/log/hiveserver2.log

4.5.7. 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.

[[email protected] nby]$ lltotal 500-rw-r--r--1 Finance Finance 123029 DEC 170006.csv-rw-r--r--1 Finance F Inance 4144 Dec 170007.csv-rw-r--r--1 Finance Finance 21075 Dec 170009.csv-rw-r--r--1 Finance Finan  Ce 135309 Dec 170010.csv-rw-rw-r--1 Finance Finance 6811 June bi_td.tsor_br_page_cate_td.log-rw-rw-r-- 1 Finance Finance 6842 June bi_td.tsor_page_prmtr_enter_td.log-rw-r--r--1 Finance finance 129 APR 21 2015  bsn.txt-rw-rw-r--1 Finance Finance 22942 APR dm_rpt_070001_rds_d.log-rw-rw-r--1 Finance Finance 25510 18    dpa_pty_onl_rgst_info.log-rw-rw-r--1 Finance Finance 167 APR finance.csv-rw-rw-r--1 Finance Finance  751 Apr 1 finance.sor_mls_fnd_bill_order.log-rw-rw-r--1 Finance finance 126 Apr finance.txt-rw-rw-r-- 1 Finance Finance 5326 June mls_epp_member_lab_info.log-rw-r--r--1 Finance finance April Nat.txt -rw-rw-r--1 Finance finance 24983 Dec rpt_170006_ams_d.log-rw-rw-r--1 Finance finance 24424 Dec rpt_170009_ams_d.log-rw-rw-r --1 Finance Finance 24495 DEC rpt_170010_ams_d.log-rw-rw-r--1 Finance Finance 513 June rpt_sms_001_ d_group.log-rw-r--r--1 Finance Finance rule.txt-rw-rw-r--1 Finance finance 24856 April tmp_a Ct_acct_info.log[[email protected] nby]$ find-newer bi_td.tsor_br_page_cate_td.log. /170006.csv./rpt_170006_ams_d.log./170009.csv./170010.csv./rpt_170009_ams_d.log./170007.csv./bi_td.tsor_page_ prmtr_enter_td.log./tmp_act_acct_info.log./rpt_170010_ams_d.log./dpa_pty_onl_rgst_info.log[[email  Protected] nby]$ Find! -newer Bi_td.tsor_br_page_cate_td.log./dm_rpt_070001_rds_d.log./rpt_sms_001_d_group.log./finance.csv./mls_epp_ Member_lab_info.log./bsn.txt./finance.txt./nat.txt./bi_td.tsor_br_page_cate_td.log./rule.txt./finance.sor_mls_ Fnd_bill_order.log[[email protected] nby]$ find-newer bi_td.tsor_br_page_cate_td.lOG! -newer bi_td.tsor_page_prmtr_enter_td.log./bi_td.tsor_page_prmtr_enter_td.log[[email protected] nby]$ Find- Newer Bi_td.tsor_br_page_cate_td.log! -newer 170006.csv./170006.csv./170009.csv./170010.csv./bi_td.tsor_page_prmtr_enter_td.log./tmp_act_acct_ Info.log./dpa_pty_onl_rgst_info.log

4.5.8. Use the type option:

Example 1: Find all directories in the/etc directory

Command:

[Email protected] ~]$ find  -type d.. /20160427./export_temp_dpa_rist_cust_init.txt./fanghh./fanghh/lx./fanghh/script./nby./jars./.gnome2./.gnome2/ Keyrings./.sqoop./.subversion./.subversion/auth./.subversion/auth/svn.simple./.subversion/auth/svn.username./. Subversion/auth/svn.ssl.server./.subversion/auth/svn.ssl.client-passphrase./script./script/log./.ssh./syf./tmp ./mls

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

Command:

[[email protected] ~]$ find. ! -type D-print./.history./123.txt./.bashrc./export_temp_dpa_rist_cust_init.txt/.000000_0.deflate.crc./export_ Temp_dpa_rist_cust_init.txt/000001_0.deflate./export_temp_dpa_rist_cust_init.txt/.000001_0.deflate.crc./export _temp_dpa_rist_cust_init.txt/000000_0.deflate./%s./fanghh/test.py./fanghh/rules.py./fanghh/lx/dm_app_finance_ Service_daily_data_20160804.csv./fanghh/script/dm_rpt_130019_fsa_web_02_d.sql./fanghh/script/dpa_fsa_br_base_ Pageview.sql./fanghh/script/dm_rpt_130016_fsa_web_01_d.sql./fanghh/script/dm_rpt_130014_fsa_web_d.sql ...

Example 3: Find all the symbolic link files in the current directory

Command:

[Email protected] ~]$ Find-type l-print./workspace

4.5.9. 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:

[[email protected] ~]$ find. -size +1000000c-print./fanghh/script/dpa_cde_fsa_br_utm_src.init./fanghh/hadoop-0.0.1-jar-with-dependencies.jar ./jars/spark-assembly-1.4.0-hadoop2.4.0.jar./script/log/hiveserver2.log./tmp/export_dpa_crd_cshop_main_ Info.txt

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

4.5.10. 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

4.5.11. 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:

A detailed look at the search command under Linux (find)

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.