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
Bash - Copy files from directory
提问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 -i
option:
如果您想在覆盖文件之前收到警告,请使用以下-i
选项:
cp -i main/*/* /path/to/otherfolder/
回答by anubhava
If I understood your requirement correct, you can try this find
command:
如果我理解你的要求是正确的,你可以试试这个find
命令:
cd main
find . -mindepth 1 -type f -exec cp '{}' /dest/dir +
Assuming your filenames are not duplicate across the sub folders in main
directory.
假设您的文件名在main
目录中的子文件夹中不重复。