Linux--Searching for files or file contents (find, locate, grep, etc.)

Table of contents

1. General classification of file search in Linux

1、find

1.1. Use the man command to view

1.2. Detailed explanation of find

1.2.1. The complete writing format of find

1.2.2. Detailed explanation of common parameters of find

1.2.3. Find practical operation

2. Find it through the locate command

2.1. View our official documentation of locate

2.2. Practical operation of locate

3. Use grep to find the file based on the content of the search file.

3.1. grep usage

3.2. grep practical operation

1. General classification of file search in Linux

1、find

1.1. Use the man command to view

Use the man command (use the shortcut key q to exit) to see how we use it:

Detailed explanation of option parameters:

Official example:

1.2. Detailed explanation of find

1.2.1. The complete writing format of find

Complete usage:

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

Basic usage:

find  /  -option params 

1.2.2. Detailed explanation of common parameters of find

parameters of find command;

pathname: The directory path searched by the find command. For example, use . to represent the current directory and / to represent the system root directory.
-print: The find command outputs matching files to standard output.
-exec: The find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' { } \;, pay attention to the space between { } and \;.
-ok: has the same function as -exec, except that it executes the shell command given by this parameter in a safer mode. Before executing each command, a prompt will be given to allow the user to determine whether to execute it.

#-print Output the found file to the standard output
#-exec command {} \; Execute the command operation on the found file, there is a space between {} and \;
#-ok is the same as -exec, but in the operation Before asking the user whether to execute

Examples in actual use:

Actual parameters detailed explanation

-name filename #Search for a file named filename
-perm #Search by execution permission
-user username #Search by file owner
-group groupname #Search by group
-mtime -n +n #Search by file change time , -n refers to within n days, +n refers to n days ago
-atime -n +n #Check GIN according to file access time: 0px">

-ctime -n +n #Search files based on file creation time, -n refers to within n days, +n refers to n days ago

-nogroup #Check for files without a valid group, that is, the file's group does not exist in /etc/groups
-nouser #Check for files without a valid owner, that is, the file's owner does not exist in /etc/passwd
-newer f1 !f2 #Find files, -n means within n days, +n means n days ago 
-ctime -n +n #Find files according to file creation time, -n means within n days, +n means n days ago 
-nogroup #Check for files without a valid group, that is, the file's group does not exist in /etc/groups
-nouser #Check for files without a valid owner, that is, the file's owner does not exist in /etc/passwd
-newer f1! f2 #Check files whose change time is newer than f1 but older than f2
-type b/d/c/p/l/f #Check whether they are block devices, directories, character devices, pipes, symbolic links, ordinary files -size
n[c ] #Check files with a length of n blocks [or n bytes]
-depth #Enable the search to complete the search in this directory before entering the subdirectory
-fstype #Check files with a change time newer than f1 but older than f2
-type b/d/c/p/l/f #Check whether it is a block device, directory, character device, pipe, symbolic link, or ordinary file
-size n[c] #Check if the length is n blocks [or n bytes] File
-depth #Enable the search to search this directory before entering the subdirectory
-fstype #Check files located in a certain type of file system. These file system types can usually be found in /etc/fstab
-mount #Do not search for files Across file system mount points
-follow #If a symbolic link file is encountered, follow the file pointed to by the link
-cpio % #Check files located in a certain type of file system. These file system types can usually be found in /etc/fstab
-mount #Do not cross the file system mount point when searching for files
-follow #If a symbolic link file is encountered, follow the file pointed to by the link
-cpio #Use the cpio command for the matching files and back them up to the tape device
-prune # ignore a directory

1.2.3. Find practical operation

============================= Actual use =================== ===============

find -name april* Find files starting with April in the current directory
find -name april* fprint file Find files starting with April in the current directory and output the results to file
find -name ap* -o -name may * Find files starting with ap or may
find /mnt -name tom.txt -ftype vfat Find files named tom.txt and file system type vfat under /mnt
find /mnt -name t.txt ! -ftype vfat Find the file named tom.txt under /mnt and the file system type is not vfat
find /tmp -name wa* -type l Find the file named starting with wa and the type is a symbolic link under /tmp
find /home/hadoop /software -mtime -2 Check files that have been changed in the past two days under /home/hadoop/software
find /home/hadoop/software -atime -1 Check files that have been accessed within 1 day
find /home/hadoop/ software -mmin +60 Check files changed 60 minutes ago under /home/hadoop/software
find /home/hadoop/software -amin +30 Check files that were accessed in the last 30 minutes
find /home/hadoop/software -newer tmp.txt Check under /home/hadoop/software that the update time is more recent than tmp.txt Find /home/hadoop/software -anewer tmp.txt for files or directories.
Check /home/hadoop/software for files or directories whose access time is more recent than tmp.txt.
Find /home/hadoop/software -used -2 ​​to list After the file or directory has been modified, the files or directories that have been accessed within 2 days
find /home/hadoop/software -user cnscn List the files or directories belonging to user cnscn in the /home/hadoop/software directory
find /home/ hadoop/software -uid +501 List the files or directories with user identification codes greater than 501 in the /home/hadoop/software directory
find /home/hadoop/software -group cnscn List the group cnscn in /home/hadoop/software file or directory
find /home/hadoop/software -gid 501 List the files or directories with group ID 501 in /home/hadoop/software
find /home/hadoop/software -nouser List the files or directories in /home/hadoop/software that do not belong to local users Files or directories
find /home/hadoop/software -nogroup List the files or directories in /home/hadoop/software that do not belong to the local group
find /home/hadoop/software -name tmp.txt -maxdepth 4 List /home/hadoop The search depth of tmp.txt in /software is up to 3 levels
. find /home/hadoop/software -name tmp.txt -mindepth 3. Search from the 2nd level.
find /home/hadoop/software -empty to find files with a size of 0. Or find /home/hadoop/software -size +512k in an empty directory
to check for files larger than 512k.
Find /home/hadoop/software -size -512k to check for files smaller than 512k.
find /home/hadoop/software -links +2 Check for files or directories with hard links greater than 2
find /home/hadoop/software -perm 0700 Check for files or directories with permissions of 700

find / -amin -10 Find files accessed in the last 10 minutes in the system
find / -atime -2 Find files accessed in the last 48 hours in the system
find / -empty Find files or folders that are empty in the system
find / - group cat Find files belonging to groupcat in the system
find / -mmin -5 Find files modified in the last 5 minutes in the system
find / -mtime -1 Find files modified in the last 24 hours in the system
find / -nouser Find files belonging to the disabled user in the system
find / -user fred Find files belonging to the user FRED in the system

Find the Hadoop-3.3.0 file name in the current directory: hdfs or a file starting with hdfs:

find ./hadoop-3.3.0/ -name hdfs

find ./hadoop-3.3.0/ -name hdfs*

 result:

2. Find it through the locate command

2.1. View our official documentation of locate

locate is much faster and simpler to find files than our find, and has many fewer parameters. This is because our locate will not read the file system for the searched file or directory name. It actually refers to a database (written by updatedb ) that finds what the user is looking for and generates its output based on that search. Although this is very fast, one problem is that the file just created will not be searched. If you want the newly created file to be searched by the locate command, you can use the sudo updatedb command to update the database. Otherwise, you have to wait until the next day to search for the file, because the background database system defaults to updating once a day;

The official explanation is as follows:

2.2. Practical operation of locate

3. Use grep to find the file based on the content of the search file.

3.1. grep usage

In Linux, the `grep` command is a tool used to search and filter the contents of files. The grep command can only search within files, and it supports a variety of regular expressions for pattern matching and filtering. The grep filter searches a file for a specific pattern of characters and displays all lines containing that pattern. This pattern of searching in files is called a regular expression. The grep filter cannot be used without specifying a regular expression (the default is a basic regular expression).

Usage syntax:

        grep [option(s)] pattern [filename]

The filename in the grep command is optional. If there is no filename, grep waits for standard input. After entering a line, grep searches the line for a regular expression and displays the line if it contains that specific expression. Execution ends when the user indicates the end of input (Ctrl + d).

3.2. grep practical operation

Search the file contents in the /etc directory for 127.0.0.1: 

Find our files containing jdk in the current folder

Link: Linux--Understanding input and output device files, filters and pipes in Linux_Qingtian¥'s blog-CSDN blog

Guess you like

Origin blog.csdn.net/qq_57492774/article/details/132143370