我想以递归方式批量复制和重命名目录中的所有文件.
我有这样的事情:
/dir/subdir/file.aa
/dir/subdir/fileb.aa
/dir/filec.aa
Run Code Online (Sandbox Code Playgroud)
并希望将所有文件复制为:
/newdir/1.xx
/newdir/2.xx
/newdir/3.xx
/newdir/4.xx
Run Code Online (Sandbox Code Playgroud)
.. /newdir/nn.xx
我怎么能用bash做到这一点?
find -name "*.aa" | cat -n | while read n f; do
cp "$f" newdir/"$n".xx
done
Run Code Online (Sandbox Code Playgroud)
将使用(几乎)任何有效的文件名(除非你有换行符,这也是允许的)。
如果您不限于外壳,python 中的另一个解决方案可能是
#!/usr/bin/env python
if __name__ == '__main__':
import sys
import os
import shutil
target = sys.argv[1]
for num, source in enumerate(sys.argv[2:]):
shutil.move(source, os.path.join(target, "%d.xx" % num))
Run Code Online (Sandbox Code Playgroud)
然后可以称为
<script name> newdir *.aa
Run Code Online (Sandbox Code Playgroud)
尝试某事。像这样:
num=0
for i in `find -name "*.aa"`; do
let num=num+1
cp $i newdir/$lc.xx
done
Run Code Online (Sandbox Code Playgroud)