linux之find使用介紹

find使用介紹:nginx

1.查找到文件而且拷貝到其餘目錄下:sql

find ./ -name "biao-frame.sh" -exec cp {} /tmp \;

find ./ -name "[1-9].txt" | cpio -pdv /opt/

2.查找到文件而且mv移動文件到/opt目錄下:centos

[root@VM_82_178_centos ~]#  touch {1..5}.txt
[root@VM_82_178_centos ~]# ll *.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 1.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 2.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 3.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 4.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 5.txt
-rw-r--r-- 1 root root 17 Sep 27  2018 test.txt
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" |xargs -i mv {} /opt/
[root@VM_82_178_centos ~]# ll /opt/*.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/1.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/2.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/3.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/4.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/5.txt

-p參數會提示讓你確認是否執行後面的命令,y執行,n不執行less

[root@VM_82_178_centos opt]# find ./ -name "[1-9].txt" |xargs -p -i mv {} /root/
mv ./4.txt /root/ ?...y
mv ./5.txt /root/ ?...y
mv ./6.txt /root/ ?...y
mv ./3.txt /root/ ?...y
mv ./2.txt /root/ ?...y
mv ./1.txt /root/ ?...y

[root@VM_82_178_centos opt]# ll /root/*.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/1.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/2.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/3.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/4.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/5.txt
-rw-r--r-- 1 root root  4 Jul 31 13:54 /root/6.txt

*3.查找目錄下50天前的文件,而且排除掉xx.conf nginx_status.conf 文件和 /usr/local/nginx/conf/vhost/bak/ 下面的文件,而後刪除掉**ide

find /usr/local/nginx/conf/vhost -type f -mtime +50 -name "*" ! -path "/usr/local/nginx/conf/vhost/xx.conf" ! -path "/usr/local/nginx/conf/vhost/nginx_status.conf"  ! -path "/usr/local/nginx/conf/vhost/bak/*" -exec rm -f {} \;

4.查找到文件並批量修改文件內容:this

[root@VM_82_178_centos ~]# cat 1.sh 
12345
22222
33333
00000
[root@VM_82_178_centos ~]# find ./ -name "1.sh" -exec sed -i s/0/9/g {} \;
[root@VM_82_178_centos ~]# less 1.sh 
12345
22222
33333
99999

5.查大於512k的文件code

find /home -size +512k

6.查找大小爲0的文件而且刪除orm

[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f  -size 0  -exec rm -f {} \;
./3.txt
./2.txt
./1.txt

7.查到txt文件,放到一行,而後刪除it

find ./ -name "[1-9]".txt -type f -print0 |xargs -0 rm -f
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f
./4.txt
./5.txt
./3.txt
./2.txt
./1.txt

[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f -print
./4.txt
./5.txt
./3.txt
./2.txt
./1.txt
null

把查到的txt文件放到一行,文件名以前沒有空格
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f -print0
./4.txt./5.txt./3.txt./2.txt./1.txt[root@VM_82_178_centos ~]# 
把查到的txt文件放到一行,文件名之間空格隔開
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f -print0|xargs -0
./4.txt ./5.txt ./3.txt ./2.txt ./1.txt

把查到的txt文件放到一行,文件名空格隔開
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f|xargs
./4.txt ./5.txt ./3.txt ./2.txt ./1.txt

[root@VM_82_178_centos ~]#  find ./ -name "[1-9].txt" -type f |xargs -n1
./4.txt
./5.txt
./3.txt
./2.txt
./1.txt

查出txt文件,而且每行放5個文件
[root@VM_82_178_centos ~]#  find ./ -name "[1-9].txt" -type f |xargs -n5
./4.txt ./5.txt ./3.txt ./2.txt ./1.txt

查出txt文件,而且每行放2個文件
[root@VM_82_178_centos ~]#  find ./ -name "[1-9].txt" -type f|xargs -n2
./4.txt ./5.txt
./3.txt ./2.txt
./1.txt

把查到的txt文件放到一行,文件名空格隔開,而後刪除文件
 find ./ -name "[1-9].txt" -type f -print0|xargs -0 rm -f

8.查看文件格式io

[root@VM_82_178_centos ~]#  find ./ -type f -name "[0-9].txt" -exec file '{}' \;
./4.txt: empty
./5.txt: empty
./3.txt: empty
./2.txt: empty
./1.txt: ASCII text

9.用grep命令在當前目錄下的txt結尾的文件中搜索AA這個詞

[root@VM_82_178_centos ~]# echo AA >>5.txt
[root@VM_82_178_centos ~]#  find ./ -name "[1-9].txt" |xargs grep "AA"
./5.txt:AA

10.find查找出以txt結尾的文件,而後把查找出的txt結尾的文件名稱追加到一個文本中

[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -print | xargs echo "" >/tmp/core.log
[root@VM_82_178_centos ~]# less /tmp/core.log 
 ./4.txt ./5.txt ./6.txt ./3.txt ./2.txt ./1.txt

11. 參數-l1是一次處理一個,-t是處理以前打印出命令

[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt"  -print0 | xargs -0 -l1 -t -i mv {} /opt/
mv ./4.txt /opt/ 
mv ./5.txt /opt/ 
mv ./6.txt /opt/ 
mv ./3.txt /opt/ 
mv ./2.txt /opt/ 
mv ./1.txt /opt/ 
[root@VM_82_178_centos ~]# ll /opt/*.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/1.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/2.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/3.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/4.txt
-rw-r--r-- 1 root root 3 Jul 31 14:40 /opt/5.txt
-rw-r--r-- 1 root root 4 Jul 31 13:54 /opt/6.txt

[root@VM_82_178_centos opt]# find ./ -name "[1-9].txt"  -print0 | xargs -0 -l1 -t rm -f
rm -f ./4.txt 
rm -f ./5.txt 
rm -f ./6.txt 
rm -f ./3.txt 
rm -f ./2.txt 
rm -f ./1.txt 
[root@VM_82_178_centos opt]# ls
 rh  test_2018-11-27.sql

12.遍歷一次以txt結尾文件,將權限爲664文件和目錄列入/root/a.txt,將大於1M的文件列入/root/big.txt

find ./ -name "[0-9].txt" \( -perm  644 -fprintf /opt/a.txt '%#m %u %p\n' \) , \( -size +1M -fprintf /opt/big.txt '%-10s %p\n' \)

[root@VM_82_178_centos opt]# cat a.txt 
0644 root ./4.txt
0644 root ./5.txt
0644 root ./3.txt
0644 www ./2.txt
0644 root ./1.txt
[root@VM_82_178_centos opt]# cat big.txt 
1901997    ./1.txt

**參數-%m指文件的模式,%#m 此處的#號,是補0位**
you will see a difference between the actual value of the file's mode and the output of %m.   Normally you will  want  to
                     have a leading zero on this number, and to do this, you should use the # flag (as in, for example, `%#m').

**參數%u 指文件的用戶名,若是用戶沒有名稱,則爲數字用戶ID**
File's user name, or numeric user ID if the user has no name
**參數%p指文件名**
**參數\n爲換行符**

** 參數%s 指文件大小(以字節爲單位)**

13.find查找到文件而後刪除

find ./ -name nn108cpv-root -delete

相關文章
相關標籤/搜索