Bash - 从目录复制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32306108/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:31:10  来源:igfitidea点击:

Bash - Copy files from directory

bashcopy

提问by Christian Giupponi

I have a folder which contains some folders:

我有一个文件夹,其中包含一些文件夹:

main
  \_ Dir 1
  \_ Dir 2
  \_ ...
  \_ Dir 40

I need to open each sub-folder, copy all the files and paste them in another folder, the same folder for all this sub-folder.

我需要打开每个子文件夹,复制所有文件并将它们粘贴到另一个文件夹中,所有这些子文件夹都在同一个文件夹中。

How can I do that in a smart way?

我怎样才能以聪明的方式做到这一点?

The only thing that comes to my mind was create a list with the name of all folders and then use a simple script to open, copy and paste but I'm sure that there is a faster way than write all the names.

我唯一想到的是创建一个包含所有文件夹名称的列表,然后使用一个简单的脚本打开、复制和粘贴,但我确信有比写所有名称更快的方法。

回答by John1024

Try:

尝试:

cp main/*/* /path/to/otherfolder/

If you want to be warned before overwriting a file, use the -ioption:

如果您想在覆盖文件之前收到警告,请使用以下-i选项:

cp -i main/*/* /path/to/otherfolder/

回答by anubhava

If I understood your requirement correct, you can try this findcommand:

如果我理解你的要求是正确的,你可以试试这个find命令:

cd main

find . -mindepth 1 -type f -exec cp '{}' /dest/dir +

Assuming your filenames are not duplicate across the sub folders in maindirectory.

假设您的文件名在main目录中的子文件夹中不重复。