find
Some common examples of some common parameters and some specific uses and considerations.
1. Using the name option.
The file name option is the most commonly used option of the find command, either alone or in combination with other options. You can use some kind of filename pattern to match files, remembering to enclose the filename pattern in quotation marks. Whatever the current path is, if you want to be in your own root directory HOME directory.
find ~ -name "*.log" -print
To find all ' *.log' files in the current directory and subdirectories, you can use.
find . -name "*.log" -print
To find files in the current directory and subdirectories whose names begin with an uppercase letter, you can use
find . -name "[A-Z]*" -print
To find files in the /etc directory with names starting with host, you can use.
find /etc -name "host*" -print
To find files in the $HOME directory, you can use.
find ~ -name "*" -print or find . -print
To overload the system, find all files starting from the root directory.
find / -name "*" -print
If you want to find files in the current directory whose names start with a single lowercase letter and end with 4 through 9 plus. log
Command.
find . -name "[a-z]*[4-9].log" -print
Output.
[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 "[a-z]*[4-9].log" -print
. /log2014.log
. /log2015.log
. /test4/log2014.log
[root@localhost test]#
2. With the perm option.
Use the -perm option to find files by file permissions mode. It is better to use octal permissions.
For example, if you are looking for a file in the current directory with the file permission bit 755, i.e. a file that can be read, written or executed by the owner and read or executed by other users, you can use.
[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 to express this: a horizontal bar - should be added in front of the octal numbers to indicate that they all match, e.g. -007 is equivalent to 777, -005 is equivalent to 555,
Command:
find . -perm -005
output:
[root@localhost test]# 11
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 . -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 directory.
If you want to ignore a directory when looking for a file because you know that it does not contain the file you are looking for, then you can use the -prune option to indicate the directory to ignore. Be careful when using the -prune option, because if you also use the -depth option, then the -prune option will be ignored by the find command. If you wish to find files in the test directory, but not in the test/test3 directory, you can use.
Command:
find test -path "test/test3" -prune -o -print
output:
[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. How to avoid a file directory when using find to find a file.
Example 1: Find all files in the test directory that are not in the test4 subdirectory
Command:
find test -path "test/test4" -prune -o -print
output:
[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:
find [-path … ] [expression]
Following the list of paths is the expression
-path “test” -prune -o -print is a shortened version of -path “test” -a -prune -o -print which evaluates in order, -a and -o are short-circuited, similar to the shell’s && and || if
-path “test” is true, then -prune , -prune returns true, and the logical expression is true; otherwise, no -prune, and the logical expression is false. If -path “test” -a -prune is false, then -print , -print returns true, or the logical expression is true; otherwise, -print is not evaluated, or the logical expression is true.
This expression combination special case can be written in pseudocode as.
if -path "test" then
-prune
else
-print
Example 2: Avoiding multiple folders:
Commands:
find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
Output:
[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]#
Explanation:
Round brackets indicate the union of expressions. \ indicates a reference, which means that the shell is instructed not to make a special interpretation of the characters that follow, but to leave it to the find command to interpret their meaning.
Example 3: Find a definite file, with options such as -name followed by -o
Commands:
find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print
Output.
[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. Using the user and nouser options.
To find files by file owner.
Example 1: Find a file in the $HOME directory with peida as the file owner
Command:
find ~ -user peida -print
Example 2: Find files in the /etc directory with peida as the file owner:
Command.
find /etc -user peida -print
Description:
Example 3: To find the files whose owner account has been deleted, you can use the -nouser option. Find all such files in the /home directory
Command:
find /home -nouser -print
Description:
This will find files whose owners do not have valid accounts in the /etc/passwd file. When using the -nouser option, it is not necessary to give the user name; the find command will do the work for you.
6. Using the group and nogroup options.
Just like the user and nouser options, the find command has the same options for the user group to which a file belongs. 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 belong to a valid user group, you can use the nogroup option. The following find command looks for such files from the root of the filesystem.
find / -nogroup-print
7. Find files by change time or access time, etc.
If you wish to find files by change time, you can use the mtime,atime or ctime options. If the system suddenly runs out of space, there is a good chance that the length of a file has grown rapidly in the meantime, so you can use the mtime option to find such a file.
Use the minus sign - to qualify files that have changed within n days of today, and the plus sign + to qualify files that have changed before n days of today.
To find files in the system root directory that have been changed within 5 days, you can use.
find / -mtime -5 -print
To find files in the /var/adm directory that have been changed more than 3 days ago, you can use
find /var/adm -mtime +3 -print
8. Find files that are newer or older than a file.
If you wish to find all files that have changed at a time newer than one file but older than another, you can use the -newer option.
Its general form is.
newest_file_name ! oldest_file_name
where ! is a logical non-sign.
Example 1: Find the file log2012.log newer than the change time but older than the file log2017.log
Commands:
find -newer log2012.log ! -newer log2017.log
output:
[root@localhost test]# 11
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
-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log
-rw-r--r-- 1 root root 0 11-16 14:43 log2017.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 -newer log2012.log ! -newer log2017.log
. /log2015.log
. /log2017.log
. /log2016.log
. /test3
[root@localhost test]#
Example 2: Find the file whose change time is newer than ** * * log2012.log * * * ** file
Commands:
find . -newer **log2012.log** -print
output:
[root@localhost test]# find -newer log2012.log
. /log2015.log
. /log2017.log
. /log2016.log
. /test3
[root@localhost test]#
9. Using the type option.
Example 1: Find all directories in the /etc directory
command.
find /etc -type d -print
Example 2: Find all types of files in the current directory except directories
Commands:
find . ! -type d -print
Example 3: Find all symbolic link files in the /etc directory
Command:
find /etc -type l -print
10. Use the size option.
You can find files by their length, which can be measured in either blocks or bytes. The expression for measuring the length of a file in bytes is N c; measuring the length of a file in blocks is simply expressed as a number.
When searching for files by file length, this file length in bytes is generally used when looking at the size of the file system, because it is then easier to convert using the block measurement.
Example 1: Find files in the current directory that are greater than 1 M bytes in length
Commands:
find . -size +1000000c -print
Example 2: Find a file in the /home/apache directory that is exactly 100 bytes long:
Command:
find /home/apache -size 100c -print
Example 3: Find files in the current directory that are more than 10 blocks long (one block equals 512 bytes)
Command:
find . -size +10 -print
11. Use the depth option.
When using the find command, you may want to match all the files first and then look in the subdirectories. Use the depth option to make the find command do this. One reason to do this is that when using the find command to back up a file system on a tape, you want to back up all the files first and the files in the subdirectories second.
**Example 1: The find command looks for a file named CON.FILE, starting at the root of the file system. **
Commands:
find / -name "CON.FILE" -depth -print
Description.
It will first match all the files and then go into the subdirectories to find them
12. Using the mount option.
To find files in the current filesystem (without going to other filesystems), you can use the mount option of the find command.
Example 1: Find files in the current file system with a file name ending in XC, starting from the current directory
Commands:
find . -name "*.XC" -mount -print
Reference: