Find is a linux Command that lists the files in the file system that match the expression. You can specify a combination of different commands, such as the name, category, time, size, and permission of the file, only those that are exactly the same will be listed.
Find can find some files or directories in the current directory or even the entire file system;
Note: The find command is resource-consuming and cannot be found, for example, root/directory;
Function Description: searches for files or directories.
Find --> real-time search: slow and accurate matching
Command syntax:
Find [options] [search path] [Search Condition] [processing action]
Search Path: the current directory by default
Search Condition: by default, all files in the specified path are searched.
Action: Display by default
Let's take a look at the usage of find:
1. search criteria: the search criteria are case-sensitive.
-Name "file name": supports globbing.
*: Repeat 0 times or any number of previous characters
? : Single Character
[]: Character range
[^]: Exclude character range
Application instance:
1). view the number of passwd files in the/etc directory:
[Root @ xiaomazi ~] #
[Root @ xiaomazi ~] # Find/etc-name "passwd"
/Etc/pam. d/passwd
/Etc/passwd
[Root @ xiaomazi ~] #
2) view all files starting with passwd In the/etc directory:
[Root @ xiaomazi ~] # Find/etc-name "passwd *"
/Etc/pam. d/passwd
/Etc/passwd-
/Etc/passwd
[Root @ xiaomazi ~] #
3) view all files ending with passwd In the/etc directory:
[Root @ xiaomazi ~] # Find/etc-name "* passwd"
/Etc/pam. d/passwd
/Etc/passwd
/Etc/security/opasswd
[Root @ xiaomazi ~] #
2.-iname "file name": the search result is case insensitive.
Example:
1). Search for all files starting with passwd in/etc (Case Insensitive ):
[Root @ xiaomazi ~] # Touch/etc/Passwd --> Create A Test File
[Root @ xiaomazi ~] # Find/etc-iname "passwd *"
/Etc/pam. d/passwd
/Etc/passwd-
/Etc/passwd
/Etc/Passwd
[Root @ xiaomazi ~] # Rm-rf/etc/Passwd
Be sure not to create a folder in the/etc directory.
3. Search by owner group:
-User UserName: search by owner
-Group GroupName: search by group
Example:
1). The owner of the search file is the file of the Hadoop User:
[Root @ xiaomazi ~] # Su-hadoop
[Hadoop @ xiaomazi ~] $ Cp/etc/fstab/tmp/--> note the identity of the file to which the file belongs.
[Hadoop @ xiaomazi ~] $ Ls-l/tmp/
Total 28
-Rw-r -- 1 hadoop 921 Feb 26 fstab
[Hadoop @ xiaomazi ~] $
[Root @ xiaomazi ~] # Find/tmp-user hadoop
/Tmp/fstab
[Root @ xiaomazi ~] #
2). The file owner group is the file of the hadoop User:
[Root @ xiaomazi ~] # Find/tmp-group hadoop
/Tmp/fstab
[Root @ xiaomazi ~] #
4. Search by UID/GID:
-Uid
-Gid GID
If I delete a Fedora user without option-r, the user file will not be deleted. If the file owner is not in the group, what will happen? However, if you are not sure about the user name before, you can also use the uid;
Example:
1). Find all files whose uid is 501 in the/tmp directory:
[Root @ xiaomazi ~] # Useradd fedora --> create a user
[Root @ xiaomazi ~] # Su-fedora --> switch to fedora user
[Fedora @ xiaomazi ~] $ Cp/etc/inittab/tmp/--> copy an object as a fedora user
[Fedora @ xiaomazi ~] $ Ls-l/tmp/
Total 28
-Rw-r -- 1 hadoop 921 Feb 26 fstab
-Rw-r -- 1 fedora 884 Feb 26 inittab
[Fedora @ xiaomazi ~] $ Exit
Logout
[Root @ xiaomazi ~] # Userdel fedora --> simulate user Deletion
[Root @ xiaomazi ~] # Ls-l/tmp/
Total 28
-Rw-r -- 1 hadoop 921 Feb 26 fstab
-Rw-r -- 1 501 501 884 Feb 26 inittab --> note: the current owner belongs to the 501
[Root @ xiaomazi ~] # Find/tmp-user fedroa --> This cannot be found!
Find: 'fedroa' is not the name of a known user
[Root @ xiaomazi ~] # Find/tmp-uid 501 --> if we know the uid of fedora, we can search for the fedora file globally.
/Tmp/inittab
[Root @ xiaomazi ~] #
5. Search for files with no owner or group according to-nouser and-nogroup;
-Nouser: Find a file without a master, that is, the owner of the file does not exist in/etc/passwd.
-Nogroup: searches for files without groups, that is, the group to which the file belongs does not exist in/etc/group.
Example:
1) What should I do if I find files with no owner on the system now?
[Root @ xiaomazi ~] # Find/tmp-nouser
/Tmp/inittab
[Root @ xiaomazi ~] #
6. Combination conditions:
-A: Same, satisfying (not writable)
-O: Or, indicating that a request is satisfied.
-Not ,! : Non, reverse
Example:
1). Search for files with no owner or hadoop owner in the/tmp directory:
[Root @ xiaomazi ~] # Find/tmp-nouser-o-user hadoop
/Tmp/inittab
/Tmp/fstab
[Root @ xiaomazi ~] #
2). Find the file whose owner is hadoopand whose end is .txt in the/tmpdirectory:
[Hadoop @ xiaomazi ~] $ Whoami
Hadoop --> current user
[Hadoop @ xiaomazi ~] $ Cd/tmp/
[Hadoop @ xiaomazi tmp] $ touch word.doc hello.txt
[Hadoop @ xiaomazi tmp] $ ll --> create a file for testing.
Total 28
-Rw-r -- 1 hadoop 921Feb 2618: 14 fstab
-Rw-r -- 1 hadoop 0Feb 2618: 58hello.txt
-Rw-r -- 1501501884Feb 2618: 22 inittab
-Rw-r -- 1 hadoop 0Feb 2618: 58word.doc
Root User
[Root @ xiaomazi ~] # Find/tmp-user hadoop-name "*. txt"
/Tmp/hello.txt
[Root @ xiaomazi ~] #
3). Find the/tmpdirectory file whose owner is hadoopand does not end with .txt:
[Root @ xiaomazi ~] # Find/tmp-user hadoop-a-not-name "*. txt"
/Tmp/fstab
/Tmp/word.doc
[Root @ xiaomazi ~] #
4). Find the/tmpdirectory file whose owner is not hadoop, and it is not a file ending with .txt.
I. The-a option is used:
[Root @ xiaomazi tmp] # find/tmp-not-user hadoop-a-not-name "*. txt"
/Tmp
/Tmp/inittab
/Tmp/. ICE-unix
[Root @ xiaomazi tmp] #
II. The-o option is used:
[Root @ xiaomazi tmp] #
[Root @ xiaomazi tmp] # find/tmp-not \ (-user hadoop-o-name "*. txt "\)
/Tmp
/Tmp/inittab
/Tmp/. ICE-unix
[Root @ xiaomazi tmp] #
5). The owner of the/tmpdirectory is not hadoop, and the suffix is not ended with .txt:
[Root @ xiaomazi tmp] # find/tmp-not-user hadoop-o-not-name "*. txt"
/Tmp
/Tmp/inittab
/Tmp/fstab
/Tmp/word.doc
/Tmp/. ICE-unix
[Root @ xiaomazi tmp] #
6. Search by file type:
-Type: search by file type
F: Common File
D: Directory
B: Block devices
C: character device
L: Symbolic Link file
P: Named Pipe
S: Socket
Example:
1). display the/tmp directory:
[Root @ xiaomazi tmp] # find/tmp-type d
/Tmp
/Tmp/. ICE-unix
[Root @ xiaomazi tmp] #
2) Search for Common files in the/tmp directory:
[Root @ xiaomazi tmp] # find/tmp-type f
/Tmp/inittab
/Tmp/fstab
/Tmp/word.doc
/Tmp/hello.txt
[Root @ xiaomazi tmp] #