歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 5分鐘學會find命令

5分鐘學會find命令

日期:2017/2/28 13:47:15   编辑:Linux教程

find的使用格式如下:

  $ find <指定目錄> <指定條件> <指定動作>

  - <指定目錄>: 所要搜索的目錄及其所有子目錄。默認為當前目錄。

  - <指定條件>: 所要搜索的文件的特征。

  - <指定動作>: 對搜索結果進行特定的處理。

如果什麼參數也不加,find默認搜索當前目錄及其子目錄,並且不過濾任何結果(也就是返回所有文件),將它們全都顯示在屏幕上。

1.當前目錄下查找文件

[[email protected] ~]# find . -name test.txt 
./findtest/test.txt

2.指定目錄下查找

[[email protected] ~]# find /root/ -name test.txt
/root/findtest/test.txt

3.忽略大小寫查找

[[email protected] ~]# find /root -iname test.txt
/root/findtest/test.txt
/root/findtest/TEST.txt

4.查找目錄

[[email protected] ~]# find / -type d -name test
/usr/lib64/python2.7/unittest/test
/usr/lib64/python2.7/test
/usr/src/kernels/3.10.0-229.14.1.el7.x86_64/include/config/test
/usr/src/kernels/3.10.0-229.14.1.el7.x86_64/lib/raid6/test

5.按名稱查找php文件

[[email protected] zabbix]# find . -type f -name events.php 
./events.php

6.在目錄中查找所有的php文件

[[email protected] zabbix]# find . -type f -name "*.php"
./graphs.php
./tr_logform.php
./authentication.php
./popup_httpstep.php
./image.php
..........

7.查找文件權限是777的

[[email protected] ~]# find . -type f -perm 0777 -print
./findtest/test.txt

8.查找文件權限不是777的

[[email protected] ~]# find . -type f ! -perm 0777 -print

9.查找644權限的SGID文件

[[email protected] ~]# find / -perm 2644

10.查找權限為551的粘著位文件

[[email protected] ~]# find / -perm 1551

11.查找所有SUID文件

[email protected] ~]# find / -perm /u=s

12.查找所有SGID文件

[[email protected] ~]# find / -perm /g+s

13.查找所有只讀文件

[[email protected] ~]# find / -perm /u=r

14.查找所有可執行文件

[[email protected] ~]# find / -perm /a=x

15.查找所有777文件,並改為644

反斜槓用來告訴find何時命令結束

[[email protected] ~]# find / -type f -perm 0777 -print -exec chmod 644 {} \;

16.查找所有777的目錄,並改為755

[[email protected] ~]# find / -type d -perm 777 -print -exec chmod 755 {} \;

17.查找並刪除某個文件

[[email protected] ~]# find . -type f -name "test.txt" -exec rm -f {} \;

18.查找並刪除多個文件

[[email protected] ~]# find . -type f -name "*.txt" -exec rm -f {} \;

19.查找所有的空文件

[[email protected] ~]# find /tmp -type f -empty

20.查找所有空目錄

[[email protected] ~]# find /tmp -type d -empty

21.查找所有隱藏文件

[[email protected] ~]# find /tmp -type f -name ".*"

22.根據用戶查找某個文件

[[email protected] ~]# find / -user root -name test.txt

23.根據用戶查找所有的文件

在/home下屬於某個用戶的所有文件

[[email protected] ~]# find /home -user zabbix
/home/zabbix
/home/zabbix/.bash_history
/home/zabbix/.config
/home/zabbix/.config/abrt
/home/zabbix/mysql-community-release-el7-5.noarch.rpm
/home/zabbix/.lesshst
/home/zabbix/.cache
/home/zabbix/.cache/abrt
/home/zabbix/.cache/abrt/lastnotification
/home/zabbix/.bash_logout
/home/zabbix/.viminfo
/home/zabbix/.mysql_history
/home/zabbix/.bashrc
/home/zabbix/.bash_profile

24./home目錄下查找某個組的所有文件

[[email protected] ~]# find /home -group developer

25./home目錄下忽略大小寫查找用戶zabbix的所有文件

[[email protected] ~]# find /home -user zabbix -iname "*.txt"

26.查找50天之內修改過的文件

[[email protected] ~]# find / -mtime 50

27.查找50天之內被存取過的文件

[[email protected] ~]# find / -atime 50

28.查找50-100天之內修改過的文件

[[email protected] ~]# find / -atime +50 -mtime -100

29.查找1個小時之內有變化的文件

[[email protected] ~]# find / -cmin -60

30.查找1個小時之內修改過的文件

[[email protected] ~]# find / -mmin -60

31.查找1個小時之內被存取過的文件

[[email protected] ~]# find / -amin -60

32.查找所有的50M大小的文件

[[email protected] ~]# find / -size 50M

33.查找50-100M之間的文件

[[email protected] ~]# find / -size +50M -size -100M

34.查找並刪除100M大小的文件

[[email protected] ~]# find / -size +100M -exec rm -rf {} \;

35.查找並刪除指定類型,指定大小的文件

[[email protected] ~]# find / -type f -name *.mp3 -size +10M -exec rm {} \;
Copyright © Linux教程網 All Rights Reserved