19, Text operation - file search find

Posted by 121212 on Mon, 06 Apr 2020 18:04:35 +0200

File lookup command find
find path search conditions [supplementary conditions]
Find the specified file in the directory

(1) Know the full name of the file and search

user1@SC02ZRC4KMD6N /etc % find passwd
passwd

(2) Find a path and whether the subpath contains files

user1@SC02ZRC4KMD6N / % find etc/ -name passwd
etc//pam.d/passwd
etc//passwd

(3) Using wildcards for fuzzy matching

user1@SC02ZRC4KMD6N / % find etc/ -name pass*

(4) Fuzzy matching using regular expressions

user1@SC02ZRC4KMD6N / % find etc/ -regex .*wd

(5) Find the specified file type

user1@SC02ZRC4KMD6N / % find etc/ -type f -regex .*wd

(6) Find files by time

user1@SC02ZRC4KMD6N / % find etc/ -ctime xx
user1@SC02ZRC4KMD6N / % stat test.txt
  File: 'test.txt'
  Size: 42          Blocks: 8          IO Block: 4096   regular file
Device: 804h/2052d  Inode: 59158038    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-06 12:53:33.659773937 +0800
Modify: 2020-04-06 12:53:28.651706463 +0800
Change: 2020-04-06 12:53:28.651706463 +0800
 Birth: -

atime refers to the access time of the file, i node time of the ctime file, and mtime refers to the time when the content of the file changes
(7) Find files, delete files

user1@SC02ZRC4KMD6N tmp % touch {1..9}.txt
user1@SC02ZRC4KMD6N tmp % ls -l
total 0
-rw-r--r--  1 user1  staff  0  4  6 15:11 1.txt
-rw-r--r--  1 user1  staff  0  4  6 15:11 2.txt
-rw-r--r--  1 user1  staff  0  4  6 15:11 3.txt
-rw-r--r--  1 user1  staff  0  4  6 15:11 4.txt
-rw-r--r--  1 user1  staff  0  4  6 15:11 5.txt
-rw-r--r--  1 user1  staff  0  4  6 15:11 6.txt
-rw-r--r--  1 user1  staff  0  4  6 15:11 7.txt
-rw-r--r--  1 user1  staff  0  4  6 15:11 8.txt
-rw-r--r--  1 user1  staff  0  4  6 15:11 9.txt

user1@SC02ZRC4KMD6N tmp % find *txt -exec rm -v {} \;
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
user1@SC02ZRC4KMD6N tmp % ls -l

Topics: Linux