MOVE 开发模式

MVC模式在编程中的应用,是一个很了不起的主意。“数据模型(Model)”可以封装与应用程序的业务逻辑相关的数据及对数据的处理方法;“视图层(View)”能够实现数据有目的的显示;“控制器(Controller)”能够在不同层面间起到组织作用,对应用程序的流程进行控制。

不过,可能你在使用这种三层架构模式的过程中会逐渐迷惑。因为有很多代码你不知道把它放哪,就只好把它放到控制层,最后发现在控制层中塞了太多的代码。

LinkedIn的软件工程师Conrad Irwin也遇到同样的问题,于是他开始使用另一种模式:MOVE,即Models(模型)、Operations(操作)、Views(视图)、Events(事件)。

近日Conrad Irwin在个人博客上分享了关于这种模式的一些观点。

概述

Irwin结合上图对MOVE模式先作了简单定义:

  • Models,封装该应用程序中知道的一切;
  • Operations,封装该应用程序要做的一切;
  • Views,帮助用户与应用程序完成交互;
  • Events,用于安全地连接所有这些组件。

为了避免意大利面条式的代码,图中标示出了对哪种类型的对象进行操作是允许的。例如,视图允许监听由模型产生的事件;操作允许修改模型,但模型不应涉及视图或操作。

Models(模型)

这里以一个“User”对象为原型,它至少应用有一个Email地址,也可能有用户名和电话号码。

在一个MOVE模式的Models中只包装知识。这意味着除了Get和Set功能,它们可以包含检查用户密码是否正确这样的方法,但不会包含把密码保存到数据库或传递给外部API这样的功能,因为后面这些工作将由Operations来完成。

Operations(操作)

对应用程序来说,一个常见的操作是用户登陆。这实际上是由两个子操作组成:首先从用户那里获得邮件地址和密码,然后从数据库载入“user”模型并检查密码是否匹配。

Operations是MOVE模式中的行动者。它负责修改模型,在正确的时间显示正确的视图,以及响应由用户交互引发的事件。在一个分解良好的应用程序中,每个子操作都可以独立运行。

采用这种方式的操作有一点很令人振奋,即程序启动后,整个应用本身就可以被当作一个Operations。它会根据需要生成尽可能多的子操作,其中每个子操作都并行地运行。当所有子操作都完成时,程序也便退出。

Views(视图)

登陆页面即是一个视图,它负责显示一些文本框给用户。当用户点击“登陆”按钮时,视图将产生一个“loginAttempt”事件,其中包含用户输入的用户名和密码。

用户能够看到的内容,以及能感受到的互动都由视图提供支持。它们会以一种用户能理解的形式呈现应用反馈,同时还能将简单的用户交互转换成有意义的事件。更重要的是视图不会直接改变模型,它们只是向Operations发起事件,然后通过监听等待由模型发起的事件。

Events(事件)

当用户登陆时,视图会发起“loginAttempt”事件。在登陆操作完成后,“currentUser”模型会发起一个事件通知应用登陆状态已改变。

事件监听让MOVE(及MVC)实现控制反转,允许模型更新视图。这是一种强大的抽象技巧,允许组件互不干扰地耦合在一起。

为什么是现在?

当然,Conrad Irwin并不想被人认为自己是在暗示MVC模式很差,这种大型应用程序架构在过去的几十年里确实非常成功。不过几十年后的今天,新的编程技术已经变得越来越流行,所以你也会在使用过程中逐渐产生一些疑惑。

MVC模式确实很了不起,但它毕竟是几十年前为老的技术而设计。MOVE模式是在其基础上的升级,让你可以更好地利用当前已有的新工具。

monitoring your javaEE application : javamelody

Screenshots

The goal of JavaMelody is to monitor Java or Java EE application servers in QA and production environments. It is not a tool to simulate requests from users, it is a tool to measure and calculate statistics on real operation of an application depending on the usage of the application by users.

JavaMelody is opensource (LGPL) and production ready: in production in an application of 25 person years. JavaMelody is easy to integrate in most applications and is lightweight (no profiling and no database).

JavaMelody is mainly based on statistics of requests and on evolution charts.

It allows to improve applications in QA and production and helps to:

  • give facts about the average response times and number of executions
  • make decisions when trends are bad, before problems become too serious
  • optimize based on the more limiting response times
  • find the root causes of response times
  • verify the real improvement after optimizations

It includes summary charts showing the evolution over time of the following indicators:

  • Number of executions, mean execution times and percentage of errors of http requests, sql requests, jsp pages or methods of business façades (if EJB3, Spring or Guice)
  • Java memory
  • Java CPU
  • Number of user sessions
  • Number of jdbc connections

These charts can be viewed on the current day, week, month, year or custom period.

JavaMelody includes statistics of predefined counters (currently http requests, sql requests, jsp pages and methods of business façades if EJB3, Spring or Guice) with, for each counter :

  • A summary indicating the overall number of executions, the average execution time, the cpu time and the percentage of errors.
  • And the percentage of time spent in the requests for which the average time exceeds a configurable threshold.
  • And the complete list of requests, aggregated without dynamic parameters with, for each, the number of executions, the mean execution time, the mean cpu time, the percentage of errors and an evolution chart of execution time over time.
  • Furthermore, each http request indicates the size of the flow response, the mean number of sql executions and the mean sql time.

It also includes statistics on http errors, on warnings and errors in logs, on data caches if ehcache and on batch jobs if quartz.

An optional and independent collect server may be used if necessary to unload the application of storage management, and of report generation and to centralize the data of clustered applications or of several applications.

 

Site : http://code.google.com/p/javamelody/

Comparing HTML5 Mobile Web Framework

Comparing HTML5 Mobile Web Framework

It’s been an exciting year for the mobile Web. Adoption of HTML5 and CSS3, improved performance in mobile browsers, and an explosion of mobile app frameworks mean it’s more feasible than ever to create rich, interactive Web experiences for mobile devices. Using a wrapper like PhoneGap, you can distribute them via the native app stores for iPhone, iPad, and Android —targeting multiple platforms with a single code-base.

Mobile Web developers have a plethora of frameworks to do the heavy lifting for them: animated transitions, toolbars, buttons, list views, even offline storage. Most of these are new and the landscape is shifting rapidly. I have gone through many of the Mobile web frameworks and compared and analyzed them. And here is something what I found:

jQTouch is easy to use and relatively well-documented. It’s featured in the excellentBuilding iPhone Apps with HTML, CSS, and JavaScript. jQTouch takes a progressive-enhancement approach, building an iPhone-like experience on top of your appropriately-constructed HTML. It’s simple, providing a basic set of widgets and animations and just enough programmatic control to permit more dynamic behavior.

But even in my simple test app there were performance issues. Page transitions can be jumpy or missing, and there are periodic delays in responding to tap events. And while the project is technically active, the original author has moved on and development seems to have slowed.

jQTouch is available under the permissive MIT License, one of my favorite open source licenses.

jQuery Mobile was the new kid on the block. Announced in August 2010, it’s quickly progressed to a very functional Alpha 2 and now on February 28 it comes with a JQM 1.1.0. It takes a similar – but more standards-compliant – approach to jQTouch and feels very much like that framework’s successor, with a broader array of UI controls and styles.

jQuery Mobile’s performance is variable (though better than that of jQTouch), particularly in responding to tap events rendering animations. It also lacks a small number of key programmatic hooks that would permit easy creation of more dynamic apps. For instance, there’s an event that triggers when a page is about to load (i.e. slide into view) but no way to tell the associated handler code what UI element resulted in the page switch, or to pass additional information to that handler. I was able to create workarounds but hope that future versions will take a cue from jQTouch and build out this functionality a little more.

In the 2011 end and the starting of 2012 the JQM catched the eye of lot of web geeks. And it is the most improving framework in the Mobile Web industry.

jQuery Mobile is available under either the MIT or the GPL2 license.

Sencha Touch is the mobile counterpart to the Ext JS framework. Its approach differs significantly from jQTouch and jQuery Mobile: instead of enhancing preexisting HTML, it generates its own DOM based on objects created in JavaScript. As such, working with Sencha feels a little less “webby” and a little more like building apps in other technologies like Java or Flex. (It’s also a bit more like YUI than like jQuery.) I personally prefer the progressive enhancement approach, but it really is a matter of preference.

Sencha is far more extensive than its competitors, with a vast array of UI components, explicit iPad support, storage and data binding facilities using JSON and HTML5 offline storage, and more. (It’s very cool to manipulate app data in one of Sencha’s data structures and watch the corresponding list update in real time.) It’s also the only Web framework I’ve seen with built-in support for objects that stay put (like a toolbar) while others scroll (like a list).

For all that apparent extra weight, Sencha performed noticeably better and more reliably than either jQTouch or jQuery Mobile in my tests, with the exception of initial load time.

When working with a library or framework, it’s usually counterproductive to “fight the framework” and do things your own way. Given how extensive Sencha Touch is, that means your app will probably end up doing just about everything the Sencha way. I’d originally usedWebKit’s built-in SQLite database for offline storage but ultimately eliminated both complexity and bugs by moving that functionality into Sencha’s data stores.

The documentation, while extensive, has odd holes. Between those and the sheer size of the framework, I spent a lot of time fighting bugs that were difficult to trace and to understand. Responses to my questions in the developer forums were more frequent and helpful than with the other frameworks, but still ultimately insufficient. Sencha provides paid support starting at $300/year; I strongly considered purchasing it but oddly, their response to my sales support inquiries was incredibly underwhelming given my interest in sending them money.

Sencha Touch is available under the GPL3; under a somewhat confusing set of exceptions to the GPL that seem similar to the LGPL; or under a free commercial license.

Much like Sencha Touch, Appcelerator’s Titanium Mobile allows you to write apps using a JavaScript API. But unlike Sencha, it compiles most of your code into a native iPhone or Android app. That means it isn’t really a Web framework, but a compatibility layer or compiler. (Note that its cousin Titanium Desktop is Web-based, allowing you to write HTML/JS applications that run inside a native wrapper on the desktop.)

So Titanium allows Web developers to produce high-performance, easily skinnable native apps using JavaScript and a little XML, i.e. without learning Objective-C or Cocoa Touch. My simple test app blew away the true Web frameworks in terms of performance, and wasn’t much harder to put together.

But that advantage is also its greatest disadvantage: you can only target the platforms Titanium supports, and you’re tied to their developer tools. As if to prove this point, my test app quickly got into a state where it wouldn’t launch on the iPhone. Titanium doesn’t include much of a debugger; Titanium projects can’t be run and debugged in XCode; and it ran fine in the simulator, leaving me with no real way to attack the problem.

Rebuilding my app on three of these four frameworks was tedious but educational. I like jQTouch but have trouble believing it will evolve much from here. I’m rooting for jQuery Mobile for its simplicity and its very Web-centric approach to development…but it lacks a few key features and doesn’t perform as well as Sencha Touch.

It’s unfair to compare an Alpha 2 product with a 1.0 one, except in one respect: I need something now. Which brings me to Sencha Touch. I was initially impressed with its performance and breadth, but put off by its development style. As I’ve dug in, the holes in its documentation have been frustrating but the breadth has continued to impress me, and I’ve gotten more used to the coding style. The option for paid support is tempting, and I’d probably buy it if they’d answer my emails. But for now, Pints is a Sencha-based app.

I haven’t answered the big question: can a Web-based app really hold its own alongside native apps? And if so, are the challenges of getting it there worth the benefit of a single codebase?

The answer is YES ! Because the Web-based apps needs the core knowledge of HTML5 and CSS3.

四大并行文件系统对比(HDFS,KFS,CEPH,PANASAS)

什么是Hadoop

Hadoop是apache下面的一个分布式并行计算框架,是从lunece中抽取出来的一个框架。Hadoop的核心设计思想是:MapReduce和HDFS,MapReduce是Google提出的一个软件架构,用于大规模数据集(大于1TB)的并行运算。概念”Map(映射)”和”Reduce(化简)”,和他们的主要思想,都是从函数式编程语言借来的,还有从矢量编程语言借来的特性;HDFS是Hadoop Distributed File System的缩写,即:Hadoop分布式文件系统,为分布式计算存储提供底层支持。注:MapReduce (google mapreduce 论文点击这里),GFS(Google File System)和bigtable是google的三大核心技术。

HadoopMapReduce介绍

Map(映射)和reduce(化简)是分开处理的,map是将一个任务分解为多个任务执行,reduce是将多个任务汇总起来得到想要的结果。把一个list拆解为多个放到线程池中启动多个线程计算list中值,然后把多个任务返回的结果合并为一个总的结果其实就是一个简单的MapReduce的应用。

在Hadoop官方文档(单击这里)介绍了HadoopMapReduce的三个步骤,map(主要是分解并行的任务),combine(主要是为了提高reduce的效率),reduce(把处理后的结果再汇总起来)

 

 

1、HDFS

即Hadoop Distributed File System (Hadoop分布式文件系统)
HDFS 具有高容错性,并且可以被部署在低价的硬件设备之上。HDFS很适合那些有大数据集的应用,并且提供了对数据读写的高吞吐率。HDFS是一个 master/slave的结构,就通常的部署来说,在master上只运行一个Namenode,而在每一个slave上运行一个Datanode。
HDFS 支持传统的层次文件组织结构,同现有的一些文件系统在操作上很类似,比如你可以创建和删除一个文件,把一个文件从一个目录移到另一个目录,重命名等等操作。Namenode管理着整个分布式文件系统,对文件系统的操作(如建立、删除文件和文件夹)都是通过Namenode来控制。
下面是HDFS的结构:

从上面的图中可以看 出,Namenode,Datanode,Client之间的通信都是建立在TCP/IP的基础之上的。当Client要执行一个写入的操作的时候,命令 不是马上就发送到Namenode,Client首先在本机上临时文件夹中缓存这些数据,当临时文件夹中的数据块达到了设定的Block的值(默认是 64M)时,Client便会通知Namenode,Namenode便响应Client的RPC请求,将文件名插入文件系统层次中并且在 Datanode中找到一块存放该数据的block,同时将该Datanode及对应的数据块信息告诉Client,Client便这些本地临时文件夹中 的数据块写入指定的数据节点。
HDFS采取了副本策略,其目的是为了提高系统的可靠性,可用性。HDFS的副本放置策略是三个副本, 一个放在本节点上,一个放在同一机架中的另一个节点上,还有一个副本放在另一个不同的机架中的一个节点上。当前版本的hadoop0.12.0中还没有实 现,但是正在进行中,相信不久就可以出来了。

KFSKOSMOS DISTRIBUTED FILE SYSTEM),一个类似GFSHadoopHDFS 的一个开源的分布式文件系统。

 

PS: google的三大基石 gfs,bigtable,map-reduce 相对应的开源产品 gfs:kfs(据传google创史人的同窗所创)hdfs(hadoop的子项目) bigtable:hbase(hadoop的子项目)Hypertable(从hbase项目组分离出去的,用c++实现) map-reduce:hadoopapache的项目,java实现,目前创史人在yahoo全力打造,已有2000个以上的节点并行计算的规模)

 

 

Google两个共同创始人的两个大学同窗(印度人)Anand RajaramanVenky Harinarayan,创立的一个新的搜索引擎Kosmix最近捐献了一个克隆GFS的文件系统KFS项目。HadoopHypertable这两个项目也开始支持KFS来做底层的存储。KFS是用C++写的,但是其client支持C++JavaPython。那么KFS到底有什么特性呢?

 

支持存储扩充(添加新的chunckserver,系统自动感知)

有效性(复制机制保证文件有效性)

负载平衡(系统周期地检查chunkservers的磁盘利用,并重新平衡chunkservers的磁盘利用,HDFS现在还没有支持)

数据完整性(当要读取数据时检查数据的完整性,如果检验出错使用另外的备份覆盖当前的数据)

支持FUSEHDFS也有工具支持FUSE

使用契约(保证Client缓存的数据和文件系统中的文件保持一致性)

HDFS未支持的高级特性:

 

支持同一文件多次写入和Append,不像HDFS支持一次写入多次读取和不支持Append(最近要增加Append,但是遇到许多问题)。

文件及时有效,当应用程序创建一个文件时,文件名在系统马上有效。不像HDFS文件只当输入流关闭时才在系统中有效,因此,如果应用程序在关闭前出现异常导致没有关闭输入流,数据将会丢失。

resin + nginx

download the upstream jvm route from http://code.google.com/p/nginx-upstream-jvm-route/

[root@web1 nginx-1.2.0]# tar -zxf nginx-upstream-jvm-route-0.1.tar.gz

[root@web1 nginx-1.2.0]# patch -p0 ./nginx_upstream_jvm_route/jvm_route.patch

[root@web1 nginx-1.2.0]# ./configure –sbin-path=/usr/sbin/ –conf-path=/etc/nginx/nginx.conf –with-http_stub_status_module –with-http_ssl_module –add-module=./nginx_upstream_jvm_route

 

edit /etc/nginx/nginx.conf

add

upstream resin_proxy {

server 127.0.0.1:85 srun_id=app-by1 max_fails=2 fail_timeout=10s weight=200; //负载均衡
server web3:85 srun_id=app-by2 max_fails=2 fail_timeout=10s weight=200;      //负载均衡
jvm_route $cookie_JSESSIONID|sessionid;
}

server {
listen 80;
server_name default yourhosts;
location / {
proxy_pass http://resin_proxy;
include proxy.conf;
index index.jsp index.html index.htm;
}
}

create proxy.conf

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

 

 

lame wav to mp3 Unsupported data format: 0x0006

今天在对上传的wav转为mp3时报错
Unsupported data format: 0x0006
从别的格式pcm导出的wav可以正常转为mp3
出现这个问题第一个想到的办法就是通过mplayer把wav用pcm导一遍再转
这里给出第二个解决方案
先删除lame
apt-get remove lame
安装sndfile
apt-get apt-get install libsndfile1 libsndfile1-dev
下载最新的lame源码
wget http://cdnetworks-kr-2.dl.sourceforge.net/project/lame/lame/3.99/lame-3.99.2.tar.gz
解压安装
tar zxf lame-3.99.2.tar.gz
cd lame-3.99.2/
./configure –with-fileio=sndfile
make && make install
做软链接
ln -s /usr/local/bin/lame /usr/bin/

执行了javascript后Gif就不动了

这是一个困扰我几天的问题,就是我的网站上的gif图标突然不动了,今天终于知道问题所在,是javascript引起的,原因就是我用了href="javascript:void(0)"这种伪协议哈。 
将我原来的代码: 
Java代码  
<a href="javascript:void(0);" onClick="email_pic()">  

改成 
Java代码  
<a href="javascript:;" onClick="email_pic();return false;">  

就OK了

Linux文件查找命令find,xargs详述

前言:关于find命令

由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。

在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。


一、find 命令格式


1、find命令的一般形式为;

 

find pathname -options [-print -exec -ok ...]

 


2、find命令的参数;

 

pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {  } \;,注意{   }和\;之间的空格。
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

 


3、find命令选项

-name

按照文件名查找文件。

-perm
按照文件权限来查找文件。

-prune
使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。

-user
按照文件属主来查找文件。

-group
按照文件所属的组来查找文件。

-mtime -n +n
按照文件的更改时间来查找文件, – n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。

-nogroup
查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

-nouser
查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2

查找更改时间比文件file1新但比文件file2旧的文件。
-type

查找某一类型的文件,诸如:

b – 块设备文件。
d – 目录。
c – 字符设备文件。
p – 管道文件。
l – 符号链接文件。
f – 普通文件。

-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。

-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

 

另外,下面三个的区别:

   -amin n
查找系统中最后N分钟访问的文件
-atime n
查找系统中最后n*24小时访问的文件

-cmin n
查找系统中最后N分钟被改变文件状态的文件

-ctime n
查找系统中最后n*24小时被改变文件状态的文件

-mmin n
查找系统中最后N分钟被改变文件数据的文件

-mtime n
查找系统中最后n*24小时被改变文件数据的文件

 


4、使用exec或ok来执行shell命令

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的

在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

例如:为了用ls -l命令列出所匹配到的文件,可以把ls -l命令放在find命令的-exec选项中

 

# find . -type f -exec ls -l {  } \;
-rw-r--r--    1 root     root        34928 2003-02-25  ./conf/httpd.conf
-rw-r--r--    1 root     root        12959 2003-02-25  ./conf/magic
-rw-r--r--    1 root     root          180 2003-02-25  ./conf.d/README

 

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
在/logs目录中查找更改时间在5日以前的文件并删除它们:

 

$ find logs -type f -mtime +5 -exec rm {  } \;

 

记住:在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

在下面的例子中, find命令在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。

 

$ find . -name "*.conf"  -mtime +5 -ok rm {  } \;
< rm ... ./conf/httpd.conf > ? n

 

按y键删除文件,按n键不删除。

任何形式的命令都可以在-exec选项中使用。

在下面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个sam用户。

 

# find /etc -name "passwd*" -exec grep "sam" {  } \;
sam:x:501:501::/usr/sam:/bin/bash

 


二、find命令的例子;


1、查找当前用户主目录下的所有文件:

下面两种方法都可以使用

 

$ find $HOME -print
$ find ~ -print


2、让当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件;

 

 

$ find . -type f -perm 644 -exec ls -l {  } \;

 


3、为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径;

 

$ find / -type f -size 0 -exec ls -l {  } \;

 


4、查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们;

 

$ find /var/logs -type f -mtime +7 -ok rm {  } \;

 


5、为了查找系统中所有属于root组的文件;

 

$find . -group root -exec ls -l {  } \;
-rw-r--r--    1 root     root          595 10月 31 01:09 ./fie1

 


6、find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件。

该命令只检查三位数字,所以相应文件的后缀不要超过999。先建几个admin.log*的文件 ,才能使用下面这个命令

 

$ find . -name "admin.log[0-9][0-9][0-9]" -atime -7  -ok
rm {  } \;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n

 


7、为了查找当前文件系统中的所有目录并排序;

 

$ find . -type d | sort

 


8、为了查找系统中所有的rmt磁带设备;

 

$ find /dev/rmt -print

 


三、xargs

xargs – build and execute command lines from standard input

在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;

而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

来看看xargs命令是如何同find命令一起使用的,并给出一些例子。

下面的例子查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件

 

#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory:      ISO-8859 text\
......

 

在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:

 

$ find / -name "core" -print | xargs echo "" >/tmp/core.log

 

上面这个执行太慢,我改成在当前目录下查找

 

#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6

 

在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限:

 

# ls -l
drwxrwxrwx    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 httpd.conf
# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf

 

用grep命令在所有的普通文件中搜索hostname这个词:

 

# find . -type f -print | xargs grep "hostname"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

 

用grep命令在当前目录下的所有普通文件中搜索hostnames这个词:

 

# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

 

注意,在上面的例子中, \用来取消find命令中的*在shell中的特殊含义。

find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。


四、find 命令的参数

下面是find一些常用参数的例子,有用到的时候查查就行了,像上面前几个贴子,都用到了其中的的一些参数,也可以用man或查看论坛里其它贴子有find的命令手册


1、使用name选项

文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用。

可以使用某种文件名模式来匹配文件,记住要用引号将文件名模式引起来。

不管当前路径是什么,如果想要在自己的根目录$HOME中查找文件名符合*.txt的文件,使用~作为 ‘pathname’参数,波浪号~代表了你的$HOME目录。

 

$ find ~ -name "*.txt" -print

 

想要在当前目录及子目录中查找所有的‘ *.txt’文件,可以用:

 

$ find . -name "*.txt" -print

 

想要的当前目录及子目录中查找文件名以一个大写字母开头的文件,可以用:

 

$ find . -name "[A-Z]*" -print

 

想要在/etc目录中查找文件名以host开头的文件,可以用:

 

$ find /etc -name "host*" -print

 

想要查找$HOME目录中的文件,可以用:

 

$ find ~ -name "*" -print 或find . -print

 

要想让系统高负荷运行,就从根目录开始查找所有的文件。

 

$ find / -name "*" -print

 

如果想在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是.txt的文件,下面的命令就能够返回名为ax37.txt的文件:

 

$find . -name "[a-z][a-z][0--9][0--9].txt" -print

 


2、用perm选项

按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。

如在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用:

 

$ find . -perm 755 -print

 

还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-006相当于666

 

# ls -l
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rw-rw-rw-    1 sam      adm         34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 10月 26 19:48 sam
-rw-rw-rw-    1 root     root         2792 10月 31 20:19 temp
# find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp

 

-perm mode:文件许可正好符合mode

-perm +mode:文件许可部分符合mode

-perm -mode: 文件许可完全符合mode


3、忽略某个目录

如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略。

如果希望在/apps目录下查找文件,但不希望在/apps/bin目录下查找,可以用:

 

$ find /apps -path "/apps/bin" -prune -o -print

 


4、使用find查找文件的时候怎么避开某个文件目录

比如要在/usr/sam目录下查找不在dir1子目录之内的所有文件

 

find /usr/sam -path "/usr/sam/dir1" -prune -o -print

 

 

find [-path ..] [expression] 在路径列表的后面的是表达式

 

-path “/usr/sam” -prune -o -print 是 -path “/usr/sam” -a -prune -o
-print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果 -path “/usr/sam” 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。如果 -path “/usr/sam” -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。

这个表达式组合特例可以用伪码写为

 

if -path "/usr/sam"  then
-prune
else
-print

 

避开多个文件夹

 

find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print

 

圆括号表示表达式的结合。

\ 表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。

 

查找某一确定文件,-name等选项加在-o 之后

 

#find /usr/sam  \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print

 


5、使用user和nouser选项

按文件属主查找文件,如在$HOME目录中查找文件属主为sam的文件,可以用:

 

$ find ~ -user sam -print

 

在/etc目录下查找文件属主为uucp的文件:

 

$ find /etc -user uucp -print

 

为了查找属主帐户已经被删除的文件,可以使用-nouser选项。这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名; find命令能够为你完成相应的工作。

例如,希望在/home目录下查找所有的这类文件,可以用:

$ find /home -nouser -print

 


6、使用group和nogroup选项

就像user和nouser选项一样,针对文件所属于的用户组, find命令也具有同样的选项,为了在/apps目录下查找属于gem用户组的文件,可以用:

 

$ find /apps -group gem -print

 

要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样的文件

 

$ find / -nogroup-print

 


7、按照更改时间或访问时间等查找文件

如果希望按照更改时间来查找文件,可以使用mtime,atime或ctime选项。如果系统突然没有可用空间了,很有可能某一个文件的长度在此期间增长迅速,这时就可以用mtime选项来查找这样的文件。

用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。

希望在系统根目录下查找更改时间在5日以内的文件,可以用:

 

$ find / -mtime -5 -print

 

为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:

 

$ find /var/adm -mtime +3 -print

 


8、查找比某个文件新或旧的文件

如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项。它的一般形式为:

 

newest_file_name ! oldest_file_name

 

其中,!是逻辑非符号。

查找更改时间比文件sam新但比文件temp旧的文件:

例:有两个文件

 

-rw-r--r--    1 sam      adm             0 10月 31 01:07 fiel
-rw-rw-rw-    1 sam      adm         34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 10月 26 19:48 sam
-rw-rw-rw-    1 root     root         2792 10月 31 20:19 temp
# find -newer httpd1.conf  ! -newer temp -ls
1077669    0 -rwxrwxr-x   2 sam      adm             0 10月 31 01:01 ./httpd.conf
1077671    4 -rw-rw-rw-   1 root     root         2792 10月 31 20:19 ./temp
1077673    0 -rw-r–r–   1 sam      adm             0 10月 31 01:07 ./fiel

 

查找更改时间在比temp文件新的文件:

 

$ find . -newer temp -print


9、使用type选项

 

在/etc目录下查找所有的目录,可以用:

 

$ find /etc -type d -print

 

在当前目录下查找除目录以外的所有类型的文件,可以用:

$ find . ! -type d -print

 

在/etc目录下查找所有的符号链接文件,可以用

$ find /etc -type l -print

 


10、使用size选项

可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。

在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。
在当前目录下查找文件长度大于1 M字节的文件:

$ find . -size +1000000c -print

 

在/home/apache目录下查找文件长度恰好为100字节的文件:

 

$ find /home/apache -size 100c -print

 

在当前目录下查找长度超过10块的文件(一块等于512字节):

 

$ find . -size +10 -print

 


11、使用depth选项

在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。这样做的一个原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目录中的文件。

在下面的例子中, find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。

它将首先匹配所有的文件然后再进入子目录中查找。

 

$ find / -name "CON.FILE" -depth -print

 


12、使用mount选项

在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。

从当前目录开始查找位于本文件系统中文件名以XC结尾的文件:

 

$ find . -name "*.XC" -mount -print