复制包含在不同的文件夹父亲许多文件(名称相同) [英] copy many files (same name) contained in different father folder

查看:122
本文介绍了复制包含在不同的文件夹父亲许多文件(名称相同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

家伙,您好我有一个关于UNIX命令行的问题。我有很多的文件这样的:

Hi guys I have a question about unix command line. I have many files like these:

/f/f1/file.txt

/f/f2/file.txt

/f/f3/file.txt

and so on...

我想复制所有 file.txt的文件夹父亲在其他的文件夹摹,如:

/g/f1/file.txt

/g/f2/file.txt

/g/f3/file.txt

我不能复制文件夹˚F的所有内容,因为在每个子文件夹F1,F2,... 我有我不想复制许多其他文件。

I can't copy all content of folder f because in each sub-folder f1, f2, ... I have many other files that I don't want copy.

我怎么可以这样使用命令行?最终使用bash脚本?

How could I do this with the command line? Eventually using a bash script?

感谢您!

推荐答案

手册 CP 显示此选项 -

Manual for cp shows this option -

--parents
              use full source file name under DIRECTORY

所以,如果你是在庆典V4 你可以做这样的事情 -

So if you are on bash v4 you can do something like this -

[jaypal:~/Temp/f] tree
.
├── f1
│   ├── file.txt  # copy this file only with parent directory f1
│   ├── file1.txt
│   └── file2.txt
└── f2
    ├── file.txt  # copy this file only with parent directory f2
    ├── file1.txt
    └── file2.txt

2 directories, 6 files
[jaypal:~/Temp/f] mkdir ../g
[jaypal:~/Temp/f] shopt -s globstar
[jaypal:~/Temp/f] for file in ./**/file.txt; do cp --parents "$file" ../g ; done
[jaypal:~/Temp/f] tree ../g
../g
├── f1
│   └── file.txt
└── f2
    └── file.txt

2 directories, 2 files

这篇关于复制包含在不同的文件夹父亲许多文件(名称相同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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