Per*_*vic 5 recursive file-copy rename
如果我有输入文件夹files_input
具有像subfilders 01-2015
,02-2015
,03-2015
等等,所有这些子文件夹有其他的子文件夹。每个子文件夹只有一个名为index.html
.
如何将所有这些index.html
文件复制到一个名为的文件夹中,files_output
以便它们最终像同一文件夹中的单独文件一样。他们当然应该重命名,我已经尝试使用 --backup ......
我试过了
find files_input -name \*.html -exec cp --backup=t '{}' files_output \;
给它们编号,但只复制一个文件而没有其他任何内容。
我不知道这有什么改变,但我正在使用 zsh,以下是版本:
$ zsh --version | head -1
zsh 5.0.2 (x86_64-pc-linux-gnu)
$ bash --version | head -1
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
$ cp --version | head -1
cp (GNU coreutils) 8.21
$ find --version | head -1
find (GNU findutils) 4.4.2
Run Code Online (Sandbox Code Playgroud)
想法?
编辑:
试图运行例如以下
cp --backup=t files_input/01-2015/index.html files_output
Run Code Online (Sandbox Code Playgroud)
连续五次仍然给我一个 index.html 在files_output
文件夹中!cp坏了吗?为什么我没有五个不同的文件?
当您是zsh
用户时:
$ tree files_input
files_input
|-- 01_2015
| |-- subfolder-1
| | `-- index.html
| |-- subfolder-2
| | `-- index.html
| |-- subfolder-3
| | `-- index.html
| |-- subfolder-4
| | `-- index.html
| `-- subfolder-5
| `-- index.html
|-- 02_2015
| |-- subfolder-1
| | `-- index.html
| |-- subfolder-2
| | `-- index.html
| |-- subfolder-3
| | `-- index.html
| |-- subfolder-4
| | `-- index.html
| `-- subfolder-5
| `-- index.html
(etc.)
Run Code Online (Sandbox Code Playgroud)
$ mkdir -p files_output
$ autoload -U zmv
$ zmv -C './files_input/(*)/(*)/index.html' './files_output/$1-$2-index.html'
$ tree files_output
files_output
|-- 01_2015-subfolder-1-index.html
|-- 01_2015-subfolder-2-index.html
|-- 01_2015-subfolder-3-index.html
|-- 01_2015-subfolder-4-index.html
|-- 01_2015-subfolder-5-index.html
|-- 02_2015-subfolder-1-index.html
|-- 02_2015-subfolder-2-index.html
(etc.)
Run Code Online (Sandbox Code Playgroud)
这里发生的事情是我们使命令zmv
可用autoload -U zmv
。此命令用于重命名、复制或链接与zsh
扩展通配模式匹配的文件。
我们使用zmv
它的-C
选项,告诉它复制文件(而不是移动它们,这是默认的)。然后,我们指定一个与我们想要复制的文件相匹配的模式,./files_input/(*)/(*)/index.html
. 两者(*)
匹配两级子目录名称,我们将它们放在括号内,以便在每个文件的新名称中使用。每个文件的新名称是第二个参数 ,./files_output/$1-$2-index.html
其中$1
和$2
是模式中括号捕获的字符串,即对子目录名称的反向引用。两个参数都应该用单引号引起来。