Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1678508
  • 博文数量: 391
  • 博客积分: 8464
  • 博客等级: 中将
  • 技术积分: 4589
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-13 15:12
个人简介

狮子的雄心,骆驼的耐力,孩子的执著!

文章分类

全部博文(391)

文章存档

2023年(4)

2018年(9)

2017年(13)

2016年(18)

2014年(7)

2013年(29)

2012年(61)

2011年(49)

2010年(84)

2009年(95)

2008年(22)

分类: Python/Ruby

2012-11-27 14:07:59


  1. [root@localhost ~]# cd
  2. [root@localhost ~]# pwd
  3. /root
  4. [root@localhost ~]# mkdir xarg-test
  5. [root@localhost ~]# cd xarg-test/
  6. [root@localhost xarg-test]# ls
  7. [root@localhost xarg-test]# touch yufei-test
  8. [root@localhost xarg-test]# find . -name yufei-test
  9. ./yufei-test
  10. [root@localhost xarg-test]# ll /tmp/yufei-test
  11. ls: /tmp/yufei-test: No such file or directory
  12. [root@localhost xarg-test]# find . -name yufei-test -exec cp {} /tmp \;
  13. [root@localhost xarg-test]# ll /tmp/yufei-test
  14. -rw-r--r-- 1 root root 0 May 8 11:01 /tmp/yufei-test
  15. [root@localhost xarg-test]# rm /tmp/yufei-test
  16. rm: remove regular empty file `/tmp/yufei-test'? yes
  17. [root@localhost xarg-test]# ll /tmp/yufei-test
  18. ls: /tmp/yufei-test: No such file or directory
  19. [root@localhost xarg-test]# find . -name yufei-test | xargs cp --target-directory=/tmp
  20. [root@localhost xarg-test]# ll /tmp/yufei-test
  21. -rw-r--r-- 1 root root 0 May 8 11:02 /tmp/yufei-test
  22. [root@localhost xarg-test]ls . | xargs -t cp ./{} /tmp
上述命令意思是把当前目录下的内容列举出来,并复制到/tmp目录。

一  查找文件名或者指定文件创建时间为10天前并且移动到指定目录

  1. [root@localhost jacky]# pwd
  2. /home/test/tmp/jacky
  3. [root@localhost jacky]# ls
  4. file1 file2 file3
  5. [root@localhost jacky]# ls ..
  6. jacky
  7. [root@localhost jacky]# find . -name "file*" -exec mv {} .. \;
    [root@localhost jacky]# find ./ -mtime +10  -exec mv {}  /archivelog/ \;
  8. [root@localhost jacky]# ls
  9. [root@localhost jacky]# ls ..
  10. file1 file2 file3 jacky
  11. [root@localhost jacky]#
二  同MV一样,也可以用exec选项执行cp命令,如下:

  1. root@localhost jacky]# pwd
  2. /home/test/tmp/jacky
  3. [root@localhost jacky]# ls
  4. file1 file2 file3
  5. [root@localhost jacky]# ls ..
  6. jacky
  7. [root@localhost jacky]# find . -name "file*" -exec cp {} .. \;
  8. [root@localhost jacky]# ls
  9. file1 file2 file3
  10. [root@localhost jacky]# ls ..
  11. file1 file2 file3 jacky
  12. [root@localhost jacky]#
三 使用xargs执行cp

  1. root@localhost jacky]# ls
  2. file1 file2 file3
  3. [root@localhost jacky]# find . -name "file*" | xargs -i cp {} {}.bak
  4. [root@localhost jacky]# ls
  5. file1 file1.bak file2 file2.bak file3 file3.bak
  6. [root@localhost jacky]#
四 使用xargs执行mv

  1. [root@localhost jacky]# pwd
  2. /home/test/tmp/jacky
  3. [root@localhost jacky]# ls
  4. file1 file2 file3
  5. [root@localhost jacky]# ls ..
  6. jacky

  7. [root@localhost jacky]# find . -name "file*" | xargs -i mv {} ..
  8. [root@localhost jacky]# ls
  9. [root@localhost jacky]# ls ..
  10. file1 file2 file3 jacky
  11. [root@localhost jacky]#
五  find后执行xargs提示xargs: argument line too long解决方法:

  1. find . -type f -atime +20 -print0 | xargs -0 -l1 -t rm -f
-l1是一次处理一个

-t是处理之前打印出命令

 六 xargs选项的其他用法

  1. [root@localhost jacky]# pwd
  2. /home/test/tmp/jacky
  3. [root@localhost jacky]# ls
  4. file1 file2 file3
  5. [root@localhost jacky]# ls ..
  6. jacky
  7. [root@localhost jacky]# find . -name "file*" | xargs -I [] cp [] ..
  8. [root@localhost jacky]# ls ..
  9. file1 file2 file3 jacky
使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[]

七  xargs的-p参数的使用

  1. [root@localhost jacky]# pwd
  2. /home/test/tmp/jacky
  3. [root@localhost jacky]# ls
  4. file1 file2 file3
  5. [root@localhost jacky]# ls ..
  6. jacky
  7. [root@localhost jacky]# find . -name "file*" | xargs -p -i mv {} ..
  8. mv ./file3 .. ?...y
  9. mv ./file1 .. ?...y
  10. mv ./file2 .. ?...n
  11. [root@localhost jacky]# ls
  12. file2
  13. [root@localhost jacky]# ls ..
  14. file1 file3 jacky
-p参数会提示让你确认是否执行后面的命令,y执行,n不执行。

阅读(6505) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~