2018年12月02日 16:35:07 mccok_MC 閱讀數 616shell
一、查找文件安全
find / -name ‘filename’bash
二、查找目錄spa
find / -name ‘path’ -type d操作系統
三、查找內容.net
find . | xargs grep -ri ‘content’
//find . | xargs grep -ril ‘content’ 只顯示文件名稱
---------------------------------------------------------------------------------------------------------------------get
經常使用的一些find命令的選項:io
find顧名思義就是查找,Linux下find命令提供至關多的查找條件,所以功能比較強大,能夠在衆多文件或目錄下查找你想要的任何文件或目錄。class
find 命令格式:find pathname -options [-print -exec -ok...]test
pathname表示find命令所查找的文件或目錄的路徑。例如:/home/tools/tongyan
-print: find命令將匹配的文件輸出到標準輸出。
-exec: find命令對匹配的文件執行該參數所給出的shell命令。相應命令的形式爲'command' { } \;,注意{ }和\;之間的空格。
-ok: 和-exec的做用相同,只不過以一種更爲安全的模式來執行該參數所給出的shell命令,在執行每個命令以前,都會給出提示,讓用戶來肯定是否執行。
find經常使用命令格式:
-name filename #查找名爲filename的文件
-perm #按執行權限來查找
-user username #按文件屬主來查找
-group groupname #按組來查找
-mtime -n +n #按文件更改時間來查找文件,-n指n天之內,+n指n天之前
-atime -n +n #按文件訪問時間來查GIN: 0px">
-ctime -n +n #按文件建立時間來查找文件,-n指n天之內,+n指n天之前
-nogroup #查無有效屬組的文件,即文件的屬組在/etc/groups中不存在
-nouser #查無有效屬主的文件,即文件的屬主在/etc/passwd中不存
-newer f1 !f2 # 找文件,-n指n天之內,+n指n天之前
-ctime -n +n #按文件建立時間來查找文件,-n指n天之內,+n指n天之前
-nogroup #查無有效屬組的文件,即文件的屬組在/etc/groups中不存在
-nouser #查無有效屬主的文件,即文件的屬主在/etc/passwd中不存
-newer f1 !f2 #查更改時間比f1新但比f2舊的文件
-type b/d/c/p/l/f #查是塊設備、目錄、字符設備、管道、符號連接、普通文件
-size n[c] #查長度爲n塊[或n字節]的文件
-depth #使查找在進入子目錄前先行查找完本目錄
-fstype #查更改時間比f1新但比f2舊的文件
-type b/d/c/p/l/f #查是塊設備、目錄、字符設備、管道、符號連接、普通文件
-size n[c] #查長度爲n塊[或n字節]的文件
-depth #使查找在進入子目錄前先行查找完本目錄
-fstype #查位於某一類型文件系統中的文件,這些文件系統類型一般可 在/etc/fstab中找到
-mount #查文件時不跨越文件系統mount點
-follow #若是遇到符號連接文件,就跟蹤連接所指的文件
-cpio #查位於某一類型文件系統中的文件,這些文件系統類型一般可 在/etc/fstab中找到
-mount #查文件時不跨越文件系統mount點
-follow #若是遇到符號連接文件,就跟蹤連接所指的文件
-cpio #對匹配的文件使用cpio命令,將他們備份到磁帶設備中
-prune #忽略某個目錄
下面具體說明
查找方式有不少種,好比按文件類型(-type),文件名(-name),文件屬主(-user),文件所屬組)(-group),文件權限(-perm),文件長度(-size),文件修改時間(-mtime)等查找。
文件類型包括:d 目錄文件。
l 符號連接(指向另外一個文件,相似於瘟下的快捷方式) 。
s 套接字文件。
b 塊設備文件,二進制文件。
c 字符設備文件。
p 命名管道文件。
- 普通文件,或更準確地說,不屬於以上幾種類型的文件。
文件權限包括:r 讀文件
w 寫文件
x 執行文件操做
-exec 參數後面跟的是command命令,它的終止是以;爲結束標誌的,因此這句命令後面的分號是不可缺乏的,考慮到各個系統中分號會有不一樣的意義,因此前面加反斜槓。
{} 花括號表明前面find查找出來的文件名。
使用find時,只要把想要的操做寫在一個文件裏,就能夠用exec來配合find查找,很方便的。在有些操做系統中只容許-exec選項執行諸如l s或ls -l這樣的命令。大多數用戶使用這一選項是爲了查找舊文件並刪除它們。建議在真正執行rm命令刪除文件以前,最好先用ls命令看一下,確認它們是所要刪除的文件。 exec選項後面跟隨着所要執行的命令或腳本,而後是一對兒{ },一個空格和一個\,最後是一個分號。爲了使用exec選項,必需要同時使用print選項。若是驗證一下find命令,會發現該命令只輸出從當前路徑起的相對路徑及文件名。
命令:
find . -type f -exec ls -l {} \;
輸出:
[root@localhost test]# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log
[root@localhost test]#
說明:
在shell中用任何方式刪除文件以前,應當先查看相應的文件,必定要當心!當使用諸如mv或rm命令時,可使用-exec選項的安全模式。它將在對每一個匹配到的文件進行操做以前提示你。
命令:
find . -type f -mtime +14 -exec rm {} \;
輸出:
[root@localhost test]# ll
總計 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
說明:
在shell中用任何方式刪除文件以前,應當先查看相應的文件,必定要當心!當使用諸如mv或rm命令時,可使用-exec選項的安全模式。它將在對每一個匹配到的文件進行操做以前提示你。
命令:
find . -name 「*.log」 -mtime +5 -ok rm {} \;
輸出:
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./log_link.log > ? y
< rm ... ./log2012.log > ? n
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
說明:
在上面的例子中, find命令在當前目錄中查找全部文件名以.log結尾、更改時間在5日以上的文件,並刪除它們,只不過在刪除以前先給出提示。 按y鍵刪除文件,按n鍵不刪除。
命令:
find /etc -name 「passwd*」 -exec grep 「root」 {} \;
輸出:
[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} \;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@localhost test]#
說明:
任何形式的命令均可以在-exec選項中使用。 在上面的例子中咱們使用grep命令。find命令首先匹配全部文件名爲「 passwd*」的文件,例如passwd、passwd.old、passwd.bak,而後執行grep命令看看在這些文件中是否存在一個root用戶。
命令:
find . -name 「*.log」 -exec mv {} .. \;
輸出:
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test3/
[root@localhost test3]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
命令:
find . -name 「*.log」 -exec cp {} test3 \;
輸出:
[root@localhost test3]# ll 總計 0[root@localhost test3]# cd .. [root@localhost test]# ll 總計 316 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log -rw-r--r-- 1 root root 61 11-12 22:44 log2013.log -rw-r--r-- 1 root root 0 11-12 22:25 log2014.log drwxr-xr-x 6 root root 4096 10-27 01:58 scf drwxrwxr-x 2 root root 4096 11-12 22:50 test3 drwxrwxr-x 2 root root 4096 11-12 19:32 test4 [root@localhost test]# find . -name "*.log" -exec cp {} test3 \; cp: 「./test3/log2014.log」 及 「test3/log2014.log」 爲同一文件 cp: 「./test3/log2013.log」 及 「test3/log2013.log」 爲同一文件 cp: 「./test3/log2012.log」 及 「test3/log2012.log」 爲同一文件 [root@localhost test]# cd test3 [root@localhost test3]# ll 總計 304 -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log -rw-r--r-- 1 root root 61 11-12 22:54 log2013.log -rw-r--r-- 1 root root 0 11-12 22:54 log2014.log [root@localhost test3]#