Shell Programming - [05] Linux find command Detailed

Shell Programming [series] https://blog.csdn.net/ilo114/article/category/8961526

High frequency parameters:

-name Find the file name

Find / etc directory ending in conf file

 find /etc -name '*conf'

Find -iname ignore case based on the file name

Find the file named abc file in the current directory, and case-insensitive

 find ./ -iname abc

According to their user -user Find

Find the current directory for the file owner testuser file

 find ./ -user testuser

Find -group according to user group

Find the file is a work group of all files

 find . -group work

Depending on the type -type Find

  • f file find. -type f
  • d directory find. -type d
  • c character device file find. -type c
  • b block device file find. -type b
  • l linked file find. -type l
  • p pipe file find. -type p

Depending on the size -size Find

  • n -n smaller than the size of the file
  • + N is greater than n files smaller than

Example 1: Find File 10000 bytes less than the / etc directory

 find /etc -size -10000c

Example 2: Find the files larger than 1M / etc directory

 find /etc -size +1M

-mtime file changes according to time to find (days)

  • Within days of modified files -nn
  • + Modified files other than nn days
  • n exactly n days to modify file

Example 1: Find / Modify conf and at the end of 5 days etc directory files

find /etc -mtime -5 -name '*.conf'

Example 2: Find / modified owner etc directory and 10 days before the root file

 find /etc -mtime +10 -user root

-mmin change depending on the file to find the time (minutes)

  • -Nn modified files within minutes
  • + Modified files other than nn minutes

Example 1: Find files modified 30 minutes before the / etc directory

find /etc -mmin +30

Example 2: Find modifications within 30 minutes of the / etc directory directory

find /etc -mmin -30 -type d

-mindepth n represents the n-level subdirectory Find

Example 0: 3 level in a subfolder of / etc Start Search

 find /etc -mindepth 3 

-maxdepth n n represents up to find a subdirectory level

Example 1: / etc files under Searching for qualifying, but the most searched subdirectory level 2

find /etc -maxdepth 3 -name '*.conf'

Example 2:

find ./etc/ -type f -name '*.conf' -size +10k -maxdepth 2

Learn parameters:

Find -nouser not belong to the user's home

find . -type f -nouser

-nogroup Find user belongs to no group

find . -type f -nogroup

Find -perm based on user permissions

find . -perm 664

-prune exclude specific directories to find

  • It is usually used with -path, for specific directory search criteria exclude

Example 1: Find the current all common file directory, but exclude the test directory

find . -path ./etc -prune -o -type f

Example 2: Find all the ordinary files in the current directory, but exclude etc, and opt directory

find . -path ./etc -prune -o -path ./opt -prune -o -type f

Example 3: Find all the ordinary files in the current directory, but exclude etc, and opt directory, but the owner is hdfs

find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs

Example 4: Find the current directory of all common files, etc, and opt excluding directories, but is owned by the HDFS, and the file size must be larger than 500 bytes

find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs -a -size +500c

-newer file1

find /etc -newer a

operating:

-print print output

-exec perform specific operations on the searched file format -exec 'command' {};

Example 1: Search / file (non-directory) under etc, files ending in conf, and greater than 10k, and then delete it

find ./etc/ -type f -name '*.conf' -size +10k -exec rm -f {} \;

Example 2: under / var / log / directory to the end of the log file, delete and change the time more than 7 days

find /var/log/ -name '*.log' -mtime +7 -exec rm -rf {} \;

Example 3: 1 search criteria and examples of the same, but does not remove, but to copy it to / root / conf directory

find ./etc/ -size +10k -type f -name '*.conf' -exec cp {} /root/conf/ \;

-ok and exec function the same, but each operation will give users tips

Logical Operators:

-a and

-o or

-not |! non

Example 1: Find the current directory, not the owner of all files hdfs

find . -not -user hdfs 	|	find . ! -user hdfs

File search the current directory, is the main part of HDFS, and has a size larger than 300 bytes: Example 2

find . -type f -a -user hdfs -a -size +300c

Example 3: Find the owner in the current directory hdfs or xml files ending with the ordinary

find . -type f -a \( -user hdfs -o -name '*.xml' \)
Published 34 original articles · won praise 7 · views 8160

Guess you like

Origin blog.csdn.net/ilo114/article/details/91419326