从所有子目录复制具有特定扩展名的所有文件 [英] Copy all files with a certain extension from all subdirectories

查看:129
本文介绍了从所有子目录复制具有特定扩展名的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unix下,我想将具有特定扩展名的所有文件(所有excel文件)从所有子目录复制到另一个目录.我有以下命令:

Under unix, I want to copy all files with a certain extension (all excel files) from all subdirectories to another directory. I have the following command:

cp --parents `find -name \*.xls*` /target_directory/

此命令的问题是:

  • 它也复制目录结构,我只想要文件(因此所有文件都应该以/target_directory/结尾)

  • It copies the directory structure as well, and I only want the files (so all files should end up in /target_directory/)

它不会复制文件名中带有空格的文件(相当多)

It does not copy files with spaces in the filenames (which are quite a few)

有解决这些问题的办法吗?

Any solutions for these problems?

推荐答案

--parents正在复制目录结构,因此您应该删除它.

--parents is copying the directory structure, so you should get rid of that.

您编写此代码的方式,执行find并将输出放入命令行,以使cp不能区分分隔文件名的空格和 within 文件名.最好做类似的事情

The way you've written this, the find executes, and the output is put onto the command line such that cp can't distinguish between the spaces separating the filenames, and the spaces within the filename. It's better to do something like

$ find . -name \*.xls -exec cp {} newDir \;

,其中对find找到的每个文件名执行cp,并正确传递了文件名. 有关此技术的更多信息.

in which cp is executed for each filename that find finds, and passed the filename correctly. Here's more info on this technique.

您可以使用 zsh 代替上述所有内容,只需键入

Instead of all the above, you could use zsh and simply type

$ cp **/*.xls target_directory

zsh可以扩展通配符以包含子目录,并使这种事情变得非常容易.

zsh can expand wildcards to include subdirectories and makes this sort of thing very easy.

这篇关于从所有子目录复制具有特定扩展名的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆