find 的命令是强大, 要会用才好

来源:互联网 发布:射雕同人之悦与君知 编辑:程序博客网 时间:2024/05/21 08:00
 

关于 find 的高级应用:

1: change the file owner: tt --> test1
  find . -name tt -exec chown test1 {} \;
 
2: rename or remove the specified file
  find . -inum 11111 -exec mv {} new_file \;
  find . -inum 11111 -exec rm {} \;
 
  future step:
  find . -user tt -exec cp {} ./file \;
  find . -inum 11111 -exec cp {} ./file \;
 
 
4:查找文件权限 700:/ 查找文件权限非400
  find . -perm 700  / find . ! -perm 400

 

5,对大文件进行转移:

find . -size +3000000
会把大于3G 的文件列出来
如果要转移的话: find . -size +3000000 -exec cp '{}' /tmp ';'

 

2012-11-04  进行的change (action):

对一个文件系统下东西进行转移:

find /tmp/a/ -mtime +100 -exec mv '{}' /tmp/bk/ ';'

(注意,这个/tmp/a/ 下面的文件可能有文件夹,执行脚本的时候,会有报错,警告,但是,没有关系,看看/tmp/bk/ 下面已经有文件转移过去,并且/tmp/a下面已经没有就可以了)

(这个操作没有压缩,是因为 怕原来的 文件系统会因为压缩,是原来的file system 撑暴

---------------

20110923 补充:

找出100 天的东西:(一定要写path, 很危险)

find /oracle/ ora102/admin/bdump -mtime +100 -exec  rm {} \;

 

20111024 补充:

找出30天以前 以log 结尾的删除:

find . -name "*.log" -mtime +30 -exec rm {} \;

 

20120817补充:

今天移动文件饿时候 报错:

./t/sheng is identical

解决方法:

find . -name "sheng*" -exec cp -R {} /t \;

注意-R 的用法。

 

找出并且删掉,注意,上面command 一定要写好路径(path) 因为是 rm, 一定要小心。

 

20121031 更新:

测试目的:
改变特定文件的权限:

先创建3个文件:
cd /tmp/test

touch a b c
ls
-rw-r--r--  1 root system a
-rw-r--r--  1 root system b
-rw-r--r--  1 root system c

chmod o+x `find /tmp/test/ -mtime -1`

看一下测试结果:

cd /tmp/test
ls

-rw-r--r-x  1 root system a
-rw-r--r-x  1 root system b
-rw-r--r-x  1 root system c

---------


下面接着测试:

打包:一些特定文件:

tar -cvf 1.tar `find /tmp/test/ -mtime -1`

压缩:

压缩到指定目录:

tar -zxvf /home/images.tar.gz -C /specific dir

 

----

20150806 update:

在linux下使用find命令时,报错:find: missing argument to `-exec'

在{}和\之间必须要有空格,否则会报上面的错。

for example: find . -name *.txt -exec ls -l {} \;