網(wǎng)站首頁 編程語言 正文
find命令詳解
1. find命令作用
find命令用來在指定目錄下查找文件。
2. find命令選項(xiàng)基本格式
find 目錄 -選項(xiàng) 動(dòng)作[-print -exec -ok ...]
3. 常用選項(xiàng):
-a:and 必須滿足兩個(gè)條件才顯示
-o:or 只要滿足一個(gè)條件就顯示
-name:按照文件名查找文件
-iname:按照文件名查找文件(忽略大小寫)
-type:根據(jù)文件類型進(jìn)行搜索
-perm:按照文件權(quán)限來查找文件
-user 按照文件屬主來查找文件。
-group 按照文件所屬的組來查找文件。
-fprint 文件名:將匹配的文件輸出到文件。
-newer file1 ! newer file2 查找更改時(shí)間比文件file1新但比文件file2舊的文件
4. 常用動(dòng)作:
-print 默認(rèn)動(dòng)作,將匹配的文件輸出到標(biāo)準(zhǔn)輸出
-exec 對匹配的文件執(zhí)行該參數(shù)所給出的命令。相應(yīng)命令的形式為 'command' { } \;,注意{ }和\;之間的空格。
-ok 和-exec的作用相同,只不過以一種更為安全的模式來執(zhí)行該參數(shù)所給出的命令,在執(zhí)行每一個(gè)命令之前,都會(huì)給出提示,讓用戶來確定是否執(zhí)行。
-delete 將匹配到的文件刪除
|xargs 將匹配到的文件刪除 |xargs rm -rf
5. 根據(jù)文件名進(jìn)行匹配
5.1 列出當(dāng)前目錄及子目錄下所有文件和文件夾
命令: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結(jié)尾的文件名
find /home/ -name "*.txt"
5.3 在/home目錄下查找以.txt結(jié)尾的文件名,但忽略大小寫
find /home -iname "*.txt"
5.4 查找 /home/ 下所有以.txt或.pdf結(jié)尾的文件
find /home/ -name "*.txt" -o -name "*.pdf"
5.5 查找 /home/ 下所有以a開頭和以.txt結(jié)尾的文件
find /home/ -name "*.txt" -a -name "a*"
5.6 搜索/home目錄下txt結(jié)尾的文件,并將輸出到指定文件中(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. 根據(jù)文件類型進(jìn)行搜索
-type 類型參數(shù)
類型參數(shù)列
f 普通文件
l 符號(hào)連接(軟連接)
d 目錄
b 塊設(shè)備
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 搜索出深度距離當(dāng)前目錄至少2個(gè)子目錄的所有文件
[root@host-136 ~]# find /usr/local/ -mindepth 2 -type f
/usr/local/sbin/mail.py
8. 根據(jù)文件時(shí)間戳進(jìn)行搜索
8.1 UNIX/Linux文件系統(tǒng)每個(gè)文件都有三種時(shí)間戳:
訪問時(shí)間(-atime/天,-amin/分鐘):用戶最近一次訪問時(shí)間。
修改時(shí)間(-mtime/天,-mmin/分鐘):文件最后一次修改時(shí)間
變化時(shí)間(-ctime/天,-cmin/分鐘):文件數(shù)據(jù)元(例如權(quán)限等)最后一次修改時(shí)間。
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 搜索最近七天內(nèi)被訪問過的所有文件
[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 搜索超過七天內(nèi)(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. 根據(jù)文件大小進(jìn)行匹配
用法
find . -type f -size 文件大小單元
文件大小單元:
b —— 塊(512字節(jié))
c —— 字節(jié)
w —— 字(2字節(jié))
k —— 千字節(jié)
M —— 兆字節(jié)
G —— 吉字節(jié)
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. 根據(jù)文件權(quán)限/所有權(quán)進(jìn)行匹配
找出指定目錄下權(quán)限不是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選項(xiàng)與其他命令結(jié)合使用
找出/tmp目錄下所有root的文件,并把所有權(quán)更改為用戶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行為一樣,不過它會(huì)給出提示,是否執(zhí)行相應(yīng)的操作。
查找/home目錄下所有.txt文件并把他們拼接到all.txt文件中
find /home/ -type f -name "*.txt" -exec cat {} \;>all.txt
查找/home目錄下所有.txt文件并把他們復(fù)制到/opt/backup文件中
find /home/ -type f -name "*.txt" -exec cp {} /opt/backup/ \;
在/var/log目錄中查找更改時(shí)間在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
相關(guān)推薦
- 2023-02-01 Python?AI編程助手AICodeHelper使用示例_python
- 2023-03-28 Go實(shí)現(xiàn)set類型的示例代碼_Golang
- 2022-06-02 jquery實(shí)現(xiàn)界面點(diǎn)擊按鈕彈出懸浮框_jquery
- 2022-09-20 Android畫圖實(shí)現(xiàn)MPAndroidchart折線圖示例詳解_Android
- 2023-03-05 so加載Linker跟NameSpace機(jī)制詳解_Android
- 2021-12-02 golang配制高性能sql.DB的使用_Golang
- 2022-07-18 Stack和Queue容器的系列操作( 詳解 )
- 2022-04-14 Qt自定義控件實(shí)現(xiàn)儀表盤_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支