Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2399913
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类:

2011-04-19 10:40:04



 
find -name
  find在不同平台,有的平台自动显示结果,有的平台需要加-print才把结果显示出来



    find与grep 正好相反,只要不用*,必须精确匹配,差一个字母都不行

 精确匹配

 模糊匹配

$ find /etc -name hosts
/etc/hosts
/etc/inet/hosts

 $ find /etc -name “host*
/etc/hosts
/etc/inet/hosts
/etc/hostname.hme0

所以,要养成-name 加*的习惯


   find 是精确匹配,如果要模糊匹配带通配符,必须带双引号
不带双引号,系统报错
$ find . -name tech*
find: paths must precede expression_r_r
Usage: find [-H] [-L] [-P] [path...] [expression_r_r]    


   为什么一定要加引号?
因为一些 Linux 下的命令行工具,比如 find、grep、sed、awk、find 及 locate ,都使用自己的正则表达式(regular expression_r_rs)。
  正则表达式必须用“         ”括出来
例1:查找数字打头的文件
find . -name [1-9]* -print         
应该是:
find . -name ”[1-9]*” -print          
./346.bak
./124.bak
./583.bak
./311.bak~
例2:寻找t或i打头的文件
$ find . -name "[ti]*"           
./index.html
./tech-2.html
./images
./test
./test1


   find -type d        只(only)查找目录,不查找文件

$ find /etc -name “net*”
/etc/inet/netmasks                  文件
/etc/inet/networks                 文件
/etc/net                                   目录
/etc/netmasks                         文件
/etc/netconfig                       文件

 $ find /etc -name “net*” -type d    
/etc/net                                  目录
$

 


    find   -size +/-n[c]  查找匹配某尺寸的文件
必须带c, c表示byte(注意,是以byte为单位,不是以kbyte为单位)
$ls -l
-rw-r--r--   1 macg     other    1547363 12月 13日 13:53 autoconf-2.60-sol10-x86-local.gz
-rw-r--r--   1 macg     other     518256 12月 13日 13:53 automake-1.5-sol8-intel-local.gz
-rw-r--r--   1 macg     other      13824 12月 13日 13:53 example.xls
-rw-r--r--   1 macg     other         77 12月 13日 13:53 makefile
-rw-r--r--   1 macg     other         63 12月 13日 13:53 mysql-5.0.24a-win-src.tar.gz.md5
-rw-------   1 macg     other       9967 12月 13日 13:53 nohup.out
$find . -size +500000c       
./autoconf-2.60-sol10-x86-local.gz
./automake-1.5-sol8-intel-local.gz
$find . -size +600000c
./autoconf-2.60-sol10-x86-local.gz
$find . -size +70c
.
./autoconf-2.60-sol10-x86-local.gz
./automake-1.5-sol8-intel-local.gz
./example.xls
./makefile
./nohup.out


    find  -mtime         以天范围内外的修改
find -mtime -n     查找n天以内被修改的文件
find -mtime +n    查找n天以前被修改的文件
==n天以内未被修改

    find   -atime                     以天范围内外的访问
find 。。。 -atime -n      n天以内被访问过
find 。。。 -atime +n      n天以前被访问过
==n天以内未被访问过


$ls -l
-rw-r--r--   1 macg     other    1547363 12月 12日 13:37 autoconf-2.60-sol10-x86-local.gz
-rw-r--r--   1 macg     other     518256 12月 12日 13:37 automake-1.5-sol8-intel-local.gz
-rwxr-xr-x   1 macg     other       6396 12月 11日 12:43 cc
-rw-r--r--   1 macg     other         69 12月 11日 12:42 cc.c
-rw-r--r--   1 macg     other        820 12月 11日 12:43 cc.o
-rw-r--r--   1 macg     other      13824 12月 11日 14:29 example.xls
-rw-r--r--   1 macg     other         77 12月 11日 12:43 makefile
-rw-r--r--   1 macg     other         63 12月 12日 13:37 mysql-5.0.24a-win-src.tar.gz.md5
-rw-------   1 macg     other       9967 12月 13日 00:09 nohup.out
-rw-r--r--   1 macg     other         55 12月 12日 22:26 testsh
drwxr-xr-x   3 macg     other       1024 12月 11日 14:43 www    
$date
2006年12月13日 星期三 13时19分19秒 CST    
$find . -mtime -3 -name "cc*"       查找3天以内(算上今天3天)被修改的"cc*"  文件
./cc.c
./cc
./cc.o
./www/cc.html
./www/cc.html.bak
./www/images/ccieUseLogo.jpg
   
$find . -mtime +2 -name "cc*"    查找2天以前被修改/2天以内未被修改的


     find -exec :找到文件 ,并对找到的文件批处理执行具体指令
    find ... -exec command {} \;   指令格式        
·    exec选项必须通过\;来终止

$find . -name "cc*" -exec rm {}
find: 不完整语句

 $find . -name "cc*" -exec rm {} \;


·    \;和前面的语句中间必须有一个空格
$find . -name "cc*" -exec rm {} \;  
·   {} 就相当于通配符,{}代表前面的find结果
$find . -name "*.html" -exec cp {} .. \;


   基本find .. -exec command例子
例1: find并删除
$ls
autoconf-2.60-sol10-x86-local.gz  cc.7                              test3
automake-1.5-sol8-intel-local.gz  cc.8                              test4
cc.1                              example.xls                       test5
cc.2                              makefile                          test6
cc.3                              mysql-5.0.24a-win-src.tar.gz.md5  test7]
cc.4                              nohup.out                         testsh
cc.5                              test1
cc.6
    
$find . -name "cc*" -exec rm {} \;               

$ls
autoconf-2.60-sol10-x86-local.gz  nohup.out                         test5
automake-1.5-sol8-intel-local.gz  test1                             test6
example.xls                       test2                             test7]
makefile                          test3                             testsh
mysql-5.0.24a-win-src.tar.gz.md5  test4   

例2:用-exec把find 和ls -l结合起来,使find输出更美观
$find . -name "*.html" -exec ls -l {} \;
-rw-r--r--   1 macg     other          0 12月 13日 14:17 ./cc1.html
-rw-r--r--   1 macg     other          0 12月 13日 14:17 ./cc2.html
-rw-r--r--   1 macg     other          0 12月 13日 14:17 ./cc3.html
-rw-r--r--   1 macg     other          0 12月 13日 14:17 ./cc4.html
-rw-r--r--   1 macg     other          0 12月 13日 14:17 ./cc5.html
-rw-r--r--   1 macg     other          0 12月 13日 14:17 ./cc6.html
  

    {}相当于通配符,代表前边find的结果(所有found的文件的文件名)
$ls
*.txt                             cc3.html                          makefile
autoconf-2.60-sol10-x86-local.gz  cc4.html                          mysql-5.0.24a-win-src.tar.gz.md5
automake-1.5-sol8-intel-local.gz  cc5.html                          nohup.out
cc1.html                          cc6.html                          {}.txt
cc2.html                          example.xls   
$find . -name "*.html" -exec cp {} .. \;    把find的html 文件copy到上层目录
$cd ..
$ls
autoconf-2.60-sol10-x86-local.gz  cc2.html                          makefile
automake-1.5-sol8-intel-local.gz  cc3.html                          mysql-5.0.24a-win-src.tar.gz.md5
cc                                cc4.html                          nohup.out
cc.c                              cc5.html                          test
cc.o                              cc6.html                          testsh
cc1.html                          example.xls                       www   

find ... -ok command {} \;          
-ok是代替-exec的交互格式.这个选项用于要求来自用户的输入命令.如 rm -i
$ls
autoconf-2.60-sol10-x86-local.gz  nohup.out                         test5
automake-1.5-sol8-intel-local.gz  test1                             test6
example.xls                       test2                             test7]
makefile                          test3                             testsh
mysql-5.0.24a-win-src.tar.gz.md5  test4

$find . -name "test*" -ok rm {} \; 
< rm ... ./testsh >?   y
< rm ... ./test1 >?   y
< rm ... ./test2 >?   y
< rm ... ./test4 >?   y
< rm ... ./test3 >?   y
< rm ... ./test5 >?   y
< rm ... ./test6 >?   y
< rm ... ./test7] >?   y

$ls
autoconf-2.60-sol10-x86-local.gz  example.xls                       mysql-5.0.24a-win-src.tar.gz.md5
automake-1.5-sol8-intel-local.gz  makefile                          nohup.out
 

转载自:http://blog.sina.com.cn/s/blog_6151984a0100ei41.html
阅读(1867) | 评论(0) | 转发(0) |
0

上一篇:开机启动 service

下一篇:GDB 调试

给主人留下些什么吧!~~