November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Linux File Find command to find and xargs Detailed

Find command general form;

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


2, find the parameters of the command;

  pathname: the find command to find the path to the directory.  For example, used to represent the current directory, use / to represent the system root directory.
 -Print: find command files that match the output to standard output.
 -Exec: find command files that match the parameters given shell command.  Corresponding commands in the form of 'command' {} \;, Note {} \; between spaces.
 -Ok: same-exec role, but in a more secure mode to execute shell commands given by the parameter will be prompted before each command is executed, allowing the user to determine whether to execute. 


3, find command options

  -Name

 Find the file according to the file name.

 -Perm
 To find files by file permissions.

 -Prune
 Use this option if you can not in the specified directory to find the find command, If you use the-depth option at the same time, then-prune is find command to ignore.

 -User
 In accordance with the owner of the file to locate the file.

 -Group
 In accordance with the group the file belongs to locate the file.

 -Mtime-n + n
 Change the time according to the file to locate the file - the file to change the time from the within n days now, + n represents a file changes from now n days ago.  find command-atime and-ctime option, but they are all-m time option.

 -Nogroup
 Find no valid group owning the file, the file belongs to the group does not exist in the / etc / groups.

 -Nouser
 Find no valid owner of the file, the owner of the file / etc / passwd does not exist.
 -Newer file1! File2

 The file file1 new but older than the file file2 files to find the changes.
 -Type

 Finding a certain type of file, such as:

 b - block device file.
 d - directory.
 c - character device file.
 p - pipe file.
 l - symbolic link files.
 f - file.

 -Size n: [c] Find the file length n block of the file, said file length in bytes with c.
 -Depth: find files, first find the file in the current directory and find then its subdirectories.
 -Fstype: Find the file in a particular type of file system, these file system types can usually be found in the configuration file / etc / fstab, the system contains information about the file system in the configuration file.

 -Mount: find files across file system mount points.
 -Follow: find command encounters a symbolic link file, on track to file pointed to by the link.
 -Cpio: cpio command matching files These files are backed up to a tape device. 

Additionally, the following three differences:

  -Amin n
 Find the last N minutes to access the file-atime n
 Find the last n * 24-hour access to the file-cmin n
 Find system last N minutes to change the file file-ctime n
 Find system last n * 24 hours to change the state of the file file-mmin n
 Find system last N minutes to change the file data files-mtime n
 Find system last n * 24 hours to change the file data files 


4, execute shell commands using the exec or ok

Use find, as long as the operation you want to write a file, you can use exec to match find find very convenient

Some operating systems only allow-exec option such as ls or ls-l command. Most users use this option to find old files and delete them. Recommended before the real execution rm command to delete the file, it is best to use the ls command look to confirm that they want to delete the file.

the exec option followed with the command or script to be executed, and then a pair of children {}, a space and a \, and finally a semicolon. In order to use the exec option, you must also use the print option. Verify the find command, you will find the command only output from the current path from the relative path and file name.

For example: ls-l command lists the files matched the ls-l command on the find-exec command options

  # 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 

The above example, find command match to all ordinary files in the current directory, use the ls-l command will list them in the-exec option.
Find / logs directory change the time in the five days prior to the file and delete them:

  $ Find logs-type f-mtime +5-exec rm {} \; 

Remember: in any way with the shell before you delete a file, you should view the file must be careful! Such as mv or rm command, you can use the-exec option of safe mode. It will prompt you before each match to file.

In the example below, find the command in the current directory to find all file names. LOG the end, change the file time in 5 days, and delete them, but prompted before deleting.

  $ Find.-Name "*. Log"-mtime +5-ok rm {} \;
 <Rm .... / conf / httpd.conf>? N 

Press y to delete the file, press n is not deleted.

Any form of command can use the-exec option.

In the following example, we use the grep command. The find commands first file named “passwd *” matches all files, such as the passwd, passwd.old, passwd.bak, and then execute the grep command to see whether there is a sam user in these documents.

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


Second, find examples of command;


1, to find all the files in the current user’s home directory:

The following two methods can be used

  $ Find $ HOME-print
 $ Find ~-print 


2, so that the current directory in the file owner has read and write permissions, and the file belongs to the group of users and other users have read access to the file;

  $ Find.-Type f-perm 644-exec ls-l {} \; 


3, in order to find the length of the file system for ordinary files, and list their complete path;

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


4, look for changes in the directory / var / logs time in the seven days prior to the regular file and ask before deleting them;

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


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

  $ Find.-Group root-exec ls-l {} \;
 -Rw-r - r - 1 root root 595 10 31 01:09 ./fie1 


6, find the command will be deleted when the directory access time since the 7th, the file containing numeric suffix admin.log.

This command checks only three digits, so the corresponding file suffix should not exceed 999. To build the first few admin.log * file, use the following command to

  $ 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 the current file system directory and sort;

  $ Find.-Type d | sort 


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

  $ Find / dev / rmt-print 


Third, xargs

xargs – build and execute command lines from standard input

When matched to the file-exec option processing using the find command, find command with all matching files passed to the exec execution. Some systems can be passed to the exec command length limit, so the find command to run a few minutes later, there will be an overflow error. The error message is usually “parameter column is too long” or “parameter column overflow. This is the xargs command useful where, in particular, used in conjunction with the find command.

The find command to match to the file passed to xargs command, xargs command each time only for a part of the file, but not all, unlike the-exec option as. So that it can get the first part of the file, and then the next batch, and so continue.

In some systems, the use-exec option will initiate a process to deal with every match to file, not the match to all the documents as a parameter to perform; process so that in some cases there will be too much, the problem of system performance degradation, and thus the efficiency is not high;

The xargs command only one process. Use xargs command, what is the time for all parameters, or in batches to obtain parameters, as well as every obtain the number of parameters will be determined according to the adjustable parameters in the command options and system kernel.

Take a look at how xargs command used in conjunction with the find command, and give some examples.

What kind of file they belong to the following example to find an ordinary file system, and then use the xargs command to test

  # Find.-Type f-print | xargs file
 . / .kde / Autostart / Autorun. desktop : UTF-8 Unicode English text
 . / .kde / Autostart / .directory: ISO-8859 text \
 ...... 

Find the entire system memory dump files (core dump), and then save the results to a file / tmp / core.log:

  $ Find /-name "core"-print | xargs echo ""> / tmp / core.log 

Above this execution is too slow, I change the current directory to find

  # Find.-Name "file *"-print | xargs echo ""> / temp / core.log
 # Cat / temp / core.log
 ./file6 

All users have read, write, and execute permissions to the file in the current directory to find and recover the appropriate write permissions:

  # Ls-l
 drwxrwxrwx 2 sam adm 4096 10 30 20:14 file6
 -Rwxrwxrwx 2 sam adm 0 10 31 01:01 http3.conf
 -Rwxrwxrwx 2 sam adm 0 10 31 01:01 httpd.conf

 # Find.-Perm -7-print | xargs chmod ow
 # Ls-l
 drwxrwxr-x 2 sam adm 4096 10 30 20:14 file6
 -Rwxrwxr-x 2 sam adm 0 10 31 01:01 http3.conf
 -Rwxrwxr-x 2 sam adm 0 10 31 01:01 httpd.conf 

Grep command in all common file search hostname word:

  # 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 

Hostnames word with the grep command to search all ordinary 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 above example, \ is used to cancel a special meaning in the shell find command *.

The find command is used in conjunction with exec and xargs allows the user to perform almost all of the command of the match to the file.


, Find the parameters of the command

Below find examples of some of the commonly used parameters, useful to look up on the line, like a few posts on the front, which some parameters are used, but also can find command with other post the man or view forum manual


1, using the name option

The file name option is the find command is the most commonly used options, the option either used alone or with other options.

Can use a file name pattern to match files, remember to quote the the filename pattern caused.

No matter what the current path, if you want to find the file name in the root directory $ HOME meet the * txt file, use ~ as ‘pathname’ parameter, tilde ~ represents your $ HOME directory.

  $ Find ~-name "*. Txt"-print 

In the current directory and subdirectories you want to find all the ‘*. Txt’ file, you can use:

  $ Find.-Name "*. Txt"-print 

Find the file name in the current directory and subdirectories want a capital letter at the beginning of the file, you can use:

  $ Find.-Name "[AZ] *"-print 

Want to find the file name in the / etc directory to host beginning of the file, can be used:

  $ Find / etc-name "host *"-print 

Want to Find the file $ HOME directory, you can use:

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

To get high-load operation of the system, to find all the files from the root directory.

  $ Find /-name "*"-print 

If you want to find the file name in the current directory to the two lowercase letter followed by two numbers, the last txt file, the following command will be able to return to a file named ax37.txt:

  $ Find.-Name "[az] [az] [0 - 9] [0 - 9]. Txt"-print 


2, with the perm option

-Perm option, according to the file permissions mode to find the file, then the file permissions mode. Best use permissions octal notation.

Find files in the current directory permissions to 755 files, the file owner can read, write, execute, and other users can read and execute the file, you can use:

  $ Find.-Perm 755-print 

There is also a form of expression: to add a dash in front of octal numbers – said match, as -007 is equivalent to 777, -006 is 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 October 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: the file permissions fits mode

-Perm + mode: file permissions part in line mode

-Perm-mode: file permission is completely in line with the mode


3, to ignore a directory

If you find a file when want to ignore a directory, because you know that the directory does not find the files you want, then you can use the-prune option to point out the need to ignore the directory. -Prune option should be careful, because if you use the-depth option, then-prune option would be find command to ignore.

If you want to find the file, directory / apps / apps / bin directory but do not want to find, you can use:

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


Find find files how to avoid a file directory

Such as Find subdirectory of dir1 not all the files in the directory / usr / sam

  find / usr / sam-path "/ usr/sam/dir1"-prune-o-print 
  find [-path ..] [expression] is followed in the path list is the expression 

-Path “/ usr / sam”-prune-o-print-path “/ usr / sam-a-prune-o
-Print shorthand expression is evaluated in order-a and-o are short-circuit evaluation value, with the shell’s && and | | similar if-path “the / usr / sam” is true, then the evaluator-prune-prune returns true, the logical expression is true; otherwise not seek value-prune logical expression is false. If the-path “/ usr / sam-a-prune false, is seeking value-print,-print returns true or logical expression is true; otherwise not evaluated-print, or logical expression is true.

This expression is a combination of a special case can be written as 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 represents the combination of the expression.

  \ References that indicate the shell right behind the characters for special explanation, and left to find the command to explain its significance. 

Find a determined file,-name options-o

  # Find / usr / sam \ (-path / usr/sam/dir1-o-path / usr/sam/file1 \)-prune-o-name "temp"-print 


5, the use of the user and nouser option

According to the owner of the file to find the file, such as $ HOME directory lookup file are Lord sam file, you can use:

  $ Find ~-user sam-print 

Find files in the / etc directory is owned by the uucp file:

  $ Find / etc-user uucp-print 

In order to find the owner of the account has been deleted files, you can use the-nouser option. So that you can find who the owner is not a valid account files in the / etc / passwd file. -Nouser option is used, it is not necessary to give the user name; find command to be able to complete the work for you.

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

  $ Find / home-nouser-print 


6, group and nogroup option

Belong to the user group for the file, find the command also has the same options as the user and nouser option in order to find the files belonging to the user group’s gem in the / apps directory, you can use:

  $ Find / apps-group gem-print 

Not effectively belongs to the user group you want to find all the files, nogroup option. Following the find command to find such files from the root directory of the file system

  $ Find /-nogroup-print 


7, change the time or access time find files

If you want to change the time to find the file, you can use the mtime, atime or ctime options. If the system suddenly there is no space available, likely the length of a file in the meantime has grown rapidly, then you can find this file mtime option.

Minus sign – to qualify change time in less than n days ago, with a plus sign + to qualify change file time ago n days ago.

I hope to change the time in the system root directory search files in 5 days or less, you can use:

  $ Find /-mtime -5-print 

In order to find the changes in the directory / var / adm file before the 3rd, you can use:

  $ Find / var / adm-mtime +3-print 


8, find the file than a file is new or old

If you want to find the changes the new all files older than another file than a file, you can use the-newer option. Its general form:

  newest_file_name! oldest_file_name 

Among them, Are logical non Symbols.

Find change the time than the file sam new but older than the file temp file:

Example: There are two files

  -Rw-r - r - 1 sam adm 0 10 31 01:07 fiel
 -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 October 26 19:48 sam
 -Rw-rw-rw-1 root root 2792 10 31 20:19 temp

 # Find-newer httpd1.conf!-Newer temp-ls
 1077669 0-rwxrwxr-x 2 sam adm 0 10 Yue 31 01:01 / httpd.conf
 1,077,671 4-rw-rw-rw-1 root root 2792 10 31 20:19. / Temp
 1,077,673 0-rw-r - r - 1 sam adm 0 10 May 31 01:07. / Fiel 

Find change the time than the temp file new file:

  $ Find.-Newer temp-print 


9, using the type option

Find all directories under the / etc directory, you can use:

  $ Find / etc-type d-print 

In addition to a directory other than the current directory to find all types of files, you can use:

  $ Find.!-Type d-print 

Find all symbols linked file, you can use the / etc directory

  $ Find / etc-type l-print 


10, using the size option

In accordance with the length of the file to find the file, the length of the documents referred to here either block (block) to measure, can also be measured in bytes. In the form of expression of the byte length of the measurement file of N C; only to the length of the block measured File can be expressed numerically.

Find files in accordance with the length of the file, the general use of the file length in bytes, in view of the size of the file system, because the block measured more easily converted.
File length is greater than 1 M bytes of the file in the current directory to find:

  $ Find.-Size +1000000 c-print 

Find files larger than 10M in the current directory:

  find.-size +10000 k-exec ls-ld {}; 

To find out the file is copied to another place:

  find *. c-exec cp '{}' / tmp ';' 

Find the length of the file is exactly 100 bytes in / home / apache directory file:

  $ Find / home / the apache -size 100c-print 

Find the length of more than 10 blocks of files in the current directory (one is equal to 512 bytes):

  $ Find.-Size +10-print 


11, using the depth option

When using the find command, you may want to match all of the files in the subdirectory Find. The depth options can find the command to do so. To do so is one of the reasons, when using the find command to a tape backup of the file system, you want to first back up all files, followed by re-backup files in subdirectories.

In the following example, the find command from the root directory of the file system, Find file named CON.FILE.

It will match all files first then into a subdirectory.

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


12, using the mount option

Find the file (do not enter the other file system) file system, you can use the find command mount option.

In the file name in the file system XC end of file from the current directory to find:

  $ Find.-Name "*. XC"-mount-print 

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>