Linux File Lookup command find,xargs details "Go"

Source: Internet
Author: User
Tags uppercase letter

Transferred from: http://blog.csdn.net/cxylaf/article/details/4069595

Transfer from http://www.linuxsir.org/main/?q=node/137

Linux File Lookup command Find,xargs details

Summary:zhy2111314
from: linuxsir.org
Finishing: North-South south-north
Abstract: This is a detailed description of the Find command, the valuable is to give a lot of examples of parameters, a large number of examples, so that beginners easier to understand; This article is zhyfly in the forum, I have to re-organize this article, for the convenience of everyone to read;

Directory

Copyright notice
Preface: About the Find command

One, find command format
1. The general form of the Find command is;
2, the parameters of the Find command;
3, find command options;
4. Use exec or OK to execute shell commands;

Ii. examples of the Find command;

1. Find all files under the current user's home directory;
2, in order to be in the current directory of the file owner has read, write permissions, and the file belongs to the group of users and other users have Read permission files;
3, in order to find all the files in the system file length of 0 ordinary files, and list their full path;
4. Look for common files in the/var/logs directory that were changed before 7th, and ask them before deleting them;
5, in order to find all the files belonging to the root group in the system;
6. The Find command will delete the Admin.log file that contains the digital suffix since the access time in the directory was 7th
7, in order to find all the directories in the current file system and sorting;
8, in order to find all the RMT tape devices in the system;

Third, Xargs
Four, the parameters of the Find command;

1. Use the name option
2. With PERM option
3. Ignore a directory
4. How to avoid a file directory when finding files with find
5. Use the user and Nouser options
6. Use Group and Nogroup options
7, according to change time or access time, etc. find files
8. Find newer or older files than a file
9. Use the Type option
10. Use the SIZE option
11. Using the Depth option
12. Using the Mount option

V. about this article
Vi. Related Documents


+++++++++++++++++++++++++++++++++++++++++++++++++
Body
+++++++++++++++++++++++++++++++++++++++++++++++++


Copyright notice

This article is Zhyfly brother posted in the linuxsir.org of a post and sorted out, if you have doubts about the copyright, please follow the thread behind it. Thank you; The HTML version of this article is compiled from north to south, modified the full width of the entire document and the text of the word in the words of each letter space, add a number for the title, convenient for everyone to read;

Preface: About the Find command

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 it can take a long time to traverse a large file system (this refers to a file system with more than 30G bytes).


One, find command format


1. The general form of the Find command is;

find pathname -options [-print -exec -ok ...]


2, the parameters of the Find command;

pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为‘command‘ {  } /;,注意{   }和/;之间的空格。
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。


3. Find command Options

-name 

按照文件名查找文件。

-perm 
按照文件权限来查找文件。

-prune 
使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。

-user 
按照文件属主来查找文件。

-group 
按照文件所属的组来查找文件。

-mtime -n +n 
按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。

-nogroup 
查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

-nouser 
查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 

查找更改时间比文件file1新但比文件file2旧的文件。
-type 

查找某一类型的文件,诸如:

b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。

-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。

-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

In addition, the following three differences:

   -amin n
  查找系统中最后N分钟访问的文件

  -atime n
  查找系统中最后n*24小时访问的文件

  -cmin n
  查找系统中最后N分钟被改变文件状态的文件

  -ctime n
  查找系统中最后n*24小时被改变文件状态的文件

    -mmin n
  查找系统中最后N分钟被改变文件数据的文件

  -mtime n
  查找系统中最后n*24小时被改变文件数据的文件


4. Use exec or OK to execute shell commands

When using find, just write the desired action in a file, you can use the exec to match the Find lookup, 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.

For example, to list the matching files with the Ls-l command, you can place 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 example above, the Find command matches all the normal files in the current directory and lists them using the Ls-l command in the-exec option.
In the/logs directory, look for files that change time before 5th and delete them:

$ find logs -type f -mtime +5 -exec rm {  } /;

Remember: before the shell can delete files in any way, you should look at the appropriate files before you must be careful! You can use the Safe mode of the-EXEC option when using a command such as MV or RM. It will prompt you before you work on each file that is matched to it.

In the following example, the Find command finds all filenames in the current directory. Log end, change files over the 5th, and delete them, but give a hint before deleting them.

$ find . -name "*.conf"  -mtime +5 -ok rm {  } /;
< rm ... ./conf/httpd.conf > ? n

Press the Y key to delete the file and press N to not delete it.

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 named "passwd*", such as passwd, Passwd.old, Passwd.bak, and then executes the grep command to see if there is a SAM user in these files.

# find /etc -name "passwd*" -exec grep "sam" {  } /;
sam:x:501:501::/usr/sam:/bin/bash


Ii. examples of the Find command;


1. Find all files under the current user's home directory:

Here are two ways to use

$ find $HOME -print
$ find ~ -print


2, let the current directory of the file owner has read, write permissions, and the file belongs to the group of users and other users have Read permission files;

$ find . -type f -perm 644 -exec ls -l {  } /;


3, in order to find all the files in the system file length of 0 ordinary files, and list their full path;

$ find / -type f -size 0 -exec ls -l {  } /;


4. Look for common files in the/var/logs directory that were changed before 7th, and ask them before deleting them;

$ find /var/logs -type f -mtime +7 -ok rm {  } /;


5, in order to find all the files belonging to the root group in the system;

$find . -group root -exec ls -l {  } /;
-rw-r--r--    1 root     root          595 10月 31 01:09 ./fie1


6. The Find command will delete the Admin.log file that contains the digital suffix since the access time in the directory was 7th.

This command checks only three digits, so the suffix of the corresponding file should not exceed 999. Build several admin.log* files before using this 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, in order to find all the directories in the current file system and sorting;

$ find . -type d | sort


8, in order to find all the RMT tape devices in the system;

$ find /dev/rmt -print


Third, Xargs

Xargs-build and execute command lines from standard 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.

Take a look at how the Xargs command is used with the Find command, and give some examples.

The following example finds every normal file in the system, and then uses the Xargs command to test what 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/
......

Find 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 above execution is too slow, I changed to find in the current directory

#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6

In the current directory, look for files with read, write, and execute permissions for all users, and reclaim the appropriate write permissions:

# ls-l
DRWXRWXRWX 2 Sam adm 4096 October 20:14 File6
-RWXRWXRWX 2 Sam Adm 0 October 01:01 http3.conf
-RWXRWXRWX 2 Sam Adm 0 October 01:01 httpd.conf

# Find. -perm-7-print | Xargs chmod o-w
# ls-l
Drwxrwxr-x 2 Sam adm 4096 October 20:14 File6
-rwxrwxr-x 2 Sam Adm 0 October 01:01 http3.conf
-rwxrwxr-x 2 Sam Adm 0 October 01:01 httpd.conf

Use the grep command to search for the word hostname in all common 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 normal 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 example above,/is used to cancel the special meaning of the * in the shell in the Find command.

The Find command, with exec and Xargs, allows the user to execute almost all commands against the matching file.


Iv. parameters for the Find command

Here are some examples of find some common parameters, useful to the time to check on the line, like the previous posts above, have used some of the parameters, you can also use man or view other posts in the Forum have find command manual


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

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 或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 with two lowercase letters, followed by two digits, and finally the. txt file, the following command will be able to return a file named Ax37.txt:

$find . -name "[a-z][a-z][0--9][0--9].txt" -print


2. With 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:

$ 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,-006 equivalent to 666

# ls -l
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rw-rw-rw-    1 sam      adm         34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 10月 26 19:48 sam
-rw-rw-rw-    1 root     root         2792 10月 31 20:19 temp

# find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp

-perm mode: File license exactly matches mode

-perm +mode: File License section complies with mode

-perm-mode: File license fully complies with mode


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/apps directory but do not want to find it in the/apps/bin directory, you can use:

$ find /apps -path "/apps/bin" -prune -o -print


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

For example, to find all files in the/usr/sam directory that are not within the DIR1 subdirectory

find /usr/sam -path "/usr/sam/dir1" -prune -o -print

find [-path ..] [expression] 在路径列表的后面的是表达式

-path "/usr/sam"-prune-o-print is-path "/usr/sam"-a-prune-o
-print shorthand expressions are evaluated sequentially,-A and-O are short-circuit evaluated, with Shell's && and | | Similarly, if-path "/usr/sam" is true, then the evaluation-prune,-prune returns True, and the logical expression is true; otherwise, the-prune is not evaluated, and the logical expression is false. If the-path "/usr/sam"-a-prune is false, the-print is evaluated, the-print returns True, or the logical expression is true, otherwise no value-print, or the logical expression is true.

This combination of expressions can be written in pseudo-code

if -path "/usr/sam"  then
          -prune
else
          -print

Avoid multiple folders

find /usr/sam /( -path /usr/sam/dir1 -o -path /usr/sam/file1 /) -prune -o -print

Parentheses represent the combination of expressions.

/ 表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。

Find a certain file,-name and other options 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

Find files by file owner, such as files in the $home directory where the file belongs to Sam, you can use:

$ find ~ -user sam -print

Look for files in the/etc directory that belong to the main UUCP:

$ find /etc -user uucp -print

In order to find files that are already deleted from the master account, you can use the-nouser option. 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.

For example, to find all such files in the/home directory, you can use:

$ find /home -nouser -print


6. Use 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 files from the root directory of the file system

$ find / -nogroup-print


7, according to change time or access time, etc. find files

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. Find newer 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.

Look for files that change time than file Sam new but older than file temp:

Example: There are two of files

-rw-r--r--1 Sam Adm 0 October 01:07 Fiel
-rw-rw-rw-1 Sam Adm 34890 October 00:57 httpd1.conf
-rwxrwxr-x 2 Sam Adm 0 October 01:01 httpd.conf
Drw-rw-rw-2 Gem Group 4096 October 19:48 Sam
-rw-rw-rw-1 root root 2792 October 20:19 Temp

# Find-newer Httpd1.conf! -newer Temp-ls
1077669 0-rwxrwxr-x 2 Sam Adm 0 October to 01:01./httpd.conf
1077671 4-rw-rw-rw-1 root root 2792 October to 20:19./temp
1077673 0-rw-r--r--1 Sam Adm 0 October to 01:07./fiel

Look for a new file that changed in time than the temp file:

$ find . -newer temp -print

9. Use the Type option

To find all the directories in/etc directory, you can use:

$ find /etc -type d -print

To find all types of files except directories in the current directory, you can use:

$ find . ! -type d -print

In the/etc directory to find all the symbolic link files, you can use

$ 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.
In the current directory, look for files with a file length greater than 1 m bytes:

$ find . -size +1000000c -print

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

$ find /home/apache -size 100c -print

Look for files that are longer than 10 blocks in the current directory (a block equals 512 bytes):

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

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 to the subdirectory to find them.

$ find / -name "CON.FILE" -depth -print


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

From the current directory, look for files in the file system with the file name ending in XC:

$ find . -name "*.XC" -mount -print



V. about this article

This article is a detailed description of the Find command, the valuable is to give a lot of examples of parameters, a large number of illustrations, so that beginners easier to understand; This article is zhy2111314 in the forum, I have to re-organize this article, for the convenience of everyone to read;

Linux File Lookup command find,xargs details "Go"

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.