十九、文本操作——文件查找find

文件查找命令find
 find 路径 查找条件[补充条件]
在目录当中找到指定的文件

(1)知道文件的完整名称,进行查找

user1@SC02ZRC4KMD6N /etc % find passwd
passwd

(2) 查找某路径以及子路径是否包含文件

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

(3)使用通配符进行模糊匹配

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

(4)使用正则表达式进行模糊匹配

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

(5)查找指定的文件类型

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

(6)按照时间来查找文件

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指文件的访问时间,ctime文件的i节点时间,mtime指文件内容发生变化的时间
(7)查找文件,删除文件

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章