C语言再学习 -- Linux下find命令用法
參看:linux下find(文件查找)命令的用法總結
linux下查找文件的命令有兩個:locate 和 find
首先說一下locate:
這個命名是對其生成的數(shù)據庫進行遍歷(生成數(shù)據庫的命令:uodatedb),這一特性決定了用locate查找文件速度很快,但是locate命令只能對文件進行模糊匹配,在精度上來說差了點,簡單介紹下它的兩個選項:
#locat
-i ? ?查找文件的時候不區(qū)分大小寫,如: locate -i passwd
-n ? ?只顯示查找結果的前N行,如: locate -n 5 passwd
root@zslf:~# locate -n 5 passwd /etc/passwd /etc/passwd- /etc/cron.daily/passwd /etc/init/passwd.conf /etc/pam.d/chpasswd
下面重點說下find:
find在不指定查找目錄的情況下是對整個系統(tǒng)進行遍歷查找的。
使用格式: find [指定查找目錄] [查找規(guī)則] [查找完后執(zhí)行的action]
[指定查找目錄]
root@zslf:~# find /etc /tmp /root -name passwd /etc/cron.daily/passwd /etc/passwd /etc/pam.d/passwd
這里要注意的是目錄之間要用空格分開
[查找規(guī)則]
1)根據文件名查找
-name ? ?根據文件名查找 (精確查找)
-iname ? 根據文件名查找,但是不區(qū)分大小寫
root@zslf:~# find /etc -name "passwd*" /etc/init/passwd.conf /etc/cron.daily/passwd /etc/passwd- /etc/passwd /etc/pam.d/passwd
root@zslf:~# find /etc -name "passwd?" /etc/passwd-
root@zslf:~# find /home/test -name "[ab].sh" /home/test/a.sh /home/test/b.sh
文件名通配:
* ? ??表示通配任意的字符
?? ? ?表示通配任意的單個字符
[] ? ?表示通配括號里面的任意一個字符
2)根據文件所屬用戶和組來查找文件
-user ? ? ? ? 根據屬主來查找文件
-group ? ? 根據屬組來查找文件
root@zslf:/opt# ls -la 總用量 32 drwxr-xr-x 8 root root 4096 10月 21 11:28 . drwxr-xr-x 23 root root 4096 9月 20 13:18 .. drwxrwxr-x 3 zslf zslf 4096 9月 27 13:03 ethtool-4.6 drwxr-xr-x 4 root root 4096 9月 28 10:46 file drwxr-xr-x 3 root root 4096 9月 5 19:14 hisi-linux drwxr-xr-x 8 501 users 4096 9月 21 19:21 i2c-tools-3.0.1 drwxr-xr-x 9 root root 4096 10月 28 14:43 mpp drwxr-xr-x 20 root root 4096 9月 19 15:47 rootfs_uclibc參看:C語言再學習 -- 修改linux文件權限
root@zslf:/# find ./ -user zslf ./opt/ethtool-4.6 root@zslf:/# find ./ -group zslf ./opt/ethtool-4.6
-uid ? ? ? ? ?根據用戶查找
-group ? ? 根據用戶組查找
列出目錄內用戶的識別碼大于500的文件或目錄 root@zslf:/mnt/test# find ./ -uid +500 ./ ./hh 列出目錄內組id為500的文件或目錄 root@zslf:/mnt/test# find ./ -gid +500 ./ ./hh
root@zslf-virtual-machine:/mnt/test# ls -la 總用量 8 drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 . drwxr-xr-x 5 root root 4096 11月 24 17:20 .. -rw-r--r-- 1 zslf zslf 0 11月 25 09:42 a.sh -rw-r--r-- 1 zslf zslf 0 11月 25 09:42 b.sh -rw-r--r-- 1 zslf zslf 0 11月 25 09:42 c.sh -rw-r--r-- 1 root root 0 11月 25 09:45 f.sh -rw-r--r-- 1 root root 0 11月 25 09:45 g.sh
-a 連接兩個不同的條件 (兩個條件必須同時滿足)
root@zslf:/mnt/test# find ./ -name "*.sh" -a -user zslf ./a.sh ./c.sh ./b.sh
-o 連接兩個不同的條件 (兩個條件滿足其一即可)
root@zslf:/mnt/test# find ./ -name "*.sh" -o -user zslf ./ ./f.sh ./g.sh ./a.sh ./c.sh ./b.sh
-not 對條件取反的
root@zslf:/mnt/test# find ./ -not -user zslf ./f.sh ./g.sh
5)根據文件時間戳的相關屬性來查找文件
我們可以使用 stat 命令來查看一個文件的時間信息,如下:
root@zslf-virtual-machine:/mnt/test# stat ./文件:"./"大小:4096 塊:8 IO 塊:4096 目錄 設備:801h/2049d Inode:291601 硬鏈接:2 權限:(0755/drwxr-xr-x) Uid:( 1000/ zslf) Gid:( 1000/ zslf) 最近訪問:2016-11-25 09:45:24.699140785 +0800 最近更改:2016-11-25 09:45:22.255140690 +0800 最近改動:2016-11-25 09:45:22.255140690 +0800 創(chuàng)建時間:-
-atime、-mtime、-ctime、-amin、-mmin、-cmin
這里的?-atime、-mtime、-ctime 分別對應的是 “最近一次訪問時間”,“最近一次內容修改時間”,“最近一次屬性修改時間”,這里的atime的單位指的是“天”,amin 的單位是分鐘。
查找在五天內沒有訪問過的文件 root@zslfe:/mnt/test# find ./ -atime +5 查找在五天內訪問過的文件 root@zslf:/mnt/test# find ./ -atime -5 ./ ./f.sh ./g.sh ./a.sh ./c.sh ./b.sh
-type?
f ? ?普通文件
d ? 目錄文件
l ? ?鏈接文件
b ? 塊設備文件
c ? ?字符設備文件
p ? 管道文件
s ? ?socket文件
root@zslf-virtual-machine:/mnt/test# find ./ -type f ./f.sh ./g.sh ./a.sh ./c.sh ./b.sh
-size
#find ?./ ?-size ? 2M ? ? ? ? ? //查找在/tmp 目錄下等于2M的文件
#find ?./ ?-size ?+2M ? ? ? ? ? //查找在/tmp 目錄下大于2M的文件
#find ?./ ?-size ?-2M ? ? ? ? ? //查找在/tmp 目錄下小于2M的文件
root@zslf-virtual-machine:/mnt/test# find ./ -size -2M ./ ./f.sh ./g.sh ./a.sh ./c.sh ./b.sh
8)根據文件權限查找文件
-perm
#find ?./ ?-perm ? 755 ? ? ? ? //查找在/tmp目錄下權限是755的文件
#find ?./ ?-perm ?+222 ? ? ? //表示只要有一類用戶(屬主,屬組,其他)的匹配寫權限就行
#find ?./ ?-perm ?-222 ? ? ? ?//表示必須所有類別用戶都滿足有寫權限
9)-nouser 和 -nogroup
-nouser ? ??查找無有效屬主的文件
-nogroup ?查找無有效所屬組的文件
#find ?./ ?-nogroup –a –nouser ? ? ??
在整個系統(tǒng)中查找既沒有屬主又沒有屬組的文件(這樣的文件通常是很危險的,作為系統(tǒng)工程師的我們應該及時清除掉)
[查找完執(zhí)行的action]
-print ? ? ? ? ? ? ? ? ? ? ? ? ? ? 默認情況下的動作
-ls ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?查找到后用 ls 顯示出來
-ok [commend] ? ? ? ? ? ?查找后執(zhí)行命令的時候詢問用戶時候要執(zhí)行
-exec [commend] ? ? ? ?查找后執(zhí)行命令的時候不詢問用戶,直接執(zhí)行
root@zslf:/mnt/test# ls -la 總用量 8 drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 . drwxr-xr-x 5 root root 4096 11月 24 17:20 .. -rw-r--r-- 1 zslf zslf 0 11月 25 09:42 a.sh -rw-r--r-- 1 zslf zslf 0 11月 25 09:42 b.sh -rw-r--r-- 1 zslf zslf 0 11月 25 09:42 c.sh -rw-r--r-- 1 root root 0 11月 25 09:45 f.sh -rw-r--r-- 1 root root 0 11月 25 09:45 g.sh 更改權限 root@zslf-virtual-machine:/mnt/test# find ./ -name "*.sh" -exec chmod 777 {} \; root@zslf-virtual-machine:/mnt/test# ls -la 總用量 8 drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 . drwxr-xr-x 5 root root 4096 11月 24 17:20 .. -rwxrwxrwx 1 zslf zslf 0 11月 25 09:42 a.sh -rwxrwxrwx 1 zslf zslf 0 11月 25 09:42 b.sh -rwxrwxrwx 1 zslf zslf 0 11月 25 09:42 c.sh -rwxrwxrwx 1 root root 0 11月 25 09:45 f.sh -rwxrwxrwx 1 root root 0 11月 25 09:45 g.sh 這里注意 ?{ } 的使用:替代查找到的文件。
刪除查找到的超過30天沒有訪問過的文件 root@zslf:/mnt/test# find ./ -atime +30 -exec rm -rf {} \;
也可以使用xargs來對查找到的文件進一步操作:
參看:C語言再學習 -- Xargs用法詳解
修改權限 root@zslf:/mnt/test# find ./ -name "*.old" | xargs chmod 700 root@zslf:/mnt/test# ls -la 總用量 8 drwxr-xr-x 2 zslf zslf 4096 11月 25 10:17 . drwxr-xr-x 5 root root 4096 11月 24 17:20 .. -rwxrwxrwx 1 zslf zslf 0 11月 25 09:42 a.sh -rwx------ 1 root root 0 11月 25 10:17 a.sh.old -rwxrwxrwx 1 zslf zslf 0 11月 25 09:42 b.sh -rwx------ 1 root root 0 11月 25 10:17 b.sh.old -rwxrwxrwx 1 zslf zslf 0 11月 25 09:42 c.sh -rwx------ 1 root root 0 11月 25 10:17 c.sh.old -rwxrwxrwx 1 root root 0 11月 25 09:45 f.sh -rwx------ 1 root root 0 11月 25 10:17 f.sh.old -rwxrwxrwx 1 root root 0 11月 25 09:45 g.sh -rwx------ 1 root root 0 11月 25 10:17 g.sh.old
總結
以上是生活随笔為你收集整理的C语言再学习 -- Linux下find命令用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springcloud服务注册中心eur
- 下一篇: 互联网晚报 | 3月26日 星期六 |