目的是使用一条命令把搜到的文件转移到另一个目录。
当前的目录结构:
~/tmp$ tree .
.
└── dir
1 directory, 0 files
生成测试文件:
:~/tmp$ for file in {1..100}.txt
> do
> touch $file
> done
:~/tmp$ ls
100.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
10.txt 20.txt 30.txt 40.txt 50.txt 60.txt 70.txt 80.txt 90.txt dir
11.txt 21.txt 31.txt 41.txt 51.txt 61.txt 71.txt 81.txt 91.txt
12.txt 22.txt 32.txt 42.txt 52.txt 62.txt 72.txt 82.txt 92.txt
13.txt 23.txt 33.txt 43.txt 53.txt 63.txt 73.txt 83.txt 93.txt
14.txt 24.txt 34.txt 44.txt 54.txt 64.txt 74.txt 84.txt 94.txt
15.txt 25.txt 35.txt 45.txt 55.txt 65.txt 75.txt 85.txt 95.txt
16.txt 26.txt 36.txt 46.txt 56.txt 66.txt 76.txt 86.txt 96.txt
17.txt 27.txt 37.txt 47.txt 57.txt 67.txt 77.txt 87.txt 97.txt
18.txt 28.txt 38.txt 48.txt 58.txt 68.txt 78.txt 88.txt 98.txt
19.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 file
mv: './dir/29.txt' and 'dir/29.txt' are the same file
mv: './dir/5.txt' and 'dir/5.txt' are the same file
mv: './dir/58.txt' and 'dir/58.txt' are the same file
mv: './dir/32.txt' and 'dir/32.txt' are the same file
mv: './dir/26.txt' and 'dir/26.txt' are the same file
mv: './dir/90.txt' and 'dir/90.txt' are the same file
mv: './dir/39.txt' and 'dir/39.txt' are the same file
mv: './dir/84.txt' and 'dir/84.txt' are the same file
mv: './dir/97.txt' and 'dir/97.txt' are the same file
mv: './dir/96.txt' and 'dir/96.txt' are the same file
mv: './dir/81.txt' and 'dir/81.txt' are the same file
mv: './dir/28.txt' and 'dir/28.txt' are the same file
mv: './dir/15.txt' and 'dir/15.txt' are the same file
mv: './dir/30.txt' and 'dir/30.txt' are the same file
mv: './dir/6.txt' and 'dir/6.txt' are the same file
mv: './dir/100.txt' and 'dir/100.txt' are the same file
mv: './dir/64.txt' and 'dir/64.txt' are the same file
mv: './dir/86.txt' and 'dir/86.txt' are the same file
mv: './dir/57.txt' and 'dir/57.txt' are the same file
mv: './dir/3.txt' and 'dir/3.txt' are the same file
mv: './dir/21.txt' and 'dir/21.txt' are the same file
mv: './dir/42.txt' and 'dir/42.txt' are the same file
mv: './dir/87.txt' and 'dir/87.txt' are the same file
mv: './dir/79.txt' and 'dir/79.txt' are the same file
mv: './dir/8.txt' and 'dir/8.txt' are the same file
mv: './dir/20.txt' and 'dir/20.txt' are the same file
mv: './dir/23.txt' and 'dir/23.txt' are the same file
mv: './dir/27.txt' and 'dir/27.txt' are the same file
mv: './dir/99.txt' and 'dir/99.txt' are the same file
mv: './dir/33.txt' and 'dir/33.txt' are the same file
mv: './dir/69.txt' and 'dir/69.txt' are the same file
mv: './dir/74.txt' and 'dir/74.txt' are the same file
mv: './dir/41.txt' and 'dir/41.txt' are the same file
mv: './dir/17.txt' and 'dir/17.txt' are the same file
mv: './dir/44.txt' and 'dir/44.txt' are the same file
mv: './dir/77.txt' and 'dir/77.txt' are the same file
mv: './dir/46.txt' and 'dir/46.txt' are the same file
mv: './dir/34.txt' and 'dir/34.txt' are the same file
mv: './dir/1.txt' and 'dir/1.txt' are the same file
mv: './dir/67.txt' and 'dir/67.txt' are the same file
mv: './dir/13.txt' and 'dir/13.txt' are the same file
mv: './dir/53.txt' and 'dir/53.txt' are the same file
mv: './dir/47.txt' and 'dir/47.txt' are the same file
mv: './dir/45.txt' and 'dir/45.txt' are the same file
mv: './dir/22.txt' and 'dir/22.txt' are the same file
mv: './dir/70.txt' and 'dir/70.txt' are the same file
mv: './dir/4.txt' and 'dir/4.txt' are the same file
mv: './dir/89.txt' and 'dir/89.txt' are the same file
mv: './dir/59.txt' and 'dir/59.txt' are the same file
mv: './dir/94.txt' and 'dir/94.txt' are the same file
mv: './dir/98.txt' and 'dir/98.txt' are the same file
mv: './dir/12.txt' and 'dir/12.txt' are the same file
mv: './dir/63.txt' and 'dir/63.txt' are the same file
mv: './dir/24.txt' and 'dir/24.txt' are the same file
mv: './dir/38.txt' and 'dir/38.txt' are the same file
mv: './dir/82.txt' and 'dir/82.txt' are the same file
mv: './dir/76.txt' and 'dir/76.txt' are the same file
mv: './dir/7.txt' and 'dir/7.txt' are the same file
mv: './dir/50.txt' and 'dir/50.txt' are the same file
mv: './dir/93.txt' and 'dir/93.txt' are the same file
mv: './dir/62.txt' and 'dir/62.txt' are the same file
mv: './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