copy many files (same name) contained in different father folder

风格不统一 提交于 2020-01-02 04:05:37

问题


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...

I'd like copy all file.txt with their father folder in another folder g like:

/g/f1/file.txt

/g/f2/file.txt

/g/f3/file.txt

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.

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

Thank you!


回答1:


Manual for cp shows this option -

--parents
              use full source file name under DIRECTORY

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



回答2:


tar is sometimes helpful for coping files: see the small test:

kent$  tree t g
t
|-- t1
|   |-- file
|   `-- foo ---->####this file we won't copy
|-- t2
|   `-- file
`-- t3
    `-- file
g

3 directories, 4 files

kent$  cd t

kent$  find -name "file"|xargs tar -cf - | tar -xf - -C ../g

kent$  tree ../t ../g
../t
|-- t1
|   |-- file
|   `-- foo
|-- t2
|   `-- file
`-- t3
    `-- file
../g
|-- t1
|   `-- file
|-- t2
|   `-- file
`-- t3
    `-- file



回答3:


Take a look at rsync. Assuming you are in '/',

rsync -r f/ g/ --include "*/" --include "*/file.txt" --exclude "*"

The first include is necessary to tell rsync to look inside the subdirectories (and counteract the last exclude). The second include selects the files you want to copy. The exclude makes sure that other files are not processed in /f that are not of the required pattern.

Note: in case you have symbolic links, rsync will copy the link and not the file the link points to, unless you specify --copy-links.

Example:

$ find f g -type f
f/f1/file.txt
f/f1/fileNew.txt
f/f2/file.txt
f/f3/file.txt
find: g: No such file or directory
$ rsync -r f/ g/ --include "*/" --include "*/file.txt" --exclude "*"
$ find f g -type f
f/f1/file.txt
f/f1/fileNew.txt
f/f2/file.txt
f/f3/file.txt
g/f1/file.txt
g/f2/file.txt
g/f3/file.txt



回答4:


This seems like it's going to help you :

find /f/ -name file.txt -execdir cp -R . /g/ \;

It locates all the files named file.txt in directory /f/, and then, using execdir (which is executed in the directory containing the matched file), copies the directory containing the file to directory /g/.



来源:https://stackoverflow.com/questions/9021931/copy-many-files-same-name-contained-in-different-father-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!