Author’s brief introduction

Mr Lemon, senior operations engineer (self-described), SRE specialist (target), dreams of buying a Porsche by the age of 35. Like to study the bottom technology, think the bottom foundation is king. All new technologies are inseparable from the operating system (CPU, memory, disk), network, etc. Adhere to the input and output, record their own learning bit by bit, in the ordinary insist on moving forward, one day will meet a different yourself. Public account: Yunwewang (ID: Leeeee_Li).

One, foreword

I usually like to take notes, work for four or five years, notes on thousands of. Recently left the job, coupled with the bottleneck of the workplace, prepare to review and sort out the relevant notes, comb the knowledge point, there may be a series of blog output, from the basic to the advanced.

Find command

The find command is one of the most powerful tools in the Linux system administrator’s toolkit. You can use the find command to search for files and directories by permission, type, date, ownership, size, etc. It can also be used in combination with other tools such as grep or sed.

Grammar:

find [options] [path...]  [expression]Copy the code

Three, practice

1. Search by file name

find / -name access.log

Copy the code

2. Find files by extension

The find. -name "*.txt"Find all TXT files in the current directory

Copy the code

3. Search for files by permission

find . -perm 755 -print  Select * from '755' where '755' is located
find . -perm -007 -print Find files that all users can read, write, and execute

Copy the code

4. Find files by owner

find ~ -group lile -print    Select * from lile
find /home -group -print     Mysql > delete user name from /etc/passwd
find ~ -user lile -print     Select * from lile
find /home -nouser -print    Mysql > delete user name from /etc/passwd

Copy the code

5. Search for files by modification date

find . -mtime -3 -print  Find files that were changed within 3 days
find . -mtime -2 -print  Find files that were changed 2 days ago

Copy the code

6. Find files by type

find . -type d -print   Find all directories in the current directory
find . ! -type d -print Find all types of files in the current directory except directories

Copy the code

7. Find files by size

find . -size 100c -printFind. -size +1000000c - Find a 100-byte file in the current directoryprintFind. -size +10 - Find files larger than 1 MB in the current directoryprintFind files in the current directory with more than 10 blocks (1 block =512 bytes)Copy the code

8. Exclude a directory

find /shell -path "/shell/tt" -prune -o -print                 Find everything in /shell except the tt directory
find /shell -path "/shell/tt" -prune -o -name "*.txt" -print   Find TXT files in /shell except tt directory

Copy the code

9, directory and file search order

find /shell -depth -print

Dept: Process the subcontents of the directory first, and then the directory itself
# no dept: Process the directory itself first and then the subcontents of the directory

Copy the code

10. Regular lookup

find . -name "[A-Z]*" -print    Find files in the current directory and subdirectories whose names start with an uppercase letter
find . |xargs grep "YZS"        Find which specific files under a folder contain a particular field
find . -regextype "posix-egrep"  -regex ".*\.(cc|h)" |xargs cat |grep -v ^$ |wc -l

Copy the code

11. Find and act on the results

find . -type l -exec ls -l {} \;                                            Select * from soft link where file type = soft link
find . -type f -print | xargs file                                          # file classification
find / -name "core*" -print | xargs echo "">/tmp/core.log                   Find the memory dump file coredump and save it to/TMP /core.log
find . -name "*.txt" -print0 | xargs -0 rm -rf                              Delete it after you find it, and use it with caution
find / -path '/etc/ssl/certs' -prune -o -name *.pem | xargs -i cp {} ./pem  Find it and copy it
cat file.txt | xargs                                                        Convert multiple lines to single lines
cat file.txt | xargs -n 3                                                   X arguments are required for each execution
echo "splitXsplitXsplitXsplit" |xargs -d X                                  Use your own delimiter
echo "splitXsplitXsplitXsplit" |xargs -d X -n 2                             # use your own delimiter and specify the number of output lines
cat args |xargs -I {} bash cecho.sh p {} 1                                  # Read data from cat, replace each read

Copy the code

12. Match multiple files

find . \(  -name "*.txt" -o -name "*.pdf" \)

Copy the code

13, find exclude a directory

find / -path '/etc/ssl/certs' -prune -o -name *.pem #find find path -path 'exclude directory path' -prune -o....

Copy the code

Find time problem

-atime (access time), -ctime (creation time), -mtime (attribute modification time). The parameter is the following time n.

-atime +n Lists the files that were accessed before n days (excluding the files accessed within n days). -atime -n Lists files accessed within n days, including n days themselvesCopy the code


Example:

If the current time point is 15:00 sharp of 20171209, then the specific time range represented by the following queries

1: Creates a file

touch -a -d "The 2021-01-01 15:00" a.txt
touch -a -d "The 2021-01-02 15:00" b.txt
touch -a -d "The 2021-01-03 15:00" c.txt
touch -a -d "The 2021-01-04 15:00" d.txt
touch -a -d "The 2021-01-05 15:00" e.txt
touch -a -d "The 2021-01-06 15:00" f.txt
touch -a -d "The 2021-01-07 15:00" g.txt
touch -a -d "The 2021-01-08 15:00" h.txt

Copy the code

2: The results of the following three commands are as follows:

Find. -atime 2**** Find the files that were accessed in one day two days ago (between -72 hours to ~ -48 hours from now)



Find.-atime-2 Finds files accessed within 2 days from now (within 48 hours before now)





Find. -atime +2 Finds files accessed before 2 days, not including the previous 2 days (i.e., not including -2 and 2).

3. Graphic analysis