指引网

当前位置: 主页 > 操作系统 > Linux >

Linux中查找指定类型文件以及删除例子

来源:网络 作者:佚名 点击: 时间:2017-05-13 00:20
[摘要]  本文章就为各位介绍查找文件指定文件或删除指定文件了,下文会从多方面给各位整理一些例子,希望这些文章对大家有帮助。

在Linux服务器上运行Weblogic服务器,每次需要关闭Weblogic服务器后,都会在domain下留下一些*.lok的文件,导致下次启动的时候因为这些文件无法启动,因此,需要删除产生的所有.lok文件。
手动一个一个的删除比较麻烦,需要domain的每个目录都进去看看有没有,有的话删除。
可以通过执行下面的命令查看有哪些这样的文件和批量删除它们。

find ./ -name "*.lok"   // 查找文件

find ./ -name "*.lok" |xargs rm -fr  // 查找文件并删除

1. find -name '*lck*' -exec rm {} \;

查找当前文件夹及其子文件夹下所有文件名中带有『lck』字符的文件并删除之。需要注意的是find -name *lck*,也就是没有加单引号,则只搜寻当前目录下的文件而不会搜索子文件夹内的文件。

顺便列一下find的相关使用

1. find   /  -name test  | xargs rm -rf      (这个命令可以查找test文件或者目录,并删除!)

2. 用下面的命令可以查找 /home下最近两天修改过的文件:find /home -type f -mtime -2

如果要把这些文件也删掉,那么可以:find /home -type f -mtime -2 -exec rm {} \;

-type f  查找文件
-type d 查找目录

-mtime -2 修改时间在2天内
-mtime +3 修改时间在3天前

-exec rm {} \;   将找到的文件 (假定找到文件的名字为 a.txt), 执行 rm a.txt 命令

find有很多参数,find一次查找多个指定文件或者排除某类文件,在 GREP 中匹配多个关键字的方法

(1)Linux下find一次查找多个指定文件:

查找a.html和b.html
find . -name "a.html"  -name "b.html" 

find . -regex '.*\.txt\|.*\.doc\|.*\.mp3'
find . -regex '.*\.txt\|.*\.doc\|.*\.mp3' 
./a.txt 
./a.doc 
./a.mp3 

(2)排除某些文件类型:

排除目录下所有以html结尾的文件:
find . -type f ! -name "*.html"   

find . -type f ! -name "*.html"      
./ge.bak.02.09 
./ge.html.changed.by.jack 
./a.txt 
./a.doc 
./a.mp3 

(3)排除多种文件类型的示例:

find . -type f ! -name "*.html" -type  f ! -name "*.php" -type  f ! -name "*.svn-base"  -type  f ! -name "*.js"  -type  f ! -name "*.gif"  -type  f ! -name "*.png"  -type  f ! -name "*.cpp"  -type  f ! -name "*.h"  -type  f ! -name "*.o"  -type  f ! -name "*.jpg"  -type  f ! -name "*.so"  -type  f ! -name "*.bak"  -type  f ! -name "*.log"  

(3)在 GREP 中匹配多个关键字的方法:

grep查找多个数字的文件:

-r 递归,-E:正则  -l:只显示文件名

root@116.255.139.240:~/a# grep -r -E '0341028|100081|10086|10001' * 
a.txt:100081 
b.txt:10086 
c/cc.txt:0341028 
c/cc.txt:100081 
c/cc.txt:10086 
c/cc.txt:10001 
c.txt:10001 
d.txt:0341028 

grep -r  -E -l '0341028|100081|10086|10001' *    
a.txt 
b.txt 
c/cc.txt 
c.txt 
d.txt 

多种类型文件示例:

find . -name "*.html" -o -name "*.js"|xargs grep -r "BusiTree"  

用Awk:

find . -name "*.php"|awk '{print "cat " $0 " |grep -H dbsys. www.111cn.net"}'|sh

------分隔线----------------------------
栏目列表
推荐内容