Linux Basic Tutorial - find Command

 



     Brothers Linux Training: Detailed explanation of the 12 common parameters of the find command of the linux command (including specific usage and precautions www.lampbrother.net)

 1. Use the name option:

  the file name option is the most commonly used option for the find command, or use this option alone , or with other options.

  A filename pattern can be used to match files, remember to enclose the filename pattern in quotes.

  No matter what the current path is, if you want to find a file whose file name matches *.log in your root directory $HOME, use ~ as the 'pathname' parameter, and the tilde ~ represents your $HOME directory.

  The code to copy the code is as follows: find ~ -name "*.log" -print

  To find all '*.log' files in the current directory and subdirectories, you can use: The code to

  copy the code is as follows: find . -name "*. log" -print   wants   to

  find files in the current directory and subdirectories whose file names start with an uppercase letter, you can use: To find files whose file names start with host, you can use:   Copy the code as follows: find /etc -name "host*" -print   To find files in the $HOME directory, you can use:   Copy the code as follows: find ~ - name "*" -print or find . -print   To make the system run under heavy load, look for all files starting from the root directory.













  The code to copy the code is as follows: find / -name "*" -print

  If you want to find files whose names start with lowercase letters in the current directory, and end with 4 to 9 plus .log at the end:

  Command:

  Copy the code as follows: find . -name "[az]*[4-9].log" -print

  output:

  copy code The code is as follows: [root@localhost test]# ll

  total 316

  -rw-r--r-- 1 root root 302108 11- 13 06:03 log2012.log

  -rw-r--r-- 1 root root 61 11-13 06:03 log2013.log

  -rw-r--r-- 1 root root 0 11-13 06:03 log2014. log

  -rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxr-x 2 root root 4096 11-13 06 :08 test3

  drwxrwxr-x 2 root root 4096 11-13 05:50 test4

  [root@localhost test]# find . -name "[az]*[4-9].log" -print

  ./log2014.log

  ./log2015.log

  ./test4/log2014.log

  [root@localhost test]#

  2. Use the perm option:

  Use the -perm option according to the file permission mode to find the file according to the file permission mode. It is best to use octal notation for permissions.

  For example, to find a file whose file permission bit is 755 in the current directory, that is, the file owner can read, write, and execute, and other users can read and execute files, you can use:

  Copy the code The code is as follows: [root@localhost test]# find . -perm 755 -print

  .

  ./scf

  ./scf/lib

  ./scf/service

  ./scf/service/deploy

  ./scf/service/deploy/product

  ./scf/service/deploy/info

  ./scf/doc

  ./scf/bin

  [root@localhost test] #There

  is another way of expression: add a horizontal bar - in front of the octal number to indicate that they all match, such as -007 is equivalent to 777, -005 is equivalent to 555,

  command:

  copy The code code is as follows: find . -perm -005

  output:

  copy the code The code is as follows: [root@localhost test]# ll

  总计 316

  -rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

  -rw-r--r-- 1 root root 61 11-13 06:03 log2013.log

  -rw-r--r-- 1 root root 0 11-13 06:03 log2014.log

  -rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxr-x 2 root root 4096 11-13 06:08 test3

  drwxrwxr-x 2 root root 4096 11-13 05:50 test4

  [root@localhost test]# find . -perm -005

  .

  ./test4

  ./scf

  ./scf/lib

  ./scf/service

  ./scf/service/deploy

  ./scf/service/deploy/product

  ./scf/service/deploy/info

  ./scf/doc

  ./scf/bin

  ./test3

  [root@localhost test]#

3. Ignore a certain directory:

Brother Linux Training www.lampbrother.net

  If you want to ignore a certain directory when looking for files, because you know that there is no file you are looking for in that directory, you can use - prune option to indicate directories to ignore. Be careful when using the -prune option, because if you also use the -depth option, the -prune option will be ignored by the find command. If you want to find files in the test directory, but not in the test/test3 directory, you can use:

  command:

  copy the code The code is as follows: find test -path "test/test3" -prune -o -print

  output:

  copy code code As follows: [root@localhost soft]# find test -path "test/test3" -prune -o -print

  test

  test/log2014.log

  test/log2015.log

  test/test4

  test/test4/log2014.log

  test/test4/log2013 .log

  test/test4/log2012.log

  test/scf

  test/scf/lib

  test/scf/service

  test/scf/service/deploy

  test/scf/service/deploy/product

  test/scf/service/deploy/info

  test/scf/doc

  test/scf/bin

  test/log2013.log

  test/log2012.log

  [root@localhost soft]#

  4. Use find to find How to avoid a certain file directory when using a file:

  Example 1: Find all files in the test directory that are not in the test4 subdirectory

  Command :

  Copy the code The code is as follows: find test -path "test/test4" -prune -o -print

  Output:

  Copy code The code is as follows: [root@localhost soft]# find test

  test

  test/log2014.log

  test/log2015.log

  test/test4

  test/test4/log2014.log

  test/test4/log2013.log

  test/test4/log2012. log

  test/scf

  test/scf/lib

  test/scf/service

  test/scf/service/deploy

  test/scf/service/deploy/product

  test/scf/service/deploy/info

  test/scf/doc

  test/scf/bin

  test/log2013.log

  test/log2012.log

  test/test3

  [root@localhost soft]# find test -path "test/test4" -prune -o -print

  test

  test/log2014.log

  test/log2015.log

  test/scf

  test/scf/lib

  test/scf/service

  test/scf/service/deploy

  test/scf/service/deploy/product

  test/scf/service/deploy/info

  test/scf/doc

  test/scf/bin

  test/log2013.log

  test/log2012.log

  test/test3

  [root@localhost soft]#

  Description:

  Copy the code The code is as follows: find [-path ..] [expression]

  After the path list is the expression

  -path "test" -prune -o -print is -path "test" -a -prune -o -print Shorthand expressions are evaluated in order, -a and -o are short-circuit evaluation, similar to shell's && and || If

  -path "test" is true, evaluate -prune , -prune returns true, and the logical expression is true; otherwise -prune is not evaluated, and the logical expression is false. If -path "test" -a -prune is false, -print is evaluated, -print returns true, or the logical expression is true; otherwise -print is not evaluated, or the logical expression is true.

  This special case of expression combination can be written in pseudocode as:

  if -path "test" then

  -prune

  else

  -print

  Example 2: Avoid multiple folders:

  Command:

  Copy the code The code is as follows: find test \( -path test/test4 -o -path test/test3 \) -prune -o -print

  output:

  Copy the code The code is as follows: [root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print

  test

  test/log2014.log

  test/log2015.log

  test/scf

  test/scf/lib

  test/scf/service

  test/scf/service/deploy

  test/scf/service/deploy/product

  test/scf/service/deploy/info

  test/scf/doc

  test/scf/bin

  test/log2013.log

  test/log2012.log

  [root@localhost soft]#

  Description:

  Parentheses indicate the combination of expressions. \ represents a quote, that is, instructs the shell not to interpret the following characters specially, and leave it to the find command to interpret its meaning.

  Example 3: Find a certain file, add options such as -name after -o

  Command :

  Copy the code as follows: find test \(-path test/test4 -o -path test/test3 \) -prune -o -name " *.log" -print

  Output:

  Copy the code The code is as follows: [root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print

  test/log2014.log

  test/log2015.log

  test/log2013.log

  test/log2012.log

  [root@localhost soft]#

  5. Use user and nouser options:

  Find files by file owner:

  Example 1: Find the file owner in the $HOME directory as peida's file

  Command :

  Copy the code The code is as follows: find ~ -user peida -print

  Example 2: Find the file whose owner is peida in the /etc directory:

  Command:

  Copy the code The code is as follows: find /etc -user peida -print

  Description :

  Example 3: To find files that have been deleted by the owner account, use the -nouser option. Find all such files in the /home directory

  Command :

  Copy the code The code is as follows: find /home -nouser -print

  Description:

  This will find files whose owners do not have a valid account in the /etc/passwd file. When using the -nouser option, you do not have to give a username; the find command does the work for you.

  6. Use the group and nogroup options:

  just like the user and nouser options, the find command also has the same options for the user group to which the file belongs. In order to find files belonging to the gem user group in the /apps directory, you can use:

  Copy The code code is as follows: find /apps -group gem -print

  To find all files that do not have a valid user group, you can use the nogroup option. The following find command finds such a file from the root directory of the file system:

  Copy the code as follows: find / -nogroup-print

  7. Use the depth option:

  When using the find command, you may want to match all files first, and then in the sub Find in the directory. Use the depth option to make the find command do this. One reason for this is that when using the find command to back up a filesystem to tape, it is desirable to back up all files first, and then back up files in subdirectories.

  Example 1: The find command starts from the root directory of the file system and looks for a file named CON.FILE.

  Command:

  Copy the code The code is as follows: find / -name "CON.FILE" -depth -print

  Description:

  It will first match all files and then enter subdirectories to find

  12. Use the mount option:

  find files in the current file system (without entering other filesystems), you can use the mount option of the find command.

  Example 1: Find the file whose file name ends with XC in this file system from the current directory

  Command :

  Copy the code as follows: find . -name "*.XC" -mount -print

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326410828&siteId=291194637