- [root@localhost ~]# cd
- [root@localhost ~]# pwd
- /root
- [root@localhost ~]# mkdir xarg-test
- [root@localhost ~]# cd xarg-test/
- [root@localhost xarg-test]# ls
- [root@localhost xarg-test]# touch yufei-test
- [root@localhost xarg-test]# find . -name yufei-test
- ./yufei-test
- [root@localhost xarg-test]# ll /tmp/yufei-test
- ls: /tmp/yufei-test: No such file or directory
- [root@localhost xarg-test]# find . -name yufei-test -exec cp {} /tmp \;
- [root@localhost xarg-test]# ll /tmp/yufei-test
- -rw-r--r-- 1 root root 0 May 8 11:01 /tmp/yufei-test
- [root@localhost xarg-test]# rm /tmp/yufei-test
- rm: remove regular empty file `/tmp/yufei-test'? yes
- [root@localhost xarg-test]# ll /tmp/yufei-test
- ls: /tmp/yufei-test: No such file or directory
- [root@localhost xarg-test]# find . -name yufei-test | xargs cp --target-directory=/tmp
- [root@localhost xarg-test]# ll /tmp/yufei-test
- -rw-r--r-- 1 root root 0 May 8 11:02 /tmp/yufei-test
- [root@localhost xarg-test]ls . | xargs -t cp ./{} /tmp
上述命令意思是把当前目录下的内容列举出来,并复制到/tmp目录。
一 查找文件名或者指定文件创建时间为10天前并且移动到指定目录
- [root@localhost jacky]# pwd
- /home/test/tmp/jacky
- [root@localhost jacky]# ls
- file1 file2 file3
- [root@localhost jacky]# ls ..
- jacky
- [root@localhost jacky]# find . -name "file*" -exec mv {} .. \;
[root@localhost jacky]# find ./ -mtime +10 -exec mv {} /archivelog/ \;
- [root@localhost jacky]# ls
- [root@localhost jacky]# ls ..
- file1 file2 file3 jacky
- [root@localhost jacky]#
二 同MV一样,也可以用exec选项执行cp命令,如下:
- root@localhost jacky]# pwd
- /home/test/tmp/jacky
- [root@localhost jacky]# ls
- file1 file2 file3
- [root@localhost jacky]# ls ..
- jacky
- [root@localhost jacky]# find . -name "file*" -exec cp {} .. \;
- [root@localhost jacky]# ls
- file1 file2 file3
- [root@localhost jacky]# ls ..
- file1 file2 file3 jacky
- [root@localhost jacky]#
三 使用xargs执行cp
- root@localhost jacky]# ls
- file1 file2 file3
- [root@localhost jacky]# find . -name "file*" | xargs -i cp {} {}.bak
- [root@localhost jacky]# ls
- file1 file1.bak file2 file2.bak file3 file3.bak
- [root@localhost jacky]#
四 使用xargs执行mv
- [root@localhost jacky]# pwd
- /home/test/tmp/jacky
- [root@localhost jacky]# ls
- file1 file2 file3
- [root@localhost jacky]# ls ..
- jacky
- [root@localhost jacky]# find . -name "file*" | xargs -i mv {} ..
- [root@localhost jacky]# ls
- [root@localhost jacky]# ls ..
- file1 file2 file3 jacky
- [root@localhost jacky]#
五 find后执行xargs提示xargs: argument line too long解决方法:
- find . -type f -atime +20 -print0 | xargs -0 -l1 -t rm -f
-l1是一次处理一个
-t是处理之前打印出命令
六 xargs选项的其他用法
- [root@localhost jacky]# pwd
- /home/test/tmp/jacky
- [root@localhost jacky]# ls
- file1 file2 file3
- [root@localhost jacky]# ls ..
- jacky
- [root@localhost jacky]# find . -name "file*" | xargs -I [] cp [] ..
- [root@localhost jacky]# ls ..
- file1 file2 file3 jacky
使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[]
七 xargs的-p参数的使用
- [root@localhost jacky]# pwd
- /home/test/tmp/jacky
- [root@localhost jacky]# ls
- file1 file2 file3
- [root@localhost jacky]# ls ..
- jacky
- [root@localhost jacky]# find . -name "file*" | xargs -p -i mv {} ..
- mv ./file3 .. ?...y
- mv ./file1 .. ?...y
- mv ./file2 .. ?...n
- [root@localhost jacky]# ls
- file2
- [root@localhost jacky]# ls ..
- file1 file3 jacky
-p参数会提示让你确认是否执行后面的命令,y执行,n不执行。
阅读(6565) | 评论(0) | 转发(3) |