将具有目录结构的特定子文件夹复制到新文件夹

将具有目录结构的特定子文件夹复制到新文件夹

我有以下目录结构:

                            Main_Dir
                               |
              -----------------------------------
            Subdir1         Subdir2         Subdir3
               |               |               |
          -----------     -----------     -----------
          |    |    |     |    |    |     |    |    |            
         fo1  fo2  f03   fo1  fo2  f03   fo1  fo2  f03

我想将所有子目录 (Subdir1、Subdir2、Subdir3) 复制到新文件夹。但我如何才能仅将 fo1 和 fo2 文件夹复制到新位置?

答案1

使用rsync

rsync -av --exclude="f03" /path/to/Main_Dir/ /pth/to/destination

答案2

最好的选择是使用 glob 及其cp选项--parents。对于你的情况:

cp -nvr --parents -- Main_Dir/*/fo{1,2} Destination/

看:

gniourf@somewhere$ mkdir -pv Main_Dir/Subdir{1..3}/f{o{1,2},03}
mkdir: created directory `Main_Dir'
mkdir: created directory `Main_Dir/Subdir1'
mkdir: created directory `Main_Dir/Subdir1/fo1'
mkdir: created directory `Main_Dir/Subdir1/fo2'
mkdir: created directory `Main_Dir/Subdir1/f03'
mkdir: created directory `Main_Dir/Subdir2'
mkdir: created directory `Main_Dir/Subdir2/fo1'
mkdir: created directory `Main_Dir/Subdir2/fo2'
mkdir: created directory `Main_Dir/Subdir2/f03'
mkdir: created directory `Main_Dir/Subdir3'
mkdir: created directory `Main_Dir/Subdir3/fo1'
mkdir: created directory `Main_Dir/Subdir3/fo2'
mkdir: created directory `Main_Dir/Subdir3/f03'
gniourf@somewhere$ tree Main_Dir/
Main_Dir/
|-- Subdir1
|   |-- f03
|   |-- fo1
|   `-- fo2
|-- Subdir2
|   |-- f03
|   |-- fo1
|   `-- fo2
`-- Subdir3
    |-- f03
    |-- fo1
    `-- fo2

12 directories, 0 files
gniourf@somewhere$ mkdir Destination
gniourf@somewhere$ cp -nvr --parents -- Main_Dir/*/fo{1,2} Destination/
Main_Dir -> Destination/Main_Dir
Main_Dir/Subdir1 -> Destination/Main_Dir/Subdir1
`Main_Dir/Subdir1/fo1' -> `Destination/Main_Dir/Subdir1/fo1'
Main_Dir/Subdir2 -> Destination/Main_Dir/Subdir2
`Main_Dir/Subdir2/fo1' -> `Destination/Main_Dir/Subdir2/fo1'
Main_Dir/Subdir3 -> Destination/Main_Dir/Subdir3
`Main_Dir/Subdir3/fo1' -> `Destination/Main_Dir/Subdir3/fo1'
`Main_Dir/Subdir1/fo2' -> `Destination/Main_Dir/Subdir1/fo2'
`Main_Dir/Subdir2/fo2' -> `Destination/Main_Dir/Subdir2/fo2'
`Main_Dir/Subdir3/fo2' -> `Destination/Main_Dir/Subdir3/fo2'
gniourf@somewhere$ tree Destination
Destination
`-- Main_Dir
    |-- Subdir1
    |   |-- fo1
    |   `-- fo2
    |-- Subdir2
    |   |-- fo1
    |   `-- fo2
    `-- Subdir3
        |-- fo1
        `-- fo2

10 directories, 0 files
gniourf@somewhere$ # Done!

确保将--parent选项与-r开关一起使用。-v我使用的选项是使命令详细(显示正在执行的操作),而选项-n没有破坏以免覆盖其他现有的文件(我经常使用这个!)。

如果您不想Main_Dir位于目录中,请直接从 Main_Dir 中执行此操作:

gniourf@somewhere$ mkdir -p Main_Dir/Subdir{1..3}/f{o{1,2},03} Destination
gniourf@somewhere$ cd Main_Dir/
gniourf@somewhere$ cp -nvr --parents -- */fo{1,2} ../Destination
Subdir1 -> ../Destination/Subdir1
`Subdir1/fo1' -> `../Destination/Subdir1/fo1'
Subdir2 -> ../Destination/Subdir2
`Subdir2/fo1' -> `../Destination/Subdir2/fo1'
Subdir3 -> ../Destination/Subdir3
`Subdir3/fo1' -> `../Destination/Subdir3/fo1'
`Subdir1/fo2' -> `../Destination/Subdir1/fo2'
`Subdir2/fo2' -> `../Destination/Subdir2/fo2'
`Subdir3/fo2' -> `../Destination/Subdir3/fo2'
gniourf@somewhere$ cd ..
gniourf@somewhere$ tree Destination/
Destination/
|-- Subdir1
|   |-- fo1
|   `-- fo2
|-- Subdir2
|   |-- fo1
|   `-- fo2
`-- Subdir3
    |-- fo1
    `-- fo2

9 directories, 0 files
gniourf@somewhere$ 

答案3

就你的情况而言,如果文件夹 fo1、fo2、fo3 有不同的名称,这是有可能的,因为一个文件夹中不能有 3 个同名的文件夹。因此,如果它看起来像这样:

user@host:~/test/1# find
.
./main
./main/sub1
./main/sub1/fo3
./main/sub1/fo1
./main/sub1/fo2
./main/sub2
./main/sub2/fo6
./main/sub2/fo4
./main/sub2/fo5
./main/sub3
./main/sub3/fo9
./main/sub3/fo8
./main/sub3/fo7

...您可以通过以下方式复制(仅限子文件夹):

user@host:~/test/1# cp -R main/*/fo* ../2/

现在../2/看起来像这样:

user@host:~/test/1# cd ../2/
user@host:~/test/2# ls
fo1  fo2  fo3  fo4  fo5  fo6  fo7  fo8  fo9

相关内容