find 和 mv
来源:互联网 发布:软件测试精品课程 编辑:程序博客网 时间:2024/05/18 03:30
目的是使用一条命令把搜到的文件转移到另一个目录。
当前的目录结构:
~/tmp$ tree ..└── dir1 directory, 0 files
生成测试文件:
:~/tmp$ for file in {1..100}.txt > do> touch $file> done
:~/tmp$ ls100.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt10.txt 20.txt 30.txt 40.txt 50.txt 60.txt 70.txt 80.txt 90.txt dir11.txt 21.txt 31.txt 41.txt 51.txt 61.txt 71.txt 81.txt 91.txt12.txt 22.txt 32.txt 42.txt 52.txt 62.txt 72.txt 82.txt 92.txt13.txt 23.txt 33.txt 43.txt 53.txt 63.txt 73.txt 83.txt 93.txt14.txt 24.txt 34.txt 44.txt 54.txt 64.txt 74.txt 84.txt 94.txt15.txt 25.txt 35.txt 45.txt 55.txt 65.txt 75.txt 85.txt 95.txt16.txt 26.txt 36.txt 46.txt 56.txt 66.txt 76.txt 86.txt 96.txt17.txt 27.txt 37.txt 47.txt 57.txt 67.txt 77.txt 87.txt 97.txt18.txt 28.txt 38.txt 48.txt 58.txt 68.txt 78.txt 88.txt 98.txt19.txt 29.txt 39.txt 49.txt 59.txt 69.txt 79.txt 89.txt 99.txt
~/tmp$ find . -type f -exec mv -t dir {} +或者:
~/tmp$ find . -type f -exec mv -t dir {} \;mv: './dir/11.txt' and 'dir/11.txt' are the same filemv: './dir/29.txt' and 'dir/29.txt' are the same filemv: './dir/5.txt' and 'dir/5.txt' are the same filemv: './dir/58.txt' and 'dir/58.txt' are the same filemv: './dir/32.txt' and 'dir/32.txt' are the same filemv: './dir/26.txt' and 'dir/26.txt' are the same filemv: './dir/90.txt' and 'dir/90.txt' are the same filemv: './dir/39.txt' and 'dir/39.txt' are the same filemv: './dir/84.txt' and 'dir/84.txt' are the same filemv: './dir/97.txt' and 'dir/97.txt' are the same filemv: './dir/96.txt' and 'dir/96.txt' are the same filemv: './dir/81.txt' and 'dir/81.txt' are the same filemv: './dir/28.txt' and 'dir/28.txt' are the same filemv: './dir/15.txt' and 'dir/15.txt' are the same filemv: './dir/30.txt' and 'dir/30.txt' are the same filemv: './dir/6.txt' and 'dir/6.txt' are the same filemv: './dir/100.txt' and 'dir/100.txt' are the same filemv: './dir/64.txt' and 'dir/64.txt' are the same filemv: './dir/86.txt' and 'dir/86.txt' are the same filemv: './dir/57.txt' and 'dir/57.txt' are the same filemv: './dir/3.txt' and 'dir/3.txt' are the same filemv: './dir/21.txt' and 'dir/21.txt' are the same filemv: './dir/42.txt' and 'dir/42.txt' are the same filemv: './dir/87.txt' and 'dir/87.txt' are the same filemv: './dir/79.txt' and 'dir/79.txt' are the same filemv: './dir/8.txt' and 'dir/8.txt' are the same filemv: './dir/20.txt' and 'dir/20.txt' are the same filemv: './dir/23.txt' and 'dir/23.txt' are the same filemv: './dir/27.txt' and 'dir/27.txt' are the same filemv: './dir/99.txt' and 'dir/99.txt' are the same filemv: './dir/33.txt' and 'dir/33.txt' are the same filemv: './dir/69.txt' and 'dir/69.txt' are the same filemv: './dir/74.txt' and 'dir/74.txt' are the same filemv: './dir/41.txt' and 'dir/41.txt' are the same filemv: './dir/17.txt' and 'dir/17.txt' are the same filemv: './dir/44.txt' and 'dir/44.txt' are the same filemv: './dir/77.txt' and 'dir/77.txt' are the same filemv: './dir/46.txt' and 'dir/46.txt' are the same filemv: './dir/34.txt' and 'dir/34.txt' are the same filemv: './dir/1.txt' and 'dir/1.txt' are the same filemv: './dir/67.txt' and 'dir/67.txt' are the same filemv: './dir/13.txt' and 'dir/13.txt' are the same filemv: './dir/53.txt' and 'dir/53.txt' are the same filemv: './dir/47.txt' and 'dir/47.txt' are the same filemv: './dir/45.txt' and 'dir/45.txt' are the same filemv: './dir/22.txt' and 'dir/22.txt' are the same filemv: './dir/70.txt' and 'dir/70.txt' are the same filemv: './dir/4.txt' and 'dir/4.txt' are the same filemv: './dir/89.txt' and 'dir/89.txt' are the same filemv: './dir/59.txt' and 'dir/59.txt' are the same filemv: './dir/94.txt' and 'dir/94.txt' are the same filemv: './dir/98.txt' and 'dir/98.txt' are the same filemv: './dir/12.txt' and 'dir/12.txt' are the same filemv: './dir/63.txt' and 'dir/63.txt' are the same filemv: './dir/24.txt' and 'dir/24.txt' are the same filemv: './dir/38.txt' and 'dir/38.txt' are the same filemv: './dir/82.txt' and 'dir/82.txt' are the same filemv: './dir/76.txt' and 'dir/76.txt' are the same filemv: './dir/7.txt' and 'dir/7.txt' are the same filemv: './dir/50.txt' and 'dir/50.txt' are the same filemv: './dir/93.txt' and 'dir/93.txt' are the same filemv: './dir/62.txt' and 'dir/62.txt' are the same filemv: './dir/43.txt' and 'dir/43.txt' are the same file
这一条命令之所以有报错是因为文件移到dir后又被find搜到了。。
可以使用 maxdepth 来避免:
~/tmp$ find . -maxdepth 1 -type f -exec mv -t dir {} \;也可以用:
~/tmp$ find . -maxdepth 1 -type f -exec mv {} dir \;
或者:
~/tmp$ find . -path "./dir*" -prune -o -type f -exec mv {} dir \;
也可以使用 xargs.
~/tmp$ find . -path "dir*" -o -type f -print0 | xargs -I {} -0 mv {} dir
其他比较不常见的方法:
~/tmp$ find . -path "dir*" -o -type f -exec sh -c 'mv "$@" dir' find-sh {} +
~/tmp$ for file in $( find . -path "dir*" -prune -o -type f); do mv $file dir; done上面这种方法不能处理文件名包括空格的情况。
~/tmp$ find . -path "./dir*" -prune -o -type f -print0 | while IFS= read -r -d $'\0' file; do mv "$file" dir; done
References:
1. http://unix.stackexchange.com/questions/154818/how-to-integrate-mv-command-after-find-command
2. http://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice
0 0
- find 和 mv
- find && mv
- 文件和目录操作命令 find mkdir mv
- find && mv 用法
- find + mv 批量移动文件
- Linux命令 find和mv的结合使用:查找文件,移动到某个目录
- Linux命令 find和mv的结合使用:查找文件,移动到某个目录
- find和mv的结合使用解决 arg list too long的问题
- Linux命令 find和mv的结合使用:查找文件,移动到某个目录
- Linux Cp和MV
- mv
- mv
- mv
- mv
- MV
- mv
- mv
- mv
- 简单工厂模式
- Linux 下安装 jdk-7u75-linux-x64.gz,jdk1.7.0_75,jdk1.7
- HDUoj 1789 Doing Homework again ( 贪心 好题
- Leetcode 139. Word Break
- Bzoj 2242: [SDOI2011]计算器(BSGS)
- find 和 mv
- Leet Code OJ 388. Longest Absolute File Path [Difficulty: Medium]
- Android UI开发中的常用控件TextView
- Linux下ARM汇编语法
- jquery DateTable
- 【学术篇】网络流24题--餐巾计划问题
- python install in linux
- Construct the Rectangle
- [LeetCode]53. Maximum Subarray (动态规划)