find命令
find 命令用於查找文件或目錄
語法格式:
find ./ -type f -name ‘文件名’
參數依次是:find命令,這里的./指的是當前路徑,-type是選擇文件類型,文件類型可以是 f 、d、 l,f是文件類型,d是目錄類型,l是鏈接類型等。-name 按照名稱查找,文件名稱要加引號。
-type #按照類型查找
find ./ -type f # 查找當前目錄 並且顯示隱藏文件 默認顯示目錄及目錄以下所有符合的文件
-name #按照名稱查找
find ./ -type f -name "1.txt" # 按照名稱查找
find ./ -type f -name "*.txt" # 匹配以.txt結尾的文件
find ./ -type f -name "1.t?t" # 通配符匹配 ?代表任意單個字符 大部分命令都支持
-size # 按照大小查找 k M G
find ./ -size +50M
find ./ -size +50M -size -100M # 並且關系 查找文件大於50 並且小於100
查找大於80並且小於90的文件
find ./ -type f -size +80M -size -90M
-maxdepth # 按照深度等級查找
find ./ -maxdepth 1 -size +50M # 查找1及目錄大於50M的文件
find ./ -maxdepth 2 -size +50M # 查找2及目錄大於50M的文件
find查找到的文件 如何cp rm move
cp 方法1: cp
[root@oldboyedu ~]# find ./ -type f -name "test.sh"|xargs -i cp {} /opt
方法2: cp
[root@oldboyedu ~]# find ./ -type f -name "test.sh" -exec cp {} /tmp \;
方法3: cp
[root@oldboyedu ~]# cp `find ./ -type f -name "test.sh"` /etc/
mv
方法1:mv
[root@oldboyedu ~]# find ./ -type f -name "test.sh"|xargs -i mv {} /opt
方法2:mv
[root@oldboyedu ~]# find /opt -size +50M -exec mv {} ./ \;
方法3:mv
[root@oldboyedu ~]# mv `find ./ -type f -name "test.sh"` /opt
在find中所有的別名失效
[root@oldboyedu ~]# find ./ -name "test.avi"|xargs ll
xargs: ll: No such file or directory
[root@oldboyedu ~]# find ./ -name "test.avi"|xargs ls -l
-rw-r--r--. 1 root root 5 Nov 5 10:35 ./test.avi
find中的rm 不會提示交互信息 慎用
[root@oldboyedu ~]# find ./ -name "test.avi"
./test.avi
[root@oldboyedu ~]# find ./ -name "test.avi"|xargs rm
[root@oldboyedu ~]# ll test.avi
ls: cannot access test.avi: No such file or directory