網站首頁 編程語言 正文
find命令詳解
1. find命令作用
find命令用來在指定目錄下查找文件。
2. find命令選項基本格式
find 目錄 -選項 動作[-print -exec -ok ...]
3. 常用選項:
-a:and 必須滿足兩個條件才顯示
-o:or 只要滿足一個條件就顯示
-name:按照文件名查找文件
-iname:按照文件名查找文件(忽略大小寫)
-type:根據文件類型進行搜索
-perm:按照文件權限來查找文件
-user 按照文件屬主來查找文件。
-group 按照文件所屬的組來查找文件。
-fprint 文件名:將匹配的文件輸出到文件。
-newer file1 ! newer file2 查找更改時間比文件file1新但比文件file2舊的文件
4. 常用動作:
-print 默認動作,將匹配的文件輸出到標準輸出
-exec 對匹配的文件執行該參數所給出的命令。相應命令的形式為 'command' { } \;,注意{ }和\;之間的空格。
-ok 和-exec的作用相同,只不過以一種更為安全的模式來執行該參數所給出的命令,在執行每一個命令之前,都會給出提示,讓用戶來確定是否執行。
-delete 將匹配到的文件刪除
|xargs 將匹配到的文件刪除 |xargs rm -rf
5. 根據文件名進行匹配
5.1 列出當前目錄及子目錄下所有文件和文件夾
命令:find .
命令:find .
[root@host-134 ~]# find .
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.mysql_history
./zuoye
./zuoye/lnmp.sh
./zuoye/system.sh
./nginx-1.18.0
./nginx-1.18.0/auto
./nginx-1.18.0/auto/cc
./nginx-1.18.0/auto/cc/acc
./nginx-1.18.0/auto/cc/bcc
5.2 在/home目錄下查找以.txt結尾的文件名
find /home/ -name "*.txt"
5.3 在/home目錄下查找以.txt結尾的文件名,但忽略大小寫
find /home -iname "*.txt"
5.4 查找 /home/ 下所有以.txt或.pdf結尾的文件
find /home/ -name "*.txt" -o -name "*.pdf"
5.5 查找 /home/ 下所有以a開頭和以.txt結尾的文件
find /home/ -name "*.txt" -a -name "a*"
5.6 搜索/home目錄下txt結尾的文件,并將輸出到指定文件中(re.txt)
[root@localhost home]# find /home/ -type f -name "*.txt" -fprint /tmp/re.txt
[root@localhost home]# cat /tmp/re.txt
/home/a.txt
/home/b.txt
6. 根據文件類型進行搜索
-type 類型參數
類型參數列
f 普通文件
l 符號連接(軟連接)
d 目錄
b 塊設備
s 套接字
6.1
[root@host-136 ~]# find /home/ -type f
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc
6.2
[root@host-136 ~]# find /usr/bin/ -type l
/usr/bin/bashbug
/usr/bin/lastb
/usr/bin/sh
/usr/bin/geqn
/usr/bin/ex
/usr/bin/lz4cat
/usr/bin/gneqn
/usr/bin/gnroff
6.3
[root@host-136 ~]# find /usr/local/ -type d
/usr/local/
/usr/local/bin
/usr/local/etc
/usr/local/games
/usr/local/include
/usr/local/lib
/usr/local/lib64
/usr/local/libexec
/usr/local/sbin
6.4
[root@host-134 ~]# find /dev/ -type b
/dev/dm-1
/dev/dm-0
/dev/sda2
/dev/sda1
/dev/sda
/dev/sr0
6.5
[root@localhost home]# find /var/lib/ -type s
/var/lib/mysql/mysql.sock
7. 基于目錄深度搜索
7.1 向下最大深度限制為3
[root@host-136 ~]# find /usr/local/ -maxdepth 3 -type d
/usr/local/
/usr/local/bin
/usr/local/etc
/usr/local/games
/usr/local/include
/usr/local/lib
/usr/local/lib64
/usr/local/libexec
/usr/local/sbin
/usr/local/share
/usr/local/share/applications
/usr/local/share/info
/usr/local/share/man
/usr/local/share/man/man1
/usr/local/share/man/man1x
7.2 搜索出深度距離當前目錄至少2個子目錄的所有文件
[root@host-136 ~]# find /usr/local/ -mindepth 2 -type f
/usr/local/sbin/mail.py
8. 根據文件時間戳進行搜索
8.1 UNIX/Linux文件系統每個文件都有三種時間戳:
訪問時間(-atime/天,-amin/分鐘):用戶最近一次訪問時間。
修改時間(-mtime/天,-mmin/分鐘):文件最后一次修改時間
變化時間(-ctime/天,-cmin/分鐘):文件數據元(例如權限等)最后一次修改時間。
8.2 stat 命令查看:
[root@host-136 ~]# stat /etc/passwd
File: ‘/etc/passwd'
Size: 950 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 33818061 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:passwd_file_t:s0
Access: 2021-01-06 09:17:36.122732027 +0800
Modify: 2021-01-06 09:17:36.114732083 +0800
Change: 2021-01-06 09:17:36.115732076 +0800
Birth: -
8.3 搜索最近七天內被訪問過的所有文件
[root@host-136 ~]# find /etc/ -type f -atime -7
/etc/fstab
/etc/crypttab
/etc/resolv.conf
/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
/etc/pki/ca-trust/ca-legacy.conf
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
8.4 搜索超過七天內(7天外)被訪問過的所有文件
[root@host-136 ~]# find /etc -type f -atime +7
/etc/sasl2/smtpd.conf
/etc/ethertypes
/etc/makedumpfile.conf.sample
/etc/postfix/access
/etc/postfix/canonical
/etc/postfix/generic
/etc/postfix/header_checks
/etc/postfix/relocated
/etc/postfix/transport
/etc/postfix/virtual
9. 根據文件大小進行匹配
用法
find . -type f -size 文件大小單元
文件大小單元:
b —— 塊(512字節)
c —— 字節
w —— 字(2字節)
k —— 千字節
M —— 兆字節
G —— 吉字節
9.1 搜索大于10KB的文件
[root@host-136 ~]# find /etc/ -type f -size +10k
/etc/ssh/moduli
/etc/postfix/access
/etc/postfix/canonical
/etc/postfix/header_checks
/etc/postfix/main.cf
/etc/postfix/transport
/etc/postfix/virtual
9.2 搜索小于10KB的文件
[root@host-136 ~]# find /etc/ -type f -size -10k
/etc/man_db.conf
/etc/sudo-ldap.conf
/etc/sudo.conf
/etc/sudoers
/etc/e2fsck.conf
/etc/mke2fs.conf
/etc/vconsole.conf
/etc/locale.conf
9.3 搜索等于10KB的文件
[root@host-136 ~]# find /etc/ -type f -size 10k
/etc/dbus-1/system.d/org.freedesktop.systemd1.conf
/etc/dbus-1/system.d/org.freedesktop.NetworkManager.conf
/etc/selinux/targeted/active/modules/100/accountsd/hll
/etc/selinux/targeted/active/modules/100/acct/hll
/etc/selinux/targeted/active/modules/100/aiccu/hll
/etc/selinux/targeted/active/modules/100/alsa/hll
/etc/selinux/targeted/active/modules/100/arpwatch/hll
9.4 搜索大于10G的日志文件,并刪除
[root@host-136 ~]# find /var/log -type f -name "*.log" -size +10G -delete
10. 根據文件權限/所有權進行匹配
找出指定目錄下權限不是644的txt文件
[root@host-136 ~]# find / -type f -name "*.txt" ! -perm 644
/usr/lib/firmware/ivtv-firmware-license-end-user.txt
/usr/lib/firmware/ivtv-firmware-license-oemihvisv.txt
/usr/share/licenses/shadow-utils-4.6/gpl-2.0.txt
/usr/share/licenses/shadow-utils-4.6/shadow-bsd.txt
找出/home目錄用戶frank擁有的所有文件
[root@host-136 ~]# find /home/ -type f -user frank
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc
找出/home目錄用戶組frank擁有的所有文件
[root@host-136 ~]# find /home/ -type f -group frank
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc
11. 借助-exec選項與其他命令結合使用
找出/tmp目錄下所有root的文件,并把所有權更改為用戶frank
find /tmp/ -type f -user root -exec chown frank {} \;
使用占位符{}來表示find到的文件名
找出家目錄下所有的.sh文件并刪除
[root@localhost home]# find $HOME -name "*.sh" -ok rm {} \;find #HOME -name "*.sh" -exec rm {} \;
< rm ... /root/install_lnmp.sh > ? y
-ok和-exec行為一樣,不過它會給出提示,是否執行相應的操作。
查找/home目錄下所有.txt文件并把他們拼接到all.txt文件中
find /home/ -type f -name "*.txt" -exec cat {} \;>all.txt
查找/home目錄下所有.txt文件并把他們復制到/opt/backup文件中
find /home/ -type f -name "*.txt" -exec cp {} /opt/backup/ \;
在/var/log目錄中查找更改時間在5日以前的文件并刪除它們:
find /var/log -type f -mtime +5 -exec rm {} \;find /var/log -type f -mtime +5 |xargs rm -rf
原文鏈接:https://www.cnblogs.com/linuxmysql/p/16398664.html
相關推薦
- 2022-12-08 c++只保留float型的小數點后兩位問題_C 語言
- 2022-07-13 RedisTemplate無法根據key 獲取值的問題
- 2023-01-01 C++?Boost.Signals2信號/槽概念_C 語言
- 2022-05-11 在?.NET?平臺使用?ReflectionDynamicObject?優化反射調用的代碼詳解_實用
- 2022-04-09 SpringBoot添加Cors跨域配置,解決No Access-Control-Allow-Ori
- 2023-04-18 Python之split函數的深入理解_python
- 2022-05-20 ElasticSearch 7.X系列之: 檢索性能優化實戰指南
- 2023-05-24 python實現對AES加密的視頻數據流解密的方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支