bash 批量复制和重命名多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7000688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch copy and rename multiple files
提问by Leonardo M. Ramé
I would like to batch copy and rename all files from a directory, recursively.
我想从一个目录中递归地批量复制和重命名所有文件。
I have something like this:
我有这样的事情:
/dir/subdir/file.aa
/dir/subdir/fileb.aa
/dir/filec.aa
and would like all files to be copied as this:
并希望将所有文件复制为:
/newdir/1.xx
/newdir/2.xx
/newdir/3.xx
/newdir/4.xx
.. /newdir/nn.xx
.. /newdir/nn.xx
How can I do this in bash?
我怎样才能在 bash 中做到这一点?
采纳答案by ypnos
Try sth. like this:
试试…… 像这样:
num=0
for i in `find -name "*.aa"`; do
let num=num+1
cp $i newdir/$lc.xx
done
回答by glglgl
find -name "*.aa" | cat -n | while read n f; do
cp "$f" newdir/"$n".xx
done
will work with (nearly) any valid filename (except if you have newlines in it, which would be allowed as well).
将使用(几乎)任何有效的文件名(除非你有换行符,这也是允许的)。
If you are not restricted to the shell, another solution in python could be
如果您不限于外壳,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))
which then could be called as
然后可以称为
<script name> newdir *.aa
回答by liberias
I will try to answer this, but this might not be the optimal solution, since there are many tools that could be used for this.
我将尝试回答这个问题,但这可能不是最佳解决方案,因为有许多工具可用于此目的。
My way of solving this problem would be to write a function and then apply this function to every file in the directory/subdirectories. Assuming that you have multiple cores/processors and that your function would be doing something more CPU consuming than just renaming and copying files you would also parallelize the task.
我解决这个问题的方法是编写一个函数,然后将该函数应用于目录/子目录中的每个文件。假设您有多个内核/处理器,并且您的函数会做一些比重命名和复制文件更消耗 CPU 的事情,您还将并行化任务。
The bash script would look like this:
bash 脚本如下所示:
#! /bin/bash
n=1
CopyAndRename() {
NEWNAME=$n.xx
cp "$i" /newdir/$NEWNAME
n=$[$n+1]
}
IFS=$'\n'
LIST=`find /dir -type f`
for i in $LIST; do
CopyAndRename $i
done
This should also handle filenames with whitespaces and other special characters. For parallelization you could use a program prll and then replace the for loop with
这也应该处理带有空格和其他特殊字符的文件名。对于并行化,您可以使用程序 prll 然后将 for 循环替换为
prll CopyAndRename $LIST
But this really isn't necessary for renaming and copying.
但这对于重命名和复制来说确实不是必需的。
回答by Ole Tange
Using GNU Parallel it will look like this:
使用 GNU Parallel 它将如下所示:
find ... | parallel cp {} /newdir/{#}.xx
It will do this in parallel (one job per core) which may speed up the copying - depending on your storage system.
它将并行执行此操作(每个内核一项作业),这可能会加快复制速度 - 取决于您的存储系统。
GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to.
GNU Parallel 是一个通用的并行器,可以很容易地在同一台机器或您可以 ssh 访问的多台机器上并行运行作业。
If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:
如果您有 32 个不同的作业要在 4 个 CPU 上运行,一个直接的并行化方法是在每个 CPU 上运行 8 个作业:


GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:
GNU Parallel 会在完成后生成一个新进程 - 保持 CPU 处于活动状态,从而节省时间:


Installation
安装
If GNU Parallel is not packaged for your distribution, you can do a personal installation, which does not require root access. It can be done in 10 seconds by doing this:
如果没有为您的发行版打包 GNU Parallel,您可以进行个人安装,这不需要 root 访问权限。这样做可以在 10 秒内完成:
(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash
For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README
对于其他安装选项,请参阅http://git.savannah.gnu.org/cgit/parallel.git/tree/README
Learn more
了解更多
See more examples: http://www.gnu.org/software/parallel/man.html
查看更多示例:http: //www.gnu.org/software/parallel/man.html
Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
观看介绍视频:https: //www.youtube.com/playlist?list =PL284C9FF2488BC6D1
Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html
演练教程:http: //www.gnu.org/software/parallel/parallel_tutorial.html
Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel
注册电子邮件列表以获得支持:https: //lists.gnu.org/mailman/listinfo/parallel

