軟鏈接
[root@centos ~]# echo "This is my1" > my1
[root@centos ~]# echo "This is my2" > my2
[root@centos ~]# echo "This is my3" > my3
[root@centos ~]# ls
anaconda-ks.cfg cr my1 my2 my3 test
[root@centos ~]# cd test
[root@centos test]# ls
[root@centos test]# ln -s ../my1 m1 #相對路徑創建軟鏈接
[root@centos test]# ln -s /root/my2 m2 #絕對路徑創建軟鏈接
[root@centos test]# ln -s my3 m3 #此處創建的軟鏈接是錯誤的
[root@centos test]# ll -a
total 4
drwxr-xr-x. 2 root root 33 Mar 7 07:15 .
dr-xr-x---. 3 root root 4096 Mar 7 07:14 ..
lrwxrwxrwx. 1 root root 6 Mar 7 07:15 m1 -> ../my1
lrwxrwxrwx. 1 root root 9 Mar 7 07:15 m2 -> /root/my2
lrwxrwxrwx. 1 root root 3 Mar 7 07:15 m3 -> my3
[root@centos test]# cat m1
This is my1
[root@centos test]# cat m2
This is my2
[root@centos test]# cat m3
cat: m3: No such file or directory
find命令操作
find命令的用法:
find [OPTION]… [查找路徑] [查找條件] [處理動作]
查找路徑 | 指定具體目標路徑;默認爲當前目錄 |
---|---|
處理動作 | 對符合條件的文件做操作,默認輸出至屏幕 |
查找條件 | 指定的查找標準,可以文件名、大小、類型、權限等標準進行;默認爲找出指定路徑下的所有文件 |
我們在使用–help查看find命令都有哪些用法時僅僅依靠find --help
命令顯示出來的結果是不全面的,這時候我們就要使用man find
命令來更多的顯示用法,這樣得出來的顯示結果我們可以利用“/”後加我們要查詢的相應選項來進行匹配我們要查看的那部分的內容解釋。
指定搜索層級
-maxdepth | 最大搜索目錄深度 |
---|---|
-mindepth | 最小搜索目錄深度 |
對應數據庫上的語句就是:select * from stdio where maxdepth(>/<)深度
[root@centos test]# find / -maxdepth 3 -name cr
/root/cr
[root@centos test]# find / -maxdepth 4 -name cr
/root/cr
/usr/share/locale/cr
[root@centos test]# find / -mindepth 3 -name cr
/usr/share/locale/cr
[root@centos test]# find / -mindepth 2 -name cr
/root/cr
/usr/share/locale/cr
根據文件名和inode號來進行查找
-name “文件名稱” | 支持使用glob*, ?, [], [^] |
---|---|
-inum n | 按inode號查找 |
-samefile name | 相同inode號的文件 |
-links n | 鏈接數爲n的文件 |
-iname"文件名稱" | 不區分字母大小寫 |
-regex “PATTERN” | 以PATTERN匹配整個文件路徑字符串,而不僅僅是文件名稱 |
對應數據庫上的語句就是:select * from stdio where inode=“number”
根據屬組、屬主查找
-user USERNAME | 查找屬主爲指定用戶(UID)的文件 |
---|---|
-group GRPNAME | 查找屬組爲指定組(GID)的文件 |
-uidUserID | 查找屬主爲指定的UID號的文件 |
-gidGroupID | 查找屬組爲指定的GID號的文件 |
-nouser | 查找沒有屬主的文件 |
-nogroup | 查找沒有屬組的文件 |
對應數據庫上的語句就是:select * from stdio where user="username"
[first@centos test1]$ touch f{1..5}
[first@centos test1]$ ls
f1 f2 f3 f4 f5
[root@centos test1]# ls
f1 f2 f3 f4 f5
[root@centos test1]# touch f{6..10}
[root@centos test1]# ls
f1 f10 f2 f3 f4 f5 f6 f7 f8 f9
[root@centos test1]# find / -user first
/home/first/test1/f1
/home/first/test1/f2
/home/first/test1/f3
/home/first/test1/f4
/home/first/test1/f5
根據文件類型查找
-type TYPE:
f | 普通文件 |
---|---|
d | 目錄文件 |
l | 符號鏈接文件 |
s | 套接字文件 |
b | 塊設備文件 |
c | 字符設備文件 |
p | 管道文件 |
[root@centos ~]# find / -type f
[root@centos ~]# find / -type d
組合條件
組合條件:
與 | -a |
---|---|
或 | -o |
非 | -not, ! |
德·摩根定律:
(非A) 或(非B) = 非(A 且B)
(非A) 且(非B) = 非(A 或B)
示例:
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)
找出/tmp目錄下,屬主不是root,且文件名不以f開頭的文件
find /tmp\( -not -user root -a -not -name 'f*' \) -ls
find /tmp-not \( -user root -o -name 'f*' \) –ls
排除目錄
示例:查找/etc/下,除/etc/sane.d目錄的其它所有.conf後綴的文件
find /etc -path ‘/etc/sane.d’ -a -prune
-o -name “*.conf”
find /etc \(–path ‘/etc/sane.d’ –o –path ’/etc/fonts’ \)
-a prune –o name “*.conf”
根據文件大小來查找
-size [+/-]#UNIT | 常用單位:k, M, G,c(byte) |
---|---|
#UNIT: (#-1, #] | 如:6k 表示(5k,6k] |
-#UNIT:[0,#-1] | 如:-6k 表示[0,5k] |
+#UNIT:(#,∞) | 如:+6k 表示(6k,∞) |
對應數據庫上的語句就是:select * from stdio where size(>/<)數值
[root@centos ~]# find /root -size -5k| ll -a
total 68
dr-xr-x---. 3 root root 4096 Mar 7 07:49 .
dr-xr-xr-x. 17 root root 4096 Mar 6 22:00 ..
-rw-------. 1 root root 1132 Mar 6 22:01 anaconda-ks.cfg
-rw-------. 1 root root 1402 Mar 7 02:05 .bash_history
-rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r--r--. 1 root root 200 Mar 7 02:03 .bashrc
-rw-r--r--. 1 root root 280 Mar 7 01:42 cr
-rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc
-rw-r--r--. 2 root root 12 Mar 7 07:14 mmy1
-rw-r--r--. 2 root root 12 Mar 7 07:14 my1
-rw-r--r--. 1 root root 12 Mar 7 07:14 my2
-rw-r--r--. 1 root root 12 Mar 7 07:14 my3
-rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc
drwxr-xr-x. 2 root root 33 Mar 7 07:15 test
-rw-------. 1 root root 4622 Mar 7 02:36 .viminfo
-rw-r--r--. 1 root root 69 Mar 7 01:30 .vimrc
根據時間戳
以“天”爲單位;
-atime[+|-]#,
#: [#,#+1)
+#: [#+1,∞]
-#: [0,#)
-mtime
-ctime
以“分鐘”爲單位:
-amin
-mmin
-cmin
根據權限查找
-perm [/|-] MODE
MODE | 精確權限匹配 |
---|---|
/MODE | 任何一類(u,g,o)對象的權限中只要能一位匹配即可,或關係,+ 從centos7開始淘汰 |
-MODE | 每一類對象都必須同時擁有指定權限,與關係 |
0 | 表示不關注 |
find -perm 755會匹配權限模式恰好是755的文件
只要當任意人有寫權限時,find -perm +222就會匹配
只有當每個人都有寫權限時,find -perm -222纔會匹配
只有當其它人(other)有寫權限時,find -perm -002纔會匹配
處理動作
默認的處理動作,顯示至屏幕 | |
---|---|
-ls | 類似於對查找到的文件執行“ls -l”命令 |
-delete | 刪除查找到的文件 |
-fls file | 查找到的所有文件的長格式信息保存至指定文件中 |
-ok COMMAND {} ; | 對查找到的每個文件執行由COMMAND指定的命令,對於每個文件執行命令之前,都會交互式要求用戶確認 |
-exec COMMAND {} ; | 對查找到的每個文件執行由COMMAND指定的命令;{}: 用於引用查找到的文件名稱自身;find傳遞查找到的文件至後面指定的命令時,查找到所有符合條件的文件一次性傳遞給後面的命令 |
參數替換
由於很多命令不支持管道|來傳遞參數,而日常工作中有這個必要,所以就有了xargs命令
xargs用於產生某個命令的參數,xargs可以讀入stdin的數據,並且以空格符或回車符將stdin的數據分隔成爲arguments
注意:文件名或者是其他意義的名詞內含有空格符的情況
有些命令不能接受過多參數,命令執行可能會失敗,xargs可以解決
示例:
ls f* |xargs rm
find /sbin-perm +700 |ls -l 這個命令是錯誤的
find /sbin-perm +700 | xargs ls –l
find和xargs格式:find | xargs COMMAND
示例:
find -name “*.conf” -exec cp {} {}.orig \;
備份配置文件,添加.orig這個擴展名
find /tmp –ctime +3 –user joe –ok rm {} \;
提示刪除存在時間超過3天以上的joe的臨時文件
find~-perm-002 -execchmodo-w{} \;
在你的主目錄中尋找可被其它用戶寫入的文件
find /data –type f -perm 644 -name “*.sh” –exec chmod 755 {} \;
find /home –type d -ls