fib*_*n82 5 unix bash copy file
大家好我对unix命令行有疑问.我有很多这样的文件:
/f/f1/file.txt
/f/f2/file.txt
/f/f3/file.txt
and so on...
Run Code Online (Sandbox Code Playgroud)
我想将file.txt他们的父文件夹复制到另一个文件夹中,例如:
/g/f1/file.txt
/g/f2/file.txt
/g/f3/file.txt
Run Code Online (Sandbox Code Playgroud)
我无法复制所有内容,folder f因为在每个sub-folder f1, f2, ...我有许多其他文件,我不想复制.
我怎么能用命令行执行此操作?最终使用bash脚本?
谢谢!
手册cp显示此选项 -
--parents
use full source file name under DIRECTORY
Run Code Online (Sandbox Code Playgroud)
所以,如果你在,bash v4你可以做这样的事情 -
[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
Run Code Online (Sandbox Code Playgroud)