1. 程式人生 > 其它 >03:檔案查詢

03:檔案查詢

內容概要

1 檔案查詢

內容詳細

1 檔案查詢

# 查詢命令所在位置
​
which ls
...
​
ps: 一些命令的路徑都被配置到了環境變數PATH裡
​
# 根據檔案屬性查詢檔案(find)
​
前戲: 
    共用引數:(預設是並且的關係)
        -a : 並且
        -o : 或者
例:
    [root@python test]# find ./ -size -30M -o -size +50M
    ./
    ./txt
    ./txt2
​
1 按照檔名查詢
    -name"xxx" : 查詢xxx檔案
例:
    [root@python 
~]# find /root/ -name "txt*" -size +40M /root/txt2 /root/a/b/c/txt2 2 按照檔案的建立時間來查詢 +7 : 7天以前建立 -7 : 7天以內建立 7 : 正好第7天前建立 -ctime : 按照建立時間查詢 -mtime : 按照修改時間查詢 -atime : 按照訪問的時間查詢 例: # 3天以內建立 [root@python ~]# find /root/ -ctime -3 # 3天以前建立 [root@python
~]# find /root/ -ctime +3 3 按照檔案屬主、屬組查詢 -user : 屬主 -group : 屬組 例: [root@python ~]# find /root/ -size +10M -user test /root/txt [root@python ~]# find /root/ -size +10M -group test /root/txt 4 按照檔案的大小查詢 find 查詢的路徑 -size 大小 例: find /root/ -size +50M : 查詢大於50M的檔案 find /root/ -size 20M : 查詢等於20M的檔案
find /root/ -size -60M : 查詢小於60M的檔案 5 設定查詢最高的目錄層級(目錄層級引數必須放在第一位) -maxdepth 層級數 例: [root@python ~]# find /root/ -maxdepth 3 -a -size +40M /root/txt2 [root@python ~]# find /root/ -maxdepth 6 -a -size +40M /root/txt2 /root/a/b/c/txt2 6 按照檔案型別來查詢 # 按照目錄查詢 -type d 例: [root@python ~]# find /root/ -type d # 按照普通檔案來查詢 -type f 例: [root@python ~]# find /root/ -type f # 檢視裝置檔案 -type c 例: [root@python dev]# find /dev/ -type c ​ 7 按照許可權來查詢 ​ 前戲: rw-r--r--: rw- : 所屬使用者 (6) r-- : 所屬使用者組 (4) r-- : 其他使用者 (4) r(4) : 只讀 w(2) : 只寫 x(1) : 執行 chmod : 修改檔案許可權 chmod 755 檔案路徑 chown : 修改所屬使用者及使用者組 chown 使用者.使用者組 檔案路徑 -perm 許可權 : 按許可權級別查詢 例: [root@python test]# ll 總用量 122880 -r--r-----. 1 test user1 20971520 6月 16 17:33 txt -rw-r--r--. 1 root root 41943040 6月 16 17:33 txt1 -rwxr-xr-x. 1 root root 62914560 6月 16 17:33 txt2 [root@python test]# find ./ -perm 440 ./txt [root@python test]# find ./ -perm 644 ./txt1 ./txt2 ​ 8 處理查詢結果 # 1 直接跟命令(有些命令不能使用,不推薦) 例: [root@python test]# find ./ -perm 755 -ls 202362858 0 drwxr-xr-x 2 root root 41 6月 16 17:59 ./ 134514204 61440 -rwxr-xr-x 1 root root 62914560 6月 16 17:33 ./txt2 # 2 exec(推薦) # {} : 代表前面查詢出來的內容,相當於一個容器 find ./ -size +50M -exec cp {} /opt/ \; # 3 使用管道 # xargs是將前面命令執行的結果先用一個{}來儲存,然後用{}取出來處理 find ./ -size +50M | xargs -I {} cp {} /mnt/