Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Thursday, December 04, 2008

find command examples related with file status

空目录
find . -type d -empty

n*24小时前访问过的文件
find . -atime +2

n分钟前状态发生改变的文件
find . -cmin +10

n*24小时前内容有被修改过的文件
find . -mtime +1

n*24小时与(n+1)*24小时这一天中,内容有被修改过的文件(数字n前没有"+"号)
find . -mtime 1

删除find命令找到的文件
find . -cmin 1 -delete

这上面的命令中需要注意一点是其中数字前面需要带上"+"号,才表示这个时间点之前的时间段,如果没有提供"+"号,则指这的就限定在这个时间点内,如这一天内或者这一分钟内。

更多find相关用法可参考http://linux.about.com/od/commands/l/blcmdl1_find.htm

Thursday, June 19, 2008

delete all file using linux command file

之前用过以下linux命令删除svn的目录:
$> find . -name '.svn' -exec /bin/rm -f {} \;

对于delete操作可以简写如下:
$> find . -name '.svn' -delete
确认即可删除所有.svn目录

Friday, February 22, 2008

Cleaning up rails old session file

$> cd rails_app
$> find tmp/sessions/ruby_sess* -ctime +2 | xargs rm -f

参数说明:
-ctime n: 文件修改时间n*24小时,+2即文件最后修改时间大于2天,此session已经过期。
xargs command: 执行命令。

Friday, September 14, 2007

ActiveRecord find_or_create methods


def create
company = params[:person].delete(:company)
@company = Company.find_or_create_by_name(company[:name])
@person = @company.people.create(params[:person])

respond_to do |format|
format.html { redirect_to(person_list_url) }
format.js
format.xml { render :xml => @person.to_xml(:include => @company) }
end
end

Added find_or_initialize_by_X which works like find_or_create_by_X but doesn’t save the newly instantiated record. [Sam Stephenson]
Added constrain scoping for creates using a hash of attributes bound to the :creation key [DHH]. Example:

Comment.constrain(:creation => { :post_id => 5 }) do
# Associated with :post_id
Comment.create :body => "Hello world"
end

This is rarely used directly, but allows for find_or_create on associations. So you can do:

# If the tag doesn't exist, a new one is created that's associated with the person
person.tags.find_or_create_by_name("Summer")

Added extension capabilities to has_many and has_and_belongs_to_many proxies [DHH]. Example:

class Account < ActiveRecord::Base
has_many :people do
def find_or_create_by_name(name)
first_name, *last_name = name.split
last_name = last_name.join " "

find_or_create_by_first_name_and_last_name(first_name, last_name)
end
end
end

person = Account.find(:first).people.find_or_create_by_name("David Heinemeier Hansson")
person.first_name # => "David"
person.last_name # => "Heinemeier Hansson"

Note that the anoymous module must be declared using brackets, not do/end (due to order of evaluation).