Method of batch searching files and file contents in Linux system

Time:2022-1-25

For example, find a keyword in all PHP files under the current directory

Copy code

The code is as follows:

find ./ – Name “*. PHP” | xargs grep ‘keyword’

If the content to be searched contains special symbols, such as $and so on, grep needs to add parameters

Copy code

The code is as follows:

find ./ – Name “*. PHP” | xargs grep – f ‘keyword’

File name lookup

File name lookup method:

For example, if we know that a file contains the three letters SRM, it is possible to find all the files in the system that contain the three letters. Enter:

Copy code

The code is as follows:

find /etc -name ‘*srm*’

This command indicates that the Linux system will find all files containing SRM in the whole directory of / etc, such as absrmyz and TIBC SRM and other qualified files can be displayed. If you also know that this file starts with SRM, we can also omit the first asterisk. The command is as follows:

Copy code

The code is as follows:

find /etc -name ‘srm*’

Some other references

Find – name April * find files starting with April in the current directory
Find – name April * fprint file find the file starting with April in the current directory and output the result to file
Find – name AP * – O – name may * find files starting with AP or may
find /mnt -name tom. Txt – ftype VFAT find the name tom.txt under / MNT Txt and the file system type is VFAT
find /mnt -name t.txt ! – Ftype VFAT find the name Tom. Under / MNT Txt and the file system type is not VFAT
Find / tmp – name wa * – type l find a file with the name beginning with WA and the type of symbolic link under / tmp
Find / home – Mtime – 2 check the files changed in the last two days under / home
Find / home – atime – 1 check the files accessed in one day
Find / home – mmin + 60 check the files changed 60 minutes ago under / home
Find / home – Amin + 30 check the files accessed in the last 30 minutes